yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/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 hcs
    16  
    17  import (
    18  	"net/url"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/cloudmux/pkg/multicloud"
    25  	"yunion.io/x/cloudmux/pkg/multicloud/huawei"
    26  )
    27  
    28  type SNatSEntry struct {
    29  	multicloud.SResourceBase
    30  	huawei.HuaweiTags
    31  	gateway *SNatGateway
    32  
    33  	Id           string `json:"id"`
    34  	NatGatewayId string `json:"nat_gateway_id"`
    35  	NetworkId    string `json:"network_id"`
    36  	SourceCIDR   string `json:"cidr"`
    37  	Status       string `json:"status"`
    38  	SNatIP       string `json:"floating_ip_address"`
    39  	AdminStateUp bool   `json:"admin_state_up"`
    40  }
    41  
    42  func (nat *SNatSEntry) GetId() string {
    43  	return nat.Id
    44  }
    45  
    46  func (nat *SNatSEntry) GetName() string {
    47  	// Snat rule has no name in Huawei Cloud, so return Id
    48  	return nat.GetId()
    49  }
    50  
    51  func (nat *SNatSEntry) GetGlobalId() string {
    52  	return nat.GetId()
    53  }
    54  
    55  func (nat *SNatSEntry) GetStatus() string {
    56  	return NatResouceStatusTransfer(nat.Status)
    57  }
    58  
    59  func (nat *SNatSEntry) GetIP() string {
    60  	return nat.SNatIP
    61  }
    62  
    63  func (nat *SNatSEntry) GetSourceCIDR() string {
    64  	return nat.SourceCIDR
    65  }
    66  
    67  func (nat *SNatSEntry) GetNetworkId() string {
    68  	return nat.NetworkId
    69  }
    70  
    71  func (nat *SNatSEntry) Delete() error {
    72  	return nat.gateway.region.DeleteNatSEntry(nat.GetId())
    73  }
    74  
    75  // getNatSTable return all snat rules of gateway
    76  func (gateway *SNatGateway) getNatSTable() ([]SNatSEntry, error) {
    77  	ret, err := gateway.region.GetNatSTable(gateway.GetId())
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	for i := range ret {
    82  		ret[i].gateway = gateway
    83  	}
    84  	return ret, nil
    85  }
    86  
    87  func (self *SRegion) GetNatSTable(natGatewayId string) ([]SNatSEntry, error) {
    88  	query := url.Values{}
    89  	query.Set("nat_gateway_id", natGatewayId)
    90  	ret := []SNatSEntry{}
    91  	err := self.list("nat", "v2.0", "snat_rules", query, &ret)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	for i := range ret {
    96  		nat := &ret[i]
    97  		if len(nat.SourceCIDR) != 0 {
    98  			continue
    99  		}
   100  		net, err := self.GetNetwork(nat.NetworkId)
   101  		if err != nil {
   102  			return nil, errors.Wrapf(err, "GetNetwork(%s)", nat.NetworkId)
   103  		}
   104  		nat.SourceCIDR = net.CIDR
   105  	}
   106  	return ret, nil
   107  }
   108  
   109  func (self *SRegion) DeleteNatSEntry(entryId string) error {
   110  	return self.delete("nat", "v2.0", "snat_rules/"+entryId)
   111  }
   112  
   113  func (nat *SNatSEntry) Refresh() error {
   114  	ret, err := nat.gateway.region.GetNatSEntry(nat.Id)
   115  	if err != nil {
   116  		return err
   117  	}
   118  	return jsonutils.Update(nat, ret)
   119  }
   120  
   121  func (self *SRegion) CreateNatSEntry(rule cloudprovider.SNatSRule, gatewayId string) (*SNatSEntry, error) {
   122  	params := make(map[string]interface{})
   123  	params["nat_gateway_id"] = gatewayId
   124  	if len(rule.NetworkID) != 0 {
   125  		params["network_id"] = rule.NetworkID
   126  	}
   127  	if len(rule.SourceCIDR) != 0 {
   128  		params["cidr"] = rule.SourceCIDR
   129  	}
   130  	params["floating_ip_id"] = rule.ExternalIPID
   131  
   132  	packParams := map[string]interface{}{
   133  		"snat_rule": params,
   134  	}
   135  
   136  	ret := &SNatSEntry{}
   137  	return ret, self.create("nat", "v2.0", "snat_rules", packParams, ret)
   138  }
   139  
   140  func (self *SRegion) GetNatSEntry(id string) (*SNatSEntry, error) {
   141  	ret := &SNatSEntry{}
   142  	return ret, self.get("nat", "v2.0", "snat_rules/"+id, ret)
   143  }