yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/networkinterface.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  	"time"
    19  
    20  	"yunion.io/x/pkg/errors"
    21  
    22  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    23  )
    24  
    25  type SGroupSet struct {
    26  	GroupId   string `xml:"groupId"`
    27  	GroupName string `xml:"groupName"`
    28  }
    29  
    30  type SPrivateIpAddress struct {
    31  	PrivateIpAddress string `xml:"privateIpAddress"`
    32  	PrivateDnsName   string `xml:"privateDnsName"`
    33  	Primary          bool   `xml:"primary"`
    34  }
    35  
    36  type SAttachment struct {
    37  	AttachmentId        string    `xml:"attachmentId"`
    38  	InstanceOwnerId     string    `xml:"instanceOwnerId"`
    39  	DeviceIndex         int       `xml:"deviceIndex"`
    40  	Status              string    `xml:"status"`
    41  	AttachTime          time.Time `xml:"attachTime"`
    42  	DeleteOnTermination bool      `xml:"deleteOnTermination"`
    43  }
    44  
    45  type SNetworkInterface struct {
    46  	NetworkInterfaceId    string              `xml:"networkInterfaceId"`
    47  	SubnetId              string              `xml:"subnetId"`
    48  	VpcId                 string              `xml:"vpcId"`
    49  	AvailabilityZone      string              `xml:"availabilityZone"`
    50  	Description           string              `xml:"description"`
    51  	OwnerId               string              `xml:"ownerId"`
    52  	RequesterId           string              `xml:"requesterId"`
    53  	RequesterManaged      bool                `xml:"requesterManaged"`
    54  	Status                string              `xml:"status"`
    55  	MacAddress            string              `xml:"macAddress"`
    56  	PrivateIpAddress      string              `xml:"privateIpAddress"`
    57  	PrivateDnsName        string              `xml:"privateDnsName"`
    58  	SourceDestCheck       bool                `xml:"sourceDestCheck"`
    59  	GroupSet              []SGroupSet         `xml:"groupSet>item"`
    60  	Attachment            SAttachment         `xml:"attachment"`
    61  	PrivateIpAddressesSet []SPrivateIpAddress `xml:"privateIpAddressesSet>item"`
    62  	InterfaceType         string              `xml:"interfaceType"`
    63  }
    64  
    65  func (self *SRegion) GetNetworkInterface(id string) (*SNetworkInterface, error) {
    66  	nets, err := self.GetNetworkInterfaces(id)
    67  	if err != nil {
    68  		return nil, errors.Wrapf(err, "GetNetworkInterface")
    69  	}
    70  	for i := range nets {
    71  		if nets[i].NetworkInterfaceId == id {
    72  			return &nets[i], nil
    73  		}
    74  	}
    75  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
    76  }
    77  
    78  func (self *SRegion) GetNetworkInterfaces(id string) ([]SNetworkInterface, error) {
    79  	params := map[string]string{}
    80  	if len(id) > 0 {
    81  		params["NetworkInterfaceId.1"] = id
    82  	}
    83  	ret := []SNetworkInterface{}
    84  	for {
    85  		result := struct {
    86  			NetworkInterfaceSet []SNetworkInterface `xml:"networkInterfaceSet>item"`
    87  			NextToken           string              `xml:"nextToken"`
    88  		}{}
    89  		err := self.ec2Request("DescribeNetworkInterfaces", params, &result)
    90  		if err != nil {
    91  			return nil, errors.Wrap(err, "DescribeNetworkInterfaces")
    92  		}
    93  		ret = append(ret, result.NetworkInterfaceSet...)
    94  		if len(result.NextToken) == 0 || len(result.NetworkInterfaceSet) == 0 {
    95  			break
    96  		}
    97  		params["NextToken"] = result.NextToken
    98  	}
    99  	return ret, nil
   100  }