natstable.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 SNatSEntry struct {
  21. multicloud.SResourceBase
  22. HuaweiTags
  23. gateway *SNatGateway
  24. ID string `json:"id"`
  25. NatGatewayID string `json:"nat_gateway_id"`
  26. NetworkID string `json:"network_id"`
  27. SourceCIDR string `json:"cidr"`
  28. Status string `json:"status"`
  29. SNatIP string `json:"floating_ip_address"`
  30. AdminStateUp bool `json:"admin_state_up"`
  31. }
  32. func (nat *SNatSEntry) GetId() string {
  33. return nat.ID
  34. }
  35. func (nat *SNatSEntry) GetName() string {
  36. return nat.GetId()
  37. }
  38. func (nat *SNatSEntry) GetGlobalId() string {
  39. return nat.GetId()
  40. }
  41. func (nat *SNatSEntry) GetStatus() string {
  42. return NatResouceStatusTransfer(nat.Status)
  43. }
  44. func (nat *SNatSEntry) GetIP() string {
  45. return nat.SNatIP
  46. }
  47. func (nat *SNatSEntry) GetSourceCIDR() string {
  48. return nat.SourceCIDR
  49. }
  50. func (nat *SNatSEntry) GetNetworkId() string {
  51. return nat.NetworkID
  52. }
  53. func (nat *SNatSEntry) Delete() error {
  54. return nat.gateway.region.DeleteNatSEntry(nat.GetId())
  55. }
  56. func (region *SRegion) GetNatSEntries(natId string) ([]SNatSEntry, error) {
  57. query := url.Values{}
  58. if len(natId) > 0 {
  59. query.Set("gateway_id", natId)
  60. }
  61. ret := []SNatSEntry{}
  62. for {
  63. resp, err := region.list(SERVICE_NAT, "private-nat/snat-rules", query)
  64. if err != nil {
  65. return nil, err
  66. }
  67. part := struct {
  68. SnatRules []SNatSEntry
  69. PageInfo sPageInfo
  70. }{}
  71. err = resp.Unmarshal(&part)
  72. if err != nil {
  73. return nil, err
  74. }
  75. ret = append(ret, part.SnatRules...)
  76. if len(part.PageInfo.NextMarker) == 0 || len(part.SnatRules) == 0 {
  77. break
  78. }
  79. query.Set("marker", part.PageInfo.NextMarker)
  80. }
  81. return ret, nil
  82. }
  83. func (region *SRegion) DeleteNatSEntry(id string) error {
  84. _, err := region.delete(SERVICE_NAT, "private-nat/snat-rules/"+id)
  85. return err
  86. }
  87. func (nat *SNatSEntry) Refresh() error {
  88. ret, err := nat.gateway.region.GetNatSEntryByID(nat.ID)
  89. if err != nil {
  90. return err
  91. }
  92. return jsonutils.Update(nat, ret)
  93. }