route.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. package aws
  15. import (
  16. "strings"
  17. api "yunion.io/x/cloudmux/pkg/apis/compute"
  18. "yunion.io/x/cloudmux/pkg/multicloud"
  19. )
  20. type SRoute struct {
  21. multicloud.SResourceBase
  22. AwsTags
  23. routetable *SRouteTable
  24. DestinationCIDRBlock string `json:"DestinationCidrBlock"`
  25. Origin string `json:"Origin"`
  26. State string `json:"State"`
  27. // only one exist
  28. GatewayId string `xml:"gatewayId,omitempty"`
  29. NatGatewayId string `xml:"natGatewayId,omitempty"`
  30. InstanceId string `xml:"instanceId,omitempty"`
  31. LocalGatewayId string `xml:"localGatewayId,omitempty"`
  32. NetworkInterfaceId string `xml:"networkInterfaceId,omitempty"`
  33. TransitGatewayId string `xml:"transitGatewayId,omitempty"`
  34. VpcPeeringConnectionId string `xml:"vpcPeeringConnectionId,omitempty"`
  35. }
  36. func (self *SRoute) GetId() string {
  37. return self.DestinationCIDRBlock + ":" + self.GetNextHop()
  38. }
  39. func (self *SRoute) GetName() string {
  40. return ""
  41. }
  42. func (self *SRoute) GetGlobalId() string {
  43. return self.GetId()
  44. }
  45. func (self *SRoute) GetStatus() string {
  46. if self.State == "active" {
  47. return api.ROUTE_ENTRY_STATUS_AVAILIABLE
  48. }
  49. return api.ROUTE_ENTRY_STATUS_UNKNOWN
  50. }
  51. func (self *SRoute) Refresh() error {
  52. return nil
  53. }
  54. func (self *SRoute) IsEmulated() bool {
  55. return false
  56. }
  57. func (self *SRoute) GetType() string {
  58. switch self.Origin {
  59. case "CreateRouteTable":
  60. return api.ROUTE_ENTRY_TYPE_SYSTEM
  61. case "CreateRoute":
  62. return api.ROUTE_ENTRY_TYPE_CUSTOM
  63. case "EnableVgwRoutePropagation":
  64. return api.ROUTE_ENTRY_TYPE_PROPAGATE
  65. default:
  66. return api.ROUTE_ENTRY_TYPE_SYSTEM
  67. }
  68. }
  69. func (self *SRoute) GetCidr() string {
  70. return self.DestinationCIDRBlock
  71. }
  72. func (self *SRoute) GetNextHopType() string {
  73. segs := strings.Split(self.GetNextHop(), "-")
  74. if len(segs) == 0 {
  75. return ""
  76. }
  77. switch segs[0] {
  78. case "i":
  79. return api.NEXT_HOP_TYPE_INSTANCE
  80. case "vgw":
  81. return api.NEXT_HOP_TYPE_VPN
  82. case "pcx":
  83. return api.NEXT_HOP_TYPE_VPCPEERING
  84. case "eni":
  85. return api.NEXT_HOP_TYPE_NETWORK
  86. case "nat":
  87. return api.NEXT_HOP_TYPE_NAT
  88. case "igw":
  89. return api.NEXT_HOP_TYPE_INTERNET
  90. case "eigw":
  91. return api.NEXT_HOP_TYPE_EGRESS_INTERNET
  92. default:
  93. return ""
  94. }
  95. }
  96. func (self *SRoute) GetNextHop() string {
  97. if len(self.NatGatewayId) > 0 {
  98. return self.NatGatewayId
  99. }
  100. if len(self.GatewayId) > 0 {
  101. return self.GatewayId
  102. }
  103. if len(self.InstanceId) > 0 {
  104. return self.InstanceId
  105. }
  106. if len(self.LocalGatewayId) > 0 {
  107. return self.LocalGatewayId
  108. }
  109. if len(self.NetworkInterfaceId) > 0 {
  110. return self.NetworkInterfaceId
  111. }
  112. if len(self.TransitGatewayId) > 0 {
  113. return self.TransitGatewayId
  114. }
  115. if len(self.VpcPeeringConnectionId) > 0 {
  116. return self.VpcPeeringConnectionId
  117. }
  118. return ""
  119. }