natgateway.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 qcloud
  15. import (
  16. "fmt"
  17. "time"
  18. api "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/cloudmux/pkg/multicloud"
  21. )
  22. type SNatGateway struct {
  23. multicloud.SNatGatewayBase
  24. QcloudTags
  25. vpc *SVpc
  26. State string
  27. VpcId string
  28. Zone string
  29. NatGatewayName string
  30. NatGatewayId string
  31. Bandwidth string `json:"bandwidth"`
  32. CreateTime time.Time `json:"createTime"`
  33. EipCount string `json:"eipCount"`
  34. MaxConcurrent float32 `json:"maxConcurrent"`
  35. PublicIpAddressSet []struct {
  36. AddressId string
  37. }
  38. SourceIpTranslationNatRuleSet []SSTable
  39. DestinationIpPortTranslationNatRuleSet []SDTable
  40. }
  41. func (nat *SNatGateway) GetName() string {
  42. if len(nat.NatGatewayName) > 0 {
  43. return nat.NatGatewayName
  44. }
  45. return nat.NatGatewayId
  46. }
  47. func (nat *SNatGateway) GetId() string {
  48. return nat.NatGatewayId
  49. }
  50. func (nat *SNatGateway) GetGlobalId() string {
  51. return nat.NatGatewayId
  52. }
  53. func (self *SNatGateway) GetINetworkId() string {
  54. return ""
  55. }
  56. func (self *SNatGateway) GetNetworkType() string {
  57. return api.NAT_NETWORK_TYPE_INTERNET
  58. }
  59. func (nat *SNatGateway) GetStatus() string {
  60. switch nat.State {
  61. case "PENDING":
  62. return api.NAT_STATUS_ALLOCATE
  63. case "AVAILABLE":
  64. return api.NAT_STAUTS_AVAILABLE
  65. case "UPDATING":
  66. return api.NAT_STATUS_DEPLOYING
  67. case "DELETING":
  68. return api.NAT_STATUS_DELETING
  69. default:
  70. return api.NAT_STATUS_UNKNOWN
  71. }
  72. }
  73. func (nat *SNatGateway) GetNatSpec() string {
  74. switch int(nat.MaxConcurrent) {
  75. case 100 * 10000:
  76. return api.QCLOUD_NAT_SPEC_SMALL
  77. case 300 * 10000:
  78. return api.QCLOUD_NAT_SPEC_MIDDLE
  79. case 1000 * 10000:
  80. return api.QCLOUD_NAT_SPEC_LARGE
  81. }
  82. return ""
  83. }
  84. func (nat *SNatGateway) GetIEips() ([]cloudprovider.ICloudEIP, error) {
  85. eips := []SEipAddress{}
  86. for {
  87. part, total, err := nat.vpc.region.GetEips("", nat.GetId(), len(eips), 50)
  88. if err != nil {
  89. return nil, err
  90. }
  91. eips = append(eips, part...)
  92. if len(eips) >= total {
  93. break
  94. }
  95. }
  96. ieips := []cloudprovider.ICloudEIP{}
  97. for i := 0; i < len(eips); i++ {
  98. eips[i].region = nat.vpc.region
  99. ieips = append(ieips, &eips[i])
  100. }
  101. return ieips, nil
  102. }
  103. func (nat *SNatGateway) GetINatSTable() ([]cloudprovider.ICloudNatSEntry, error) {
  104. ret := []cloudprovider.ICloudNatSEntry{}
  105. for i := 0; i < len(nat.SourceIpTranslationNatRuleSet); i++ {
  106. nat.SourceIpTranslationNatRuleSet[i].nat = nat
  107. ret = append(ret, &nat.SourceIpTranslationNatRuleSet[i])
  108. }
  109. return ret, nil
  110. }
  111. func (nat *SNatGateway) GetINatDTable() ([]cloudprovider.ICloudNatDEntry, error) {
  112. ret := []cloudprovider.ICloudNatDEntry{}
  113. for i := 0; i < len(nat.DestinationIpPortTranslationNatRuleSet); i++ {
  114. nat.DestinationIpPortTranslationNatRuleSet[i].nat = nat
  115. ret = append(ret, &nat.DestinationIpPortTranslationNatRuleSet[i])
  116. }
  117. return ret, nil
  118. }
  119. func (nat *SNatGateway) GetINatDEntryById(id string) (cloudprovider.ICloudNatDEntry, error) {
  120. return nil, cloudprovider.ErrNotImplemented
  121. }
  122. func (nat *SNatGateway) GetINatSEntryById(id string) (cloudprovider.ICloudNatSEntry, error) {
  123. return nil, cloudprovider.ErrNotImplemented
  124. }
  125. func (nat *SNatGateway) CreateINatDEntry(rule cloudprovider.SNatDRule) (cloudprovider.ICloudNatDEntry, error) {
  126. return nil, cloudprovider.ErrNotImplemented
  127. }
  128. func (nat *SNatGateway) CreateINatSEntry(rule cloudprovider.SNatSRule) (cloudprovider.ICloudNatSEntry, error) {
  129. return nil, cloudprovider.ErrNotImplemented
  130. }
  131. func (region *SRegion) GetNatGateways(vpcId string, offset int, limit int) ([]SNatGateway, int, error) {
  132. if limit > 50 || limit <= 0 {
  133. limit = 50
  134. }
  135. params := make(map[string]string)
  136. params["Limit"] = fmt.Sprintf("%d", limit)
  137. params["Offset"] = fmt.Sprintf("%d", offset)
  138. if len(vpcId) > 0 {
  139. params["Filters.0.Name"] = "vpc-id"
  140. params["Filters.0.Values.0"] = vpcId
  141. }
  142. body, err := region.vpcRequest("DescribeNatGateways", params)
  143. if err != nil {
  144. return nil, 0, err
  145. }
  146. nats := []SNatGateway{}
  147. err = body.Unmarshal(&nats, "NatGatewaySet")
  148. if err != nil {
  149. return nil, 0, err
  150. }
  151. total, _ := body.Float("TotalCount")
  152. return nats, int(total), nil
  153. }