natgateway.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 apsara
  15. import (
  16. "fmt"
  17. "time"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. )
  24. type SBandwidthPackageIds struct {
  25. BandwidthPackageId []string
  26. }
  27. type SForwardTableIds struct {
  28. ForwardTableId []string
  29. }
  30. type SSnatTableIds struct {
  31. SnatTableId []string
  32. }
  33. type SNatGetway struct {
  34. multicloud.SNatGatewayBase
  35. ApsaraTags
  36. vpc *SVpc
  37. BandwidthPackageIds SBandwidthPackageIds
  38. BusinessStatus string
  39. CreationTime time.Time
  40. ExpiredTime time.Time
  41. NetworkType string
  42. Description string
  43. ForwardTableIds SForwardTableIds
  44. SnatTableIds SSnatTableIds
  45. InstanceChargeType TChargeType
  46. Name string
  47. NatGatewayId string
  48. RegionId string
  49. Spec string
  50. Status string
  51. VpcId string
  52. }
  53. func (nat *SNatGetway) GetId() string {
  54. return nat.NatGatewayId
  55. }
  56. func (nat *SNatGetway) GetGlobalId() string {
  57. return nat.NatGatewayId
  58. }
  59. func (nat *SNatGetway) GetName() string {
  60. if len(nat.Name) > 0 {
  61. return nat.Name
  62. }
  63. return nat.NatGatewayId
  64. }
  65. func (self *SNatGetway) GetINetworkId() string {
  66. return ""
  67. }
  68. func (nat *SNatGetway) GetStatus() string {
  69. switch nat.Status {
  70. case "Initiating":
  71. return api.NAT_STATUS_ALLOCATE
  72. case "Available":
  73. return api.NAT_STAUTS_AVAILABLE
  74. case "Pending":
  75. return api.NAT_STATUS_DEPLOYING
  76. default:
  77. return api.NAT_STATUS_UNKNOWN
  78. }
  79. }
  80. func (nat *SNatGetway) GetBillingType() string {
  81. return convertChargeType(nat.InstanceChargeType)
  82. }
  83. func (nat *SNatGetway) GetNatSpec() string {
  84. return nat.Spec
  85. }
  86. func (nat *SNatGetway) GetNetworkType() string {
  87. return nat.NetworkType
  88. }
  89. func (nat *SNatGetway) GetCreatedAt() time.Time {
  90. return nat.CreationTime
  91. }
  92. func (nat *SNatGetway) GetIEips() ([]cloudprovider.ICloudEIP, error) {
  93. eips := []SEipAddress{}
  94. for {
  95. parts, total, err := nat.vpc.region.GetEips("", nat.NatGatewayId, len(eips), 50)
  96. if err != nil {
  97. return nil, err
  98. }
  99. eips = append(eips, parts...)
  100. if len(eips) >= total {
  101. break
  102. }
  103. }
  104. ieips := []cloudprovider.ICloudEIP{}
  105. for i := 0; i < len(eips); i++ {
  106. eips[i].region = nat.vpc.region
  107. ieips = append(ieips, &eips[i])
  108. }
  109. return ieips, nil
  110. }
  111. func (nat *SNatGetway) GetINatDTable() ([]cloudprovider.ICloudNatDEntry, error) {
  112. itables := []cloudprovider.ICloudNatDEntry{}
  113. for _, dtableId := range nat.ForwardTableIds.ForwardTableId {
  114. dtables, err := nat.vpc.region.GetAllDTables(dtableId)
  115. if err != nil {
  116. return nil, err
  117. }
  118. for i := 0; i < len(dtables); i++ {
  119. dtables[i].nat = nat
  120. itables = append(itables, &dtables[i])
  121. }
  122. }
  123. return itables, nil
  124. }
  125. func (nat *SNatGetway) GetINatSTable() ([]cloudprovider.ICloudNatSEntry, error) {
  126. stables, err := nat.getSnatEntries()
  127. if err != nil {
  128. return nil, err
  129. }
  130. itables := []cloudprovider.ICloudNatSEntry{}
  131. for i := 0; i < len(stables); i++ {
  132. stables[i].nat = nat
  133. itables = append(itables, &stables[i])
  134. }
  135. return itables, nil
  136. }
  137. func (nat *SNatGetway) GetINatDEntryById(id string) (cloudprovider.ICloudNatDEntry, error) {
  138. dNATEntry, err := nat.vpc.region.GetForwardTableEntry(nat.ForwardTableIds.ForwardTableId[0], id)
  139. if err != nil {
  140. return nil, cloudprovider.ErrNotFound
  141. }
  142. dNATEntry.nat = nat
  143. return &dNATEntry, nil
  144. }
  145. func (nat *SNatGetway) GetINatSEntryById(id string) (cloudprovider.ICloudNatSEntry, error) {
  146. sNATEntry, err := nat.vpc.region.GetSNATEntry(nat.SnatTableIds.SnatTableId[0], id)
  147. if err != nil {
  148. return nil, cloudprovider.ErrNotFound
  149. }
  150. sNATEntry.nat = nat
  151. return &sNATEntry, nil
  152. }
  153. func (nat *SNatGetway) CreateINatDEntry(rule cloudprovider.SNatDRule) (cloudprovider.ICloudNatDEntry, error) {
  154. entryID, err := nat.vpc.region.CreateForwardTableEntry(rule, nat.ForwardTableIds.ForwardTableId[0])
  155. if err != nil {
  156. return nil, errors.Wrapf(err, `create dnat rule for nat gateway %q`, nat.GetId())
  157. }
  158. return nat.GetINatDEntryById(entryID)
  159. }
  160. func (nat *SNatGetway) CreateINatSEntry(rule cloudprovider.SNatSRule) (cloudprovider.ICloudNatSEntry, error) {
  161. entryID, err := nat.vpc.region.CreateSNATTableEntry(rule, nat.SnatTableIds.SnatTableId[0])
  162. if err != nil {
  163. return nil, errors.Wrapf(err, `create snat rule for nat gateway %q`, nat.GetId())
  164. }
  165. return nat.GetINatSEntryById(entryID)
  166. }
  167. func (self *SRegion) GetNatGateways(vpcId string, natGwId string, offset, limit int) ([]SNatGetway, int, error) {
  168. if limit > 50 || limit <= 0 {
  169. limit = 50
  170. }
  171. params := make(map[string]string)
  172. params["RegionId"] = self.RegionId
  173. params["PageSize"] = fmt.Sprintf("%d", limit)
  174. params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1)
  175. if len(vpcId) > 0 {
  176. params["VpcId"] = vpcId
  177. }
  178. if len(natGwId) > 0 {
  179. params["NatGatewayId"] = natGwId
  180. }
  181. body, err := self.vpcRequest("DescribeNatGateways", params)
  182. if err != nil {
  183. log.Errorf("GetVSwitches fail %s", err)
  184. return nil, 0, err
  185. }
  186. if self.client.debug {
  187. log.Debugf("%s", body.PrettyString())
  188. }
  189. gateways := make([]SNatGetway, 0)
  190. err = body.Unmarshal(&gateways, "NatGateways", "NatGateway")
  191. if err != nil {
  192. log.Errorf("Unmarshal gateways fail %s", err)
  193. return nil, 0, err
  194. }
  195. total, _ := body.Int("TotalCount")
  196. return gateways, int(total), nil
  197. }