yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/natdtable.go (about)

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