natdtable.go 2.9 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 huawei
  15. import (
  16. "net/url"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/cloudmux/pkg/multicloud"
  19. )
  20. type SNatDEntry struct {
  21. multicloud.SResourceBase
  22. HuaweiTags
  23. gateway *SNatGateway
  24. ID string `json:"id"`
  25. NatGatewayID string `json:"nat_gateway_id"`
  26. Protocol string `json:"protocol"`
  27. Status string `json:"status"`
  28. ExternalIP string `json:"floating_ip_address"`
  29. ExternalPort int `json:"external_service_port"`
  30. InternalIP string `json:"private_ip"`
  31. InternalPort int `json:"internal_service_port"`
  32. PortID string `json:"port_id"`
  33. AdminStateUp bool `json:"admin_state_up"`
  34. }
  35. func (nat *SNatDEntry) GetId() string {
  36. return nat.ID
  37. }
  38. func (nat *SNatDEntry) GetName() string {
  39. // No name so return id
  40. return nat.GetId()
  41. }
  42. func (nat *SNatDEntry) GetGlobalId() string {
  43. return nat.GetId()
  44. }
  45. func (nat *SNatDEntry) GetStatus() string {
  46. return NatResouceStatusTransfer(nat.Status)
  47. }
  48. func (nat *SNatDEntry) GetIpProtocol() string {
  49. return nat.Protocol
  50. }
  51. func (nat *SNatDEntry) GetExternalIp() string {
  52. return nat.ExternalIP
  53. }
  54. func (nat *SNatDEntry) GetExternalPort() int {
  55. return nat.ExternalPort
  56. }
  57. func (nat *SNatDEntry) GetInternalIp() string {
  58. return nat.InternalIP
  59. }
  60. func (nat *SNatDEntry) GetInternalPort() int {
  61. return nat.InternalPort
  62. }
  63. func (nat *SNatDEntry) Delete() error {
  64. return nat.gateway.region.DeleteNatDEntry(nat.GetId())
  65. }
  66. func (region *SRegion) GetNatDEntries(natId string) ([]SNatDEntry, error) {
  67. query := url.Values{}
  68. if len(natId) > 0 {
  69. query.Set("gateway_id", natId)
  70. }
  71. ret := []SNatDEntry{}
  72. for {
  73. resp, err := region.list(SERVICE_NAT, "private-nat/dnat-rules", query)
  74. if err != nil {
  75. return nil, err
  76. }
  77. part := struct {
  78. DnatRules []SNatDEntry
  79. PageInfo sPageInfo
  80. }{}
  81. err = resp.Unmarshal(&part)
  82. if err != nil {
  83. return nil, err
  84. }
  85. ret = append(ret, part.DnatRules...)
  86. if len(part.PageInfo.NextMarker) == 0 || len(part.DnatRules) == 0 {
  87. break
  88. }
  89. query.Set("marker", part.PageInfo.NextMarker)
  90. }
  91. return ret, nil
  92. }
  93. func (region *SRegion) DeleteNatDEntry(id string) error {
  94. _, err := region.delete(SERVICE_NAT, "private-nat/dnat-rules/"+id)
  95. return err
  96. }
  97. func (nat *SNatDEntry) Refresh() error {
  98. ret, err := nat.gateway.region.GetNatDEntryByID(nat.ID)
  99. if err != nil {
  100. return err
  101. }
  102. return jsonutils.Update(nat, ret)
  103. }