yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/cloudpods/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 cloudpods
    16  
    17  import (
    18  	"time"
    19  
    20  	"yunion.io/x/jsonutils"
    21  
    22  	api "yunion.io/x/onecloud/pkg/apis/compute"
    23  	modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
    24  
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  	"yunion.io/x/cloudmux/pkg/multicloud"
    27  )
    28  
    29  type SEip struct {
    30  	multicloud.SVirtualResourceBase
    31  	multicloud.SBillingBase
    32  	CloudpodsTags
    33  	region *SRegion
    34  
    35  	api.ElasticipDetails
    36  }
    37  
    38  func (self *SEip) GetName() string {
    39  	return self.Name
    40  }
    41  
    42  func (self *SEip) GetId() string {
    43  	return self.Id
    44  }
    45  
    46  func (self *SEip) GetGlobalId() string {
    47  	return self.Id
    48  }
    49  
    50  func (self *SEip) GetProjectId() string {
    51  	return self.TenantId
    52  }
    53  
    54  func (self *SEip) GetStatus() string {
    55  	return self.Status
    56  }
    57  
    58  func (self *SEip) Refresh() error {
    59  	eip, err := self.region.GetEip(self.Id)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	return jsonutils.Update(self, eip)
    64  }
    65  
    66  func (self *SEip) GetBillingType() string {
    67  	return self.BillingType
    68  }
    69  
    70  func (self *SEip) GetIpAddr() string {
    71  	return self.IpAddr
    72  }
    73  
    74  func (self *SEip) GetINetworkId() string {
    75  	return self.NetworkId
    76  }
    77  
    78  func (self *SEip) GetAssociationType() string {
    79  	return self.AssociateType
    80  }
    81  
    82  func (self *SEip) GetAssociationExternalId() string {
    83  	return self.AssociateId
    84  }
    85  
    86  func (self *SEip) GetBandwidth() int {
    87  	return self.Bandwidth
    88  }
    89  
    90  func (self *SEip) GetInternetChargeType() string {
    91  	return self.ChargeType
    92  }
    93  
    94  func (self *SEip) Delete() error {
    95  	return self.region.cli.delete(&modules.Elasticips, self.Id)
    96  }
    97  
    98  func (self *SEip) IsAutoRenew() bool {
    99  	return self.AutoRenew
   100  }
   101  
   102  func (self *SEip) Associate(opts *cloudprovider.AssociateConfig) error {
   103  	input := api.ElasticipAssociateInput{}
   104  	input.InstanceType = opts.AssociateType
   105  	input.InstanceId = opts.InstanceId
   106  	switch opts.AssociateType {
   107  	case api.EIP_ASSOCIATE_TYPE_SERVER:
   108  	default:
   109  		return cloudprovider.ErrNotImplemented
   110  	}
   111  	_, err := self.region.perform(&modules.Elasticips, self.Id, "associate", input)
   112  	return err
   113  }
   114  
   115  func (self *SEip) Dissociate() error {
   116  	_, err := self.region.perform(&modules.Elasticips, self.Id, "dissociate", nil)
   117  	return err
   118  }
   119  
   120  func (self *SEip) GetCreatedAt() time.Time {
   121  	return self.CreatedAt
   122  }
   123  
   124  func (self *SEip) GetExpiredAt() time.Time {
   125  	return self.ExpiredAt
   126  }
   127  
   128  func (self *SEip) GetMode() string {
   129  	return self.Mode
   130  }
   131  
   132  func (self *SEip) ChangeBandwidth(bw int) error {
   133  	return cloudprovider.ErrNotImplemented
   134  }
   135  
   136  func (self *SRegion) GetIEipById(id string) (cloudprovider.ICloudEIP, error) {
   137  	eip, err := self.GetEip(id)
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  	return eip, nil
   142  }
   143  
   144  func (self *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
   145  	eips, err := self.GetEips("")
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  	ret := []cloudprovider.ICloudEIP{}
   150  	for i := range eips {
   151  		eips[i].region = self
   152  		ret = append(ret, &eips[i])
   153  	}
   154  	return ret, nil
   155  }
   156  
   157  func (self *SRegion) CreateEIP(opts *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
   158  	input := api.SElasticipCreateInput{}
   159  	input.Name = opts.Name
   160  	input.CloudregionId = self.Id
   161  	input.ChargeType = opts.ChargeType
   162  	input.BandwidthMb = opts.BandwidthMbps
   163  	input.ProjectId = opts.ProjectId
   164  	input.NetworkId = opts.NetworkExternalId
   165  	input.BgpType = opts.BGPType
   166  	input.IpAddr = opts.IP
   167  	eip := &SEip{region: self}
   168  	return eip, self.create(&modules.Elasticips, input, eip)
   169  }
   170  
   171  func (self *SRegion) GetEips(associateId string) ([]SEip, error) {
   172  	eips := []SEip{}
   173  	params := map[string]interface{}{}
   174  	if len(associateId) > 0 {
   175  		params["associate_id"] = associateId
   176  	}
   177  	return eips, self.list(&modules.Elasticips, params, &eips)
   178  }
   179  
   180  func (self *SRegion) GetEip(id string) (*SEip, error) {
   181  	eip := &SEip{region: self}
   182  	return eip, self.cli.get(&modules.Elasticips, id, nil, eip)
   183  }