yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ucloud/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 ucloud
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"time"
    21  
    22  	"yunion.io/x/jsonutils"
    23  	"yunion.io/x/log"
    24  
    25  	billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
    26  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    27  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    28  	"yunion.io/x/cloudmux/pkg/multicloud"
    29  )
    30  
    31  const (
    32  	EIP_CHARGE_TYPE_BY_TRAFFIC   = "traffic"
    33  	EIP_CHARGE_TYPE_BY_BANDWIDTH = "bandwidth"
    34  )
    35  
    36  // https://docs.ucloud.cn/api/unet-api/describe_eip
    37  type SEip struct {
    38  	region *SRegion
    39  	multicloud.SEipBase
    40  	UcloudTags
    41  
    42  	BandwidthMb       int               `json:"Bandwidth"`
    43  	BandwidthType     int               `json:"BandwidthType"`
    44  	ChargeType        string            `json:"ChargeType"`
    45  	CreateTime        int64             `json:"CreateTime"`
    46  	EIPAddr           []EIPAddr         `json:"EIPAddr"`
    47  	EIPID             string            `json:"EIPId"`
    48  	Expire            bool              `json:"Expire"`
    49  	ExpireTime        int64             `json:"ExpireTime"`
    50  	Name              string            `json:"Name"`
    51  	PayMode           string            `json:"PayMode"`
    52  	Remark            string            `json:"Remark"`
    53  	Resource          Resource          `json:"Resource"`
    54  	ShareBandwidthSet ShareBandwidthSet `json:"ShareBandwidthSet"`
    55  	Status            string            `json:"Status"`
    56  	Tag               string            `json:"Tag"`
    57  	Weight            int               `json:"Weight"`
    58  }
    59  
    60  func (self *SEip) GetProjectId() string {
    61  	return self.region.client.projectId
    62  }
    63  
    64  type EIPAddr struct {
    65  	IP           string `json:"IP"`
    66  	OperatorName string `json:"OperatorName"`
    67  }
    68  
    69  type Resource struct {
    70  	ResourceID   string `json:"ResourceID"`
    71  	ResourceName string `json:"ResourceName"`
    72  	ResourceType string `json:"ResourceType"`
    73  	Zone         string `json:"Zone"`
    74  }
    75  
    76  type ShareBandwidthSet struct {
    77  	ShareBandwidth     int    `json:"ShareBandwidth"`
    78  	ShareBandwidthID   string `json:"ShareBandwidthId"`
    79  	ShareBandwidthName string `json:"ShareBandwidthName"`
    80  }
    81  
    82  func (self *SEip) GetId() string {
    83  	return self.EIPID
    84  }
    85  
    86  func (self *SEip) GetName() string {
    87  	if len(self.Name) == 0 {
    88  		return self.GetId()
    89  	}
    90  
    91  	return self.Name
    92  }
    93  
    94  func (self *SEip) GetGlobalId() string {
    95  	return self.GetId()
    96  }
    97  
    98  // 弹性IP的资源绑定状态, 枚举值为: used: 已绑定, free: 未绑定, freeze: 已冻结
    99  func (self *SEip) GetStatus() string {
   100  	switch self.Status {
   101  	case "used":
   102  		return api.EIP_STATUS_ASSOCIATE // ?
   103  	case "free":
   104  		return api.EIP_STATUS_READY
   105  	case "freeze":
   106  		return api.EIP_STATUS_UNKNOWN
   107  	default:
   108  		return api.EIP_STATUS_UNKNOWN
   109  	}
   110  }
   111  
   112  func (self *SEip) Refresh() error {
   113  	if self.IsEmulated() {
   114  		return nil
   115  	}
   116  	new, err := self.region.GetEipById(self.GetId())
   117  	if err != nil {
   118  		return err
   119  	}
   120  	return jsonutils.Update(self, new)
   121  }
   122  
   123  func (self *SEip) IsEmulated() bool {
   124  	return false
   125  }
   126  
   127  // 付费方式, 枚举值为: Year, 按年付费; Month, 按月付费; Dynamic, 按小时付费; Trial, 试用. 按小时付费和试用这两种付费模式需要开通权限.
   128  func (self *SEip) GetBillingType() string {
   129  	switch self.ChargeType {
   130  	case "Year", "Month":
   131  		return billing_api.BILLING_TYPE_PREPAID
   132  	default:
   133  		return billing_api.BILLING_TYPE_POSTPAID
   134  	}
   135  }
   136  
   137  func (self *SEip) GetCreatedAt() time.Time {
   138  	return time.Unix(self.CreateTime, 0)
   139  }
   140  
   141  func (self *SEip) GetExpiredAt() time.Time {
   142  	return time.Unix(self.ExpireTime, 0)
   143  }
   144  
   145  func (self *SEip) GetIpAddr() string {
   146  	if len(self.EIPAddr) > 1 {
   147  		log.Warningf("GetIpAddr %d eip addr found", len(self.EIPAddr))
   148  	} else if len(self.EIPAddr) == 0 {
   149  		return ""
   150  	}
   151  
   152  	return self.EIPAddr[0].IP
   153  }
   154  
   155  func (self *SEip) GetMode() string {
   156  	return api.EIP_MODE_STANDALONE_EIP
   157  }
   158  
   159  func (self *SEip) GetAssociationType() string {
   160  	switch self.Resource.ResourceType {
   161  	case "uhost":
   162  		return api.EIP_ASSOCIATE_TYPE_SERVER
   163  	case "natgw":
   164  		return api.EIP_ASSOCIATE_TYPE_NAT_GATEWAY
   165  	case "ulb":
   166  		return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
   167  	default:
   168  		return self.Resource.ResourceType
   169  	}
   170  }
   171  
   172  // 已绑定的资源类型, 枚举值为: uhost, 云主机;natgw:NAT网关;ulb:负载均衡器;upm: 物理机; hadoophost: 大数据集群;fortresshost:堡垒机;udockhost:容器;udhost:私有专区主机;vpngw:IPSec VPN;ucdr:云灾备;dbaudit:数据库审计。
   173  func (self *SEip) GetAssociationExternalId() string {
   174  	return self.Resource.ResourceID
   175  }
   176  
   177  func (self *SEip) GetBandwidth() int {
   178  	return self.BandwidthMb
   179  }
   180  
   181  func (self *SEip) GetINetworkId() string {
   182  	return ""
   183  }
   184  
   185  // 弹性IP的计费模式, 枚举值为: "Bandwidth", 带宽计费; "Traffic", 流量计费; "ShareBandwidth",共享带宽模式. 默认为 "Bandwidth".
   186  func (self *SEip) GetInternetChargeType() string {
   187  	switch self.PayMode {
   188  	case "Bandwidth":
   189  		return api.EIP_CHARGE_TYPE_BY_BANDWIDTH
   190  	case "Traffic":
   191  		return api.EIP_CHARGE_TYPE_BY_TRAFFIC
   192  	default:
   193  		return api.EIP_CHARGE_TYPE_BY_BANDWIDTH
   194  	}
   195  }
   196  
   197  func (self *SEip) Delete() error {
   198  	return self.region.DeallocateEIP(self.GetId())
   199  }
   200  
   201  func (self *SEip) Associate(conf *cloudprovider.AssociateConfig) error {
   202  	return self.region.AssociateEip(self.GetId(), conf.InstanceId)
   203  }
   204  
   205  func (self *SEip) Dissociate() error {
   206  	return self.region.DissociateEip(self.GetId(), self.Resource.ResourceID)
   207  }
   208  
   209  func (self *SEip) ChangeBandwidth(bw int) error {
   210  	return self.region.UpdateEipBandwidth(self.GetId(), bw)
   211  }
   212  
   213  // https://docs.ucloud.cn/api/unet-api/allocate_eip
   214  // 增加共享带宽模式ShareBandwidth
   215  func (self *SRegion) CreateEIP(eip *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
   216  	if len(eip.BGPType) == 0 {
   217  		if strings.HasPrefix(self.GetId(), "cn-") {
   218  			eip.BGPType = "Bgp"
   219  		} else {
   220  			eip.BGPType = "International"
   221  		}
   222  	}
   223  
   224  	params := NewUcloudParams()
   225  	params.Set("OperatorName", eip.BGPType)
   226  	params.Set("Bandwidth", eip.BandwidthMbps)
   227  	params.Set("Name", eip.Name)
   228  	var payMode string
   229  	switch eip.ChargeType {
   230  	case api.EIP_CHARGE_TYPE_BY_TRAFFIC:
   231  		payMode = "Traffic"
   232  	case api.EIP_CHARGE_TYPE_BY_BANDWIDTH:
   233  		payMode = "Bandwidth"
   234  	}
   235  	params.Set("PayMode", payMode)
   236  	params.Set("ChargeType", "Dynamic") // 按需付费
   237  
   238  	eips := make([]SEip, 0)
   239  	err := self.DoAction("AllocateEIP", params, &eips)
   240  	if err != nil {
   241  		return nil, err
   242  	}
   243  
   244  	if len(eips) == 1 {
   245  		eip := eips[0]
   246  		eip.region = self
   247  		eip.Refresh()
   248  		return &eip, nil
   249  	} else {
   250  		return nil, fmt.Errorf("CreateEIP %d eip created", len(eips))
   251  	}
   252  }
   253  
   254  // https://docs.ucloud.cn/api/unet-api/release_eip
   255  func (self *SRegion) DeallocateEIP(eipId string) error {
   256  	params := NewUcloudParams()
   257  	params.Set("EIPId", eipId)
   258  
   259  	return self.DoAction("ReleaseEIP", params, nil)
   260  }
   261  
   262  // https://docs.ucloud.cn/api/unet-api/bind_eip
   263  func (self *SRegion) AssociateEip(eipId string, instanceId string) error {
   264  	params := NewUcloudParams()
   265  	params.Set("EIPId", eipId)
   266  	params.Set("ResourceType", "uhost")
   267  	params.Set("ResourceId", instanceId)
   268  
   269  	return self.DoAction("BindEIP", params, nil)
   270  }
   271  
   272  // https://docs.ucloud.cn/api/unet-api/unbind_eip
   273  func (self *SRegion) DissociateEip(eipId string, instanceId string) error {
   274  	params := NewUcloudParams()
   275  	params.Set("EIPId", eipId)
   276  	params.Set("ResourceType", "uhost")
   277  	params.Set("ResourceId", instanceId)
   278  
   279  	return self.DoAction("UnBindEIP", params, nil)
   280  }
   281  
   282  // https://docs.ucloud.cn/api/unet-api/modify_eip_bandwidth
   283  func (self *SRegion) UpdateEipBandwidth(eipId string, bw int) error {
   284  	params := NewUcloudParams()
   285  	params.Set("EIPId", eipId)
   286  	params.Set("Bandwidth", bw)
   287  
   288  	return self.DoAction("ModifyEIPBandwidth", params, nil)
   289  }