nat.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 huawei
  15. import (
  16. "net/url"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
  21. api "yunion.io/x/cloudmux/pkg/apis/compute"
  22. "yunion.io/x/cloudmux/pkg/cloudprovider"
  23. "yunion.io/x/cloudmux/pkg/multicloud"
  24. )
  25. // 公网NAT
  26. type SNatgateway struct {
  27. multicloud.SNatGatewayBase
  28. HuaweiTags
  29. region *SRegion
  30. Id string
  31. Name string
  32. Description string
  33. Spec string
  34. Status string
  35. AdminStateUp bool
  36. InternalNetworkId string
  37. RouterId string
  38. CreatedAt string `json:"created_at"`
  39. EnterpriseProject_id string
  40. NgportIpAddress string
  41. BillingInfo string
  42. DnatRulesLimit int
  43. SnatRulePublicIpLimit int
  44. }
  45. func (gateway *SNatgateway) GetId() string {
  46. return gateway.Id
  47. }
  48. func (gateway *SNatgateway) GetName() string {
  49. return gateway.Name
  50. }
  51. func (gateway *SNatgateway) GetGlobalId() string {
  52. return gateway.GetId()
  53. }
  54. func (gateway *SNatgateway) GetStatus() string {
  55. return NatResouceStatusTransfer(gateway.Status)
  56. }
  57. func (self *SNatgateway) Delete() error {
  58. return self.region.DeleteNatgateway(self.Id)
  59. }
  60. func (self *SNatgateway) Refresh() error {
  61. nat, err := self.region.GetNatgateway(self.Id)
  62. if err != nil {
  63. return errors.Wrapf(err, "GetNatgateway(%s)", self.Id)
  64. }
  65. return jsonutils.Update(self, nat)
  66. }
  67. func (self *SNatgateway) GetINetworkId() string {
  68. return self.InternalNetworkId
  69. }
  70. func (self *SNatgateway) GetNetworkType() string {
  71. return api.NAT_NETWORK_TYPE_INTERNET
  72. }
  73. func (gateway *SNatgateway) GetNatSpec() string {
  74. switch gateway.Spec {
  75. case "1":
  76. return api.NAT_SPEC_SMALL
  77. case "2":
  78. return api.NAT_SPEC_MIDDLE
  79. case "3":
  80. return api.NAT_SPEC_LARGE
  81. case "4":
  82. return api.NAT_SPEC_XLARGE
  83. }
  84. return gateway.Spec
  85. }
  86. func (gateway *SNatgateway) GetDescription() string {
  87. return gateway.Description
  88. }
  89. func (gateway *SNatgateway) GetBillingType() string {
  90. if len(gateway.BillingInfo) > 0 {
  91. return billing_api.BILLING_TYPE_PREPAID
  92. }
  93. return billing_api.BILLING_TYPE_POSTPAID
  94. }
  95. func (gateway *SNatgateway) GetCreatedAt() time.Time {
  96. t, _ := time.Parse("2006-01-02 15:04:05.000000", gateway.CreatedAt)
  97. return t
  98. }
  99. func (gateway *SNatgateway) GetExpiredAt() time.Time {
  100. // no support for expired time
  101. return time.Time{}
  102. }
  103. func (gateway *SNatgateway) GetIEips() ([]cloudprovider.ICloudEIP, error) {
  104. ports, err := gateway.region.GetPorts(gateway.Id)
  105. if err != nil {
  106. return nil, errors.Wrapf(err, "GetPorts(%s)", gateway.Id)
  107. }
  108. ret := []cloudprovider.ICloudEIP{}
  109. for i := range ports {
  110. eips, err := gateway.region.GetEips(ports[i].ID, nil)
  111. if err != nil {
  112. return nil, err
  113. }
  114. for i := range eips {
  115. eips[i].region = gateway.region
  116. ret = append(ret, &eips[i])
  117. }
  118. }
  119. return ret, nil
  120. }
  121. func (gateway *SNatgateway) GetINatDTable() ([]cloudprovider.ICloudNatDEntry, error) {
  122. dNatTable, err := gateway.region.GetPublicNatDTable(gateway.Id)
  123. if err != nil {
  124. return nil, errors.Wrapf(err, "GetPublicNatDTable")
  125. }
  126. ret := []cloudprovider.ICloudNatDEntry{}
  127. for i := range dNatTable {
  128. dNatTable[i].nat = gateway
  129. ret = append(ret, &dNatTable[i])
  130. }
  131. return ret, nil
  132. }
  133. func (gateway *SNatgateway) GetINatSTable() ([]cloudprovider.ICloudNatSEntry, error) {
  134. sNatTable, err := gateway.region.GetPublicNatSTable(gateway.Id)
  135. if err != nil {
  136. return nil, errors.Wrapf(err, "GetNatSTable")
  137. }
  138. ret := []cloudprovider.ICloudNatSEntry{}
  139. for i := range sNatTable {
  140. sNatTable[i].nat = gateway
  141. ret = append(ret, &sNatTable[i])
  142. }
  143. return ret, nil
  144. }
  145. func (gateway *SNatgateway) GetINatDEntryById(id string) (cloudprovider.ICloudNatDEntry, error) {
  146. dnat, err := gateway.region.GetNatDTable(id)
  147. if err != nil {
  148. return nil, err
  149. }
  150. dnat.nat = gateway
  151. return dnat, nil
  152. }
  153. func (gateway *SNatgateway) GetINatSEntryById(id string) (cloudprovider.ICloudNatSEntry, error) {
  154. snat, err := gateway.region.GetNatSEntry(id)
  155. if err != nil {
  156. return nil, err
  157. }
  158. snat.nat = gateway
  159. return snat, nil
  160. }
  161. func (region *SRegion) GetNatgateways(vpcId, id string) ([]SNatgateway, error) {
  162. query := url.Values{}
  163. if len(id) != 0 {
  164. query.Set("id", id)
  165. }
  166. if len(vpcId) != 0 {
  167. query.Set("router_id", vpcId)
  168. }
  169. ret := []SNatgateway{}
  170. for {
  171. resp, err := region.list(SERVICE_NAT_V2, "nat_gateways", query)
  172. if err != nil {
  173. return nil, err
  174. }
  175. part := struct {
  176. NatGateways []SNatgateway
  177. }{}
  178. err = resp.Unmarshal(&part)
  179. if err != nil {
  180. return nil, err
  181. }
  182. ret = append(ret, part.NatGateways...)
  183. if len(part.NatGateways) == 0 {
  184. break
  185. }
  186. query.Set("marker", part.NatGateways[len(part.NatGateways)-1].Id)
  187. }
  188. return ret, nil
  189. }
  190. func (region *SRegion) GetNatDTable(id string) (*SNatDTable, error) {
  191. resp, err := region.list(SERVICE_NAT_V2, "dnat_rules/"+id, nil)
  192. if err != nil {
  193. return nil, err
  194. }
  195. ret := &SNatDTable{}
  196. err = resp.Unmarshal(ret, "dnat_rule")
  197. if err != nil {
  198. return nil, err
  199. }
  200. return ret, nil
  201. }
  202. func (self *SRegion) GetNatgateway(id string) (*SNatgateway, error) {
  203. resp, err := self.list(SERVICE_NAT_V2, "nat_gateways/"+id, nil)
  204. if err != nil {
  205. return nil, err
  206. }
  207. nat := &SNatgateway{region: self}
  208. err = resp.Unmarshal(nat, "nat_gateway")
  209. if err != nil {
  210. return nil, err
  211. }
  212. return nat, nil
  213. }
  214. func (self *SRegion) DeleteNatgateway(id string) error {
  215. _, err := self.delete(SERVICE_NAT_V2, "nat_gateways/"+id)
  216. return err
  217. }