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