yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/route.go (about) 1 // Copyright 2019 Yunion 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package aws 16 17 import ( 18 "strings" 19 20 api "yunion.io/x/cloudmux/pkg/apis/compute" 21 "yunion.io/x/cloudmux/pkg/multicloud" 22 ) 23 24 type SRoute struct { 25 multicloud.SResourceBase 26 AwsTags 27 routetable *SRouteTable 28 29 DestinationCIDRBlock string `json:"DestinationCidrBlock"` 30 Origin string `json:"Origin"` 31 State string `json:"State"` 32 // only one exist 33 GatewayID *string `json:"GatewayId,omitempty"` 34 NatGatewayID *string `json:"NatGatewayId,omitempty"` 35 InstanceID *string `json:"InstanceId,omitempty"` 36 LocalGatewayID *string `json:"LocalGatewayId,omitempty"` 37 NetworkInterfaceID *string `json:"NetworkInterfaceId,omitempty"` 38 TransitGatewayID *string `json:"TransitGatewayId,omitempty"` 39 VpcPeeringConnectionID *string `json:"VpcPeeringConnectionId,omitempty"` 40 } 41 42 func (self *SRoute) GetId() string { 43 return self.DestinationCIDRBlock + ":" + self.GetNextHop() 44 } 45 46 func (self *SRoute) GetName() string { 47 return "" 48 } 49 50 func (self *SRoute) GetGlobalId() string { 51 return self.GetId() 52 } 53 54 func (self *SRoute) GetStatus() string { 55 if self.State == "active" { 56 return api.ROUTE_ENTRY_STATUS_AVAILIABLE 57 } 58 return api.ROUTE_ENTRY_STATUS_UNKNOWN 59 } 60 61 func (self *SRoute) Refresh() error { 62 return nil 63 } 64 65 func (self *SRoute) IsEmulated() bool { 66 return false 67 } 68 69 func (self *SRoute) GetType() string { 70 switch self.Origin { 71 case "CreateRouteTable": 72 return api.ROUTE_ENTRY_TYPE_SYSTEM 73 case "CreateRoute": 74 return api.ROUTE_ENTRY_TYPE_CUSTOM 75 case "EnableVgwRoutePropagation": 76 return api.ROUTE_ENTRY_TYPE_PROPAGATE 77 default: 78 return api.ROUTE_ENTRY_TYPE_SYSTEM 79 } 80 } 81 82 func (self *SRoute) GetCidr() string { 83 return self.DestinationCIDRBlock 84 } 85 86 func (self *SRoute) GetNextHopType() string { 87 segs := strings.Split(self.GetNextHop(), "-") 88 if len(segs) == 0 { 89 return "" 90 } 91 92 switch segs[0] { 93 case "i": 94 return api.NEXT_HOP_TYPE_INSTANCE 95 case "vgw": 96 return api.NEXT_HOP_TYPE_VPN 97 case "pcx": 98 return api.NEXT_HOP_TYPE_VPCPEERING 99 case "eni": 100 return api.NEXT_HOP_TYPE_NETWORK 101 case "nat": 102 return api.NEXT_HOP_TYPE_NAT 103 case "igw": 104 return api.NEXT_HOP_TYPE_INTERNET 105 case "eigw": 106 return api.NEXT_HOP_TYPE_EGRESS_INTERNET 107 default: 108 return "" 109 } 110 } 111 112 func (self *SRoute) GetNextHop() string { 113 if self.NatGatewayID != nil { 114 return *self.NatGatewayID 115 } 116 if self.GatewayID != nil { 117 return *self.GatewayID 118 } 119 if self.InstanceID != nil { 120 return *self.InstanceID 121 } 122 if self.LocalGatewayID != nil { 123 return *self.LocalGatewayID 124 } 125 if self.NetworkInterfaceID != nil { 126 return *self.NetworkInterfaceID 127 } 128 if self.TransitGatewayID != nil { 129 return *self.TransitGatewayID 130 } 131 if self.VpcPeeringConnectionID != nil { 132 return *self.VpcPeeringConnectionID 133 } 134 135 return "" 136 }