yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/natgateway.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 aws
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"time"
    21  
    22  	"yunion.io/x/jsonutils"
    23  	"yunion.io/x/pkg/errors"
    24  
    25  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    26  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    27  	"yunion.io/x/cloudmux/pkg/multicloud"
    28  )
    29  
    30  type NatGatewayAddress struct {
    31  	AllocationId       string `xml:"allocationId"`
    32  	NetworkInterfaceId string `xml:"networkInterfaceId"`
    33  	PrivateIp          string `xml:"privateIp"`
    34  	PublicIp           string `xml:"publicIp"`
    35  }
    36  
    37  type ProvisionedBandwidth struct {
    38  	ProvisionTime time.Time `xml:"provisionTime"`
    39  	Provisioned   string    `xml:"provisioned"`
    40  	RequestTime   time.Time `xml:"requestTime"`
    41  	Requested     string    `xml:"requested"`
    42  	Status        string    `xml:"status"`
    43  }
    44  
    45  type SNatGateway struct {
    46  	multicloud.SNatGatewayBase
    47  	AwsTags
    48  
    49  	region *SRegion
    50  
    51  	ConnectivityType     string               `xml:"connectivityType"`
    52  	CreateTime           time.Time            `xml:"createTime"`
    53  	DeleteTime           time.Time            `xml:"deleteTime"`
    54  	FailureCode          string               `xml:"failureCode"`
    55  	FailureMessage       string               `xml:"failureMessage"`
    56  	NatGatewayAddresses  []NatGatewayAddress  `xml:"natGatewayAddressSet>item"`
    57  	NatGatewayId         string               `xml:"natGatewayId"`
    58  	ProvisionedBandwidth ProvisionedBandwidth `xml:"provisionedBandwidth"`
    59  	// pending | failed | available | deleting | deleted
    60  	State    string `xml:"state"`
    61  	SubnetId string `xml:"subnetId"`
    62  	VpcId    string `xml:"vpcId"`
    63  }
    64  
    65  func (self *SNatGateway) GetName() string {
    66  	name := self.AwsTags.GetName()
    67  	if len(name) > 0 {
    68  		return name
    69  	}
    70  	return self.NatGatewayId
    71  }
    72  
    73  func (self *SNatGateway) GetId() string {
    74  	return self.NatGatewayId
    75  }
    76  
    77  func (self *SNatGateway) GetGlobalId() string {
    78  	return self.NatGatewayId
    79  }
    80  
    81  func (self *SNatGateway) GetStatus() string {
    82  	switch self.State {
    83  	case "pending":
    84  		return api.NAT_STATUS_ALLOCATE
    85  	case "failed":
    86  		return api.NAT_STATUS_CREATE_FAILED
    87  	case "available":
    88  		return api.NAT_STAUTS_AVAILABLE
    89  	case "deleting", "deleted":
    90  		return api.NAT_STATUS_DELETING
    91  	default:
    92  		return api.NAT_STATUS_UNKNOWN
    93  	}
    94  }
    95  
    96  func (self *SNatGateway) GetNatSpec() string {
    97  	return ""
    98  }
    99  
   100  func (self *SNatGateway) Refresh() error {
   101  	nat, err := self.region.GetNatGateway(self.NatGatewayId)
   102  	if err != nil {
   103  		return err
   104  	}
   105  	return jsonutils.Update(self, nat)
   106  }
   107  
   108  func (self *SNatGateway) GetIEips() ([]cloudprovider.ICloudEIP, error) {
   109  	eips, err := self.region.GetEips("", "", self.NatGatewayId)
   110  	if err != nil {
   111  		return nil, errors.Wrapf(err, "GetEIPs")
   112  	}
   113  	ret := []cloudprovider.ICloudEIP{}
   114  	for i := range eips {
   115  		eips[i].region = self.region
   116  		ret = append(ret, &eips[i])
   117  	}
   118  	return ret, nil
   119  }
   120  
   121  func (self *SNatGateway) GetINatDTable() ([]cloudprovider.ICloudNatDEntry, error) {
   122  	return []cloudprovider.ICloudNatDEntry{}, nil
   123  }
   124  
   125  func (self *SNatGateway) GetINatSTable() ([]cloudprovider.ICloudNatSEntry, error) {
   126  	return []cloudprovider.ICloudNatSEntry{}, nil
   127  }
   128  
   129  func (self *SNatGateway) GetINatDEntryByID(id string) (cloudprovider.ICloudNatDEntry, error) {
   130  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
   131  }
   132  
   133  func (self *SNatGateway) GetINatSEntryByID(id string) (cloudprovider.ICloudNatSEntry, error) {
   134  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
   135  }
   136  
   137  func (self *SNatGateway) CreateINatDEntry(rule cloudprovider.SNatDRule) (cloudprovider.ICloudNatDEntry, error) {
   138  	return nil, cloudprovider.ErrNotImplemented
   139  }
   140  
   141  func (self *SNatGateway) CreateINatSEntry(rule cloudprovider.SNatSRule) (cloudprovider.ICloudNatSEntry, error) {
   142  	return nil, cloudprovider.ErrNotImplemented
   143  }
   144  
   145  func (self *SNatGateway) GetINetworkId() string {
   146  	return self.SubnetId
   147  }
   148  
   149  func (self *SNatGateway) GetBandwidthMb() int {
   150  	return 0
   151  }
   152  
   153  func (self *SNatGateway) GetIpAddr() string {
   154  	ipAddrs := []string{}
   155  	for _, addr := range self.NatGatewayAddresses {
   156  		if len(addr.PrivateIp) > 0 {
   157  			ipAddrs = append(ipAddrs, addr.PrivateIp)
   158  		}
   159  	}
   160  	return strings.Join(ipAddrs, ",")
   161  }
   162  
   163  func (self *SNatGateway) Delete() error {
   164  	return self.region.DeleteNatgateway(self.NatGatewayId)
   165  }
   166  
   167  func (self *SRegion) DeleteNatgateway(id string) error {
   168  	params := map[string]string{
   169  		"NatGatewayId": id,
   170  	}
   171  	return self.ec2Request("DeleteNatGateway", params, nil)
   172  }
   173  
   174  func (self *SRegion) GetNatGateways(ids []string, vpcId, subnetId string) ([]SNatGateway, error) {
   175  	params := map[string]string{}
   176  	for i, id := range ids {
   177  		params[fmt.Sprintf("NatGatewayId.%d", i+1)] = id
   178  	}
   179  	idx := 1
   180  	if len(vpcId) > 0 {
   181  		params[fmt.Sprintf("Filter.%d.Name", idx)] = "vpc-id"
   182  		params[fmt.Sprintf("Filter.%d.Value.1", idx)] = vpcId
   183  		idx++
   184  	}
   185  	if len(subnetId) > 0 {
   186  		params[fmt.Sprintf("Filter.%d.Name", idx)] = "subnet-id"
   187  		params[fmt.Sprintf("Filter.%d.Value.1", idx)] = subnetId
   188  		idx++
   189  	}
   190  	params[fmt.Sprintf("Filter.%d.Name", idx)] = "state"
   191  	for i, state := range []string{
   192  		"pending",
   193  		"failed",
   194  		"available",
   195  		"deleting",
   196  	} {
   197  		params[fmt.Sprintf("Filter.%d.Value.%d", idx, i+1)] = state
   198  	}
   199  	idx++
   200  	ret := []SNatGateway{}
   201  	for {
   202  		result := struct {
   203  			Nats      []SNatGateway `xml:"natGatewaySet>item"`
   204  			NextToken string        `xml:"nextToken"`
   205  		}{}
   206  		err := self.ec2Request("DescribeNatGateways", params, &result)
   207  		if err != nil {
   208  			return nil, errors.Wrapf(err, "DescribeNatGateways")
   209  		}
   210  		ret = append(ret, result.Nats...)
   211  		if len(result.NextToken) == 0 || len(result.Nats) == 0 {
   212  			break
   213  		}
   214  		params["NextToken"] = result.NextToken
   215  	}
   216  	return ret, nil
   217  }
   218  
   219  func (self *SRegion) GetNatGateway(id string) (*SNatGateway, error) {
   220  	nats, err := self.GetNatGateways([]string{id}, "", "")
   221  	if err != nil {
   222  		return nil, errors.Wrapf(err, "GetNatGateways")
   223  	}
   224  	for i := range nats {
   225  		if nats[i].GetGlobalId() == id {
   226  			nats[i].region = self
   227  			return &nats[i], nil
   228  		}
   229  	}
   230  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
   231  }
   232  
   233  func (self *SVpc) GetINatGateways() ([]cloudprovider.ICloudNatGateway, error) {
   234  	nats, err := self.region.GetNatGateways(nil, self.VpcId, "")
   235  	if err != nil {
   236  		return nil, errors.Wrapf(err, "GetINatGateways")
   237  	}
   238  	ret := []cloudprovider.ICloudNatGateway{}
   239  	for i := range nats {
   240  		nats[i].region = self.region
   241  		ret = append(ret, &nats[i])
   242  	}
   243  	return ret, nil
   244  }