natstable.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 hcso
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/cloudmux/pkg/multicloud"
  19. "yunion.io/x/cloudmux/pkg/multicloud/huawei"
  20. )
  21. type SNatSEntry struct {
  22. multicloud.SResourceBase
  23. huawei.HuaweiTags
  24. gateway *SNatGateway
  25. ID string `json:"id"`
  26. NatGatewayID string `json:"nat_gateway_id"`
  27. NetworkID string `json:"network_id"`
  28. SourceCIDR string `json:"cidr"`
  29. Status string `json:"status"`
  30. SNatIP string `json:"floating_ip_address"`
  31. AdminStateUp bool `json:"admin_state_up"`
  32. }
  33. func (nat *SNatSEntry) GetId() string {
  34. return nat.ID
  35. }
  36. func (nat *SNatSEntry) GetName() string {
  37. // Snat rule has no name in Huawei Cloud, so return ID
  38. return nat.GetId()
  39. }
  40. func (nat *SNatSEntry) GetGlobalId() string {
  41. return nat.GetId()
  42. }
  43. func (nat *SNatSEntry) GetStatus() string {
  44. return NatResouceStatusTransfer(nat.Status)
  45. }
  46. func (nat *SNatSEntry) GetIP() string {
  47. return nat.SNatIP
  48. }
  49. func (nat *SNatSEntry) GetSourceCIDR() string {
  50. return nat.SourceCIDR
  51. }
  52. func (nat *SNatSEntry) GetNetworkId() string {
  53. return nat.NetworkID
  54. }
  55. func (nat *SNatSEntry) Delete() error {
  56. return nat.gateway.region.DeleteNatSEntry(nat.GetId())
  57. }
  58. // getNatSTable return all snat rules of gateway
  59. func (gateway *SNatGateway) getNatSTable() ([]SNatSEntry, error) {
  60. ret, err := gateway.region.GetNatSTable(gateway.GetId())
  61. if err != nil {
  62. return nil, err
  63. }
  64. for i := range ret {
  65. ret[i].gateway = gateway
  66. }
  67. return ret, nil
  68. }
  69. func (region *SRegion) GetNatSTable(natGatewayID string) ([]SNatSEntry, error) {
  70. queuies := map[string]string{
  71. "nat_gateway_id": natGatewayID,
  72. }
  73. sNatSTableEntris := make([]SNatSEntry, 0, 2)
  74. err := doListAllWithMarker(region.ecsClient.SNatRules.List, queuies, &sNatSTableEntris)
  75. if err != nil {
  76. return nil, errors.Wrapf(err, `get snat rule of gateway %q`, natGatewayID)
  77. }
  78. for i := range sNatSTableEntris {
  79. nat := &sNatSTableEntris[i]
  80. if len(nat.SourceCIDR) != 0 {
  81. continue
  82. }
  83. subnet := SNetwork{}
  84. err := DoGet(region.ecsClient.Subnets.Get, nat.NetworkID, map[string]string{}, &subnet)
  85. if err != nil {
  86. return nil, errors.Wrapf(err, `get cidr of subnet %q`, nat.NetworkID)
  87. }
  88. nat.SourceCIDR = subnet.CIDR
  89. }
  90. return sNatSTableEntris, nil
  91. }
  92. func (region *SRegion) DeleteNatSEntry(entryID string) error {
  93. _, err := region.ecsClient.SNatRules.Delete(entryID, nil)
  94. if err != nil {
  95. return errors.Wrapf(err, `delete snat rule %q failed`, entryID)
  96. }
  97. return nil
  98. }
  99. func (nat *SNatSEntry) Refresh() error {
  100. new, err := nat.gateway.region.GetNatSEntryByID(nat.ID)
  101. if err != nil {
  102. return err
  103. }
  104. return jsonutils.Update(nat, new)
  105. }