yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/bingocloud/eip.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 bingocloud
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/pkg/errors"
    21  
    22  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/cloudmux/pkg/multicloud"
    25  )
    26  
    27  type SEip struct {
    28  	multicloud.SEipBase
    29  	BingoTags
    30  
    31  	region *SRegion
    32  
    33  	AddressId    string
    34  	AddressType  string
    35  	Bandtype     string
    36  	Bandwidth    int
    37  	CanAssociate bool
    38  	InstanceId   string
    39  	Owner        string
    40  	PublicIp     string
    41  	SubnetId     string
    42  	VpcId        string
    43  }
    44  
    45  func (self *SEip) GetId() string {
    46  	return self.PublicIp
    47  }
    48  
    49  func (self *SEip) GetGlobalId() string {
    50  	return self.PublicIp
    51  }
    52  
    53  func (self *SEip) GetName() string {
    54  	return self.PublicIp
    55  }
    56  
    57  func (self *SEip) GetIpAddr() string {
    58  	return self.PublicIp
    59  }
    60  
    61  func (self *SEip) GetMode() string {
    62  	return api.EIP_MODE_STANDALONE_EIP
    63  }
    64  
    65  func (self *SEip) GetINetworkId() string {
    66  	return self.SubnetId
    67  }
    68  
    69  func (self *SEip) GetAssociationType() string {
    70  	if len(self.InstanceId) > 0 {
    71  		return api.EIP_ASSOCIATE_TYPE_SERVER
    72  	}
    73  	return ""
    74  }
    75  
    76  func (self *SEip) GetAssociationExternalId() string {
    77  	return self.InstanceId
    78  }
    79  
    80  func (self *SEip) GetBandwidth() int {
    81  	return self.Bandwidth
    82  }
    83  
    84  func (self *SEip) GetInternetChargeType() string {
    85  	return api.EIP_CHARGE_TYPE_BY_BANDWIDTH
    86  }
    87  
    88  func (self *SEip) Delete() error {
    89  	return cloudprovider.ErrNotImplemented
    90  }
    91  
    92  func (self *SEip) Associate(conf *cloudprovider.AssociateConfig) error {
    93  	return cloudprovider.ErrNotImplemented
    94  }
    95  
    96  func (self *SEip) Dissociate() error {
    97  	return cloudprovider.ErrNotImplemented
    98  }
    99  
   100  func (self *SEip) ChangeBandwidth(bw int) error {
   101  	return cloudprovider.ErrNotImplemented
   102  }
   103  
   104  func (self *SEip) GetProjectId() string {
   105  	return ""
   106  }
   107  
   108  func (self *SEip) GetStatus() string {
   109  	return api.EIP_STATUS_READY
   110  }
   111  
   112  func (self *SRegion) GetEips(ip, instanceId, nextToken string) ([]SEip, string, error) {
   113  	params := map[string]string{}
   114  	if len(ip) > 0 {
   115  		params["publicIp"] = ip
   116  	}
   117  	if len(nextToken) > 0 {
   118  		params["nextToken"] = nextToken
   119  	}
   120  
   121  	idx := 1
   122  	if len(instanceId) > 0 {
   123  		params[fmt.Sprintf("Filter.%d.Name", idx)] = "instance-id"
   124  		params[fmt.Sprintf("Filter.%d.Value.1", idx)] = instanceId
   125  		idx++
   126  	}
   127  
   128  	resp, err := self.invoke("DescribeAddresses", params)
   129  	if err != nil {
   130  		return nil, "", errors.Wrapf(err, "DescribeAddresses")
   131  	}
   132  	ret := struct {
   133  		AddressesSet []SEip
   134  		NextToken    string
   135  	}{}
   136  	resp.Unmarshal(&ret)
   137  	return ret.AddressesSet, ret.NextToken, nil
   138  }
   139  
   140  func (self *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
   141  	part, nextToken, err := self.GetEips("", "", "")
   142  	if err != nil {
   143  		return nil, err
   144  	}
   145  	eips := []SEip{}
   146  	eips = append(eips, part...)
   147  	for len(nextToken) > 0 {
   148  		part, nextToken, err = self.GetEips("", "", nextToken)
   149  		if err != nil {
   150  			return nil, err
   151  		}
   152  		eips = append(eips, part...)
   153  	}
   154  	ret := []cloudprovider.ICloudEIP{}
   155  	for i := range eips {
   156  		eips[i].region = self
   157  		ret = append(ret, &eips[i])
   158  	}
   159  	return ret, nil
   160  }
   161  
   162  func (self *SRegion) GetIEipById(id string) (cloudprovider.ICloudEIP, error) {
   163  	eips, _, err := self.GetEips(id, "", "")
   164  	if err != nil {
   165  		return nil, err
   166  	}
   167  	for i := range eips {
   168  		if eips[i].GetGlobalId() == id {
   169  			eips[i].region = self
   170  			return &eips[i], nil
   171  		}
   172  	}
   173  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, id)
   174  }