natdtable.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 SNatDEntry struct {
  22. multicloud.SResourceBase
  23. huawei.HuaweiTags
  24. gateway *SNatGateway
  25. ID string `json:"id"`
  26. NatGatewayID string `json:"nat_gateway_id"`
  27. Protocol string `json:"protocol"`
  28. Status string `json:"status"`
  29. ExternalIP string `json:"floating_ip_address"`
  30. ExternalPort int `json:"external_service_port"`
  31. InternalIP string `json:"private_ip"`
  32. InternalPort int `json:"internal_service_port"`
  33. PortID string `json:"port_id"`
  34. AdminStateUp bool `json:"admin_state_up"`
  35. }
  36. func (nat *SNatDEntry) GetId() string {
  37. return nat.ID
  38. }
  39. func (nat *SNatDEntry) GetName() string {
  40. // No name so return id
  41. return nat.GetId()
  42. }
  43. func (nat *SNatDEntry) GetGlobalId() string {
  44. return nat.GetId()
  45. }
  46. func (nat *SNatDEntry) GetStatus() string {
  47. return NatResouceStatusTransfer(nat.Status)
  48. }
  49. func (nat *SNatDEntry) GetIpProtocol() string {
  50. return nat.Protocol
  51. }
  52. func (nat *SNatDEntry) GetExternalIp() string {
  53. return nat.ExternalIP
  54. }
  55. func (nat *SNatDEntry) GetExternalPort() int {
  56. return nat.ExternalPort
  57. }
  58. func (nat *SNatDEntry) GetInternalIp() string {
  59. return nat.InternalIP
  60. }
  61. func (nat *SNatDEntry) GetInternalPort() int {
  62. return nat.InternalPort
  63. }
  64. func (nat *SNatDEntry) Delete() error {
  65. return nat.gateway.region.DeleteNatDEntry(nat.GetId())
  66. }
  67. // getNatSTable return all snat rules of gateway
  68. func (gateway *SNatGateway) getNatDTable() ([]SNatDEntry, error) {
  69. ret, err := gateway.region.GetNatDTable(gateway.GetId())
  70. if err != nil {
  71. return nil, err
  72. }
  73. for i := range ret {
  74. ret[i].gateway = gateway
  75. }
  76. return ret, nil
  77. }
  78. func (region *SRegion) GetNatDTable(natGatewayID string) ([]SNatDEntry, error) {
  79. queuies := map[string]string{
  80. "nat_gateway_id": natGatewayID,
  81. }
  82. dNatSTableEntries := make([]SNatDEntry, 0, 2)
  83. // can't make true that restapi support marker para in Huawei Cloud
  84. err := doListAllWithMarker(region.ecsClient.DNatRules.List, queuies, &dNatSTableEntries)
  85. if err != nil {
  86. return nil, errors.Wrapf(err, `get dnat rule of gateway %q`, natGatewayID)
  87. }
  88. for i := range dNatSTableEntries {
  89. nat := &dNatSTableEntries[i]
  90. if len(nat.InternalIP) == 0 {
  91. port, err := region.GetPort(nat.PortID)
  92. if err != nil {
  93. return nil, errors.Wrapf(err, `get port info for transfer to ip of port_id %q error`, nat.PortID)
  94. }
  95. nat.InternalIP = port.FixedIps[0].IpAddress
  96. }
  97. }
  98. return dNatSTableEntries, nil
  99. }
  100. func (region *SRegion) DeleteNatDEntry(entryID string) error {
  101. _, err := region.ecsClient.DNatRules.Delete(entryID, nil)
  102. if err != nil {
  103. return errors.Wrapf(err, `delete dnat rule %q failed`, entryID)
  104. }
  105. return nil
  106. }
  107. func (nat *SNatDEntry) Refresh() error {
  108. new, err := nat.gateway.region.GetNatDEntryByID(nat.ID)
  109. if err != nil {
  110. return err
  111. }
  112. return jsonutils.Update(nat, new)
  113. }