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