mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge pull request #161 in YUNIONIO/onecloud from ~QIUJIAN/onecloud:feature/qj-elastic-ip to release/2.1.0
* commit '82bbb2054b9d72b542ea5d4d3aea931fe1103f26': fix log 修正:1. 已经绑定的EIP禁止删除 2. 删除server时候清除public IP记录 浮动IP支持 temp commit
This commit is contained in:
@@ -0,0 +1,296 @@
|
||||
package aliyun
|
||||
|
||||
import (
|
||||
"time"
|
||||
"yunion.io/x/log"
|
||||
"fmt"
|
||||
|
||||
"yunion.io/x/pkg/utils"
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/cloudprovider"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
)
|
||||
|
||||
type TInternetChargeType string
|
||||
|
||||
const (
|
||||
InternetChargeByTraffic = TInternetChargeType("PayByTraffic")
|
||||
InternetChargeByBandwidth = TInternetChargeType("PayByBandwidth")
|
||||
)
|
||||
|
||||
const (
|
||||
EIP_STATUS_ASSOCIATING = "Associating"
|
||||
EIP_STATUS_UNASSOCIATING = "Unassociating"
|
||||
EIP_STATUS_INUSE = "InUse"
|
||||
EIP_STATUS_AVAILABLE = "Available"
|
||||
|
||||
EIP_OPERATION_LOCK_FINANCIAL = "financial"
|
||||
EIP_OPERATION_LOCK_SECURITY = "security"
|
||||
|
||||
|
||||
EIP_INSTANCE_TYPE_ECS = "EcsInstance" // (默认值):VPC类型的ECS实例
|
||||
EIP_INTANNCE_TYPE_SLB = "SlbInstance" // :VPC类型的SLB实例
|
||||
EIP_INSTANCE_TYPE_NAT = "Nat" // :NAT网关
|
||||
EIP_INSTANCE_TYPE_HAVIP = "HaVip" // :HAVIP
|
||||
)
|
||||
|
||||
type SEipAddress struct {
|
||||
region *SRegion
|
||||
|
||||
AllocationId string
|
||||
|
||||
InternetChargeType string
|
||||
|
||||
IpAddress string
|
||||
Status string
|
||||
|
||||
InstanceType string
|
||||
InstanceId string
|
||||
Bandwidth int /* Mbps */
|
||||
|
||||
AllocationTime time.Time
|
||||
|
||||
OperationLocks string
|
||||
}
|
||||
|
||||
func (self *SEipAddress) GetId() string {
|
||||
return self.AllocationId
|
||||
}
|
||||
|
||||
func (self *SEipAddress) GetName() string {
|
||||
return self.IpAddress
|
||||
}
|
||||
|
||||
func (self *SEipAddress) GetGlobalId() string {
|
||||
return self.AllocationId
|
||||
}
|
||||
|
||||
func (self *SEipAddress) GetStatus() string {
|
||||
switch self.Status {
|
||||
case EIP_STATUS_AVAILABLE, EIP_STATUS_INUSE:
|
||||
return models.EIP_STATUS_READY
|
||||
case EIP_STATUS_ASSOCIATING:
|
||||
return models.EIP_STATUS_ASSOCIATE
|
||||
case EIP_STATUS_UNASSOCIATING:
|
||||
return models.EIP_STATUS_DISSOCIATE
|
||||
default:
|
||||
return models.EIP_STATUS_UNKNOWN
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SEipAddress) Refresh() error {
|
||||
if self.IsEmulated() {
|
||||
return nil
|
||||
}
|
||||
new, err := self.region.GetEip(self.AllocationId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return jsonutils.Update(self, new)
|
||||
}
|
||||
|
||||
func (self *SEipAddress) IsEmulated() bool {
|
||||
if self.AllocationId == self.InstanceId {
|
||||
// fixed Public IP
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SEipAddress) GetMetadata() *jsonutils.JSONDict {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SEipAddress) GetIpAddr() string {
|
||||
return self.IpAddress
|
||||
}
|
||||
|
||||
func (self *SEipAddress) GetMode() string {
|
||||
if self.InstanceId == self.AllocationId {
|
||||
return models.EIP_MODE_INSTANCE_PUBLICIP
|
||||
} else {
|
||||
return models.EIP_MODE_STANDALONE_EIP
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SEipAddress) GetAssociationType() string {
|
||||
switch self.InstanceType {
|
||||
case EIP_INSTANCE_TYPE_ECS:
|
||||
return "server"
|
||||
default:
|
||||
log.Fatalf("unsupported type: %s", self.InstanceType)
|
||||
return "unsupported"
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SEipAddress) GetAssociationExternalId() string {
|
||||
return self.InstanceId
|
||||
}
|
||||
|
||||
func (self *SEipAddress) GetManagerId() string {
|
||||
return self.region.client.providerId
|
||||
}
|
||||
|
||||
func (self *SEipAddress) Delete() error {
|
||||
return self.region.DeallocateEIP(self.AllocationId)
|
||||
}
|
||||
|
||||
func (self *SEipAddress) GetBandwidth() int {
|
||||
return self.Bandwidth
|
||||
}
|
||||
|
||||
func (self *SEipAddress) GetInternetChargeType() string {
|
||||
switch self.InternetChargeType {
|
||||
case string(InternetChargeByTraffic):
|
||||
return models.EIP_CHARGE_TYPE_BY_TRAFFIC
|
||||
case string(InternetChargeByBandwidth):
|
||||
return models.EIP_CHARGE_TYPE_BY_BANDWIDTH
|
||||
default:
|
||||
return models.EIP_CHARGE_TYPE_BY_TRAFFIC
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SEipAddress) Associate(instanceId string) error {
|
||||
err := self.region.AssociateEip(self.AllocationId, instanceId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = cloudprovider.WaitStatus(self, models.EIP_STATUS_READY, 10*time.Second, 180*time.Second)
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *SEipAddress) Dissociate() error {
|
||||
err := self.region.DissociateEip(self.AllocationId, self.InstanceId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = cloudprovider.WaitStatus(self, models.EIP_STATUS_READY, 10*time.Second, 180*time.Second)
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *SEipAddress) ChangeBandwidth(bw int) error {
|
||||
return self.region.UpdateEipBandwidth(self.AllocationId, bw)
|
||||
}
|
||||
|
||||
func (region *SRegion) GetEips(eipId string, offset int, limit int) ([]SEipAddress, int, error) {
|
||||
if limit > 50 || limit <= 0 {
|
||||
limit = 50
|
||||
}
|
||||
|
||||
params := make(map[string]string)
|
||||
params["RegionId"] = region.RegionId
|
||||
params["PageSize"] = fmt.Sprintf("%d", limit)
|
||||
params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1)
|
||||
|
||||
if len(eipId) > 0 {
|
||||
params["AllocationId"] = eipId
|
||||
}
|
||||
|
||||
body, err := region.ecsRequest("DescribeEipAddresses", params)
|
||||
if err != nil {
|
||||
log.Errorf("DescribeEipAddresses fail %s", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// log.Errorf("%s", body)
|
||||
|
||||
eips := make([]SEipAddress, 0)
|
||||
err = body.Unmarshal(&eips, "EipAddresses", "EipAddress")
|
||||
if err != nil {
|
||||
log.Errorf("Unmarshal EipAddress details fail %s", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
total, _ := body.Int("TotalCount")
|
||||
for i := 0; i < len(eips); i += 1 {
|
||||
eips[i].region = region
|
||||
}
|
||||
return eips, int(total), nil
|
||||
}
|
||||
|
||||
func (region *SRegion) GetEip(eipId string) (*SEipAddress, error) {
|
||||
eips, total, err := region.GetEips(eipId, 0, 1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if total != 1 {
|
||||
return nil, cloudprovider.ErrNotFound
|
||||
}
|
||||
return &eips[0], nil
|
||||
}
|
||||
|
||||
func (region *SRegion) AllocateEIP(bwMbps int, chargeType TInternetChargeType) (*SEipAddress, error) {
|
||||
params := make(map[string]string)
|
||||
|
||||
params["Bandwidth"] = fmt.Sprintf("%d", bwMbps)
|
||||
params["InternetChargeType"] = string(chargeType)
|
||||
params["InstanceChargeType"] = "PostPaid"
|
||||
params["ClientToken"] = utils.GenRequestId(20)
|
||||
|
||||
body, err := region.ecsRequest("AllocateEipAddress", params)
|
||||
if err != nil {
|
||||
log.Errorf("AllocateEipAddress fail %s", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
eipId, err := body.GetString("AllocationId")
|
||||
if err != nil {
|
||||
log.Errorf("fail to get AllocationId after EIP allocation??? %s", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return region.GetEip(eipId)
|
||||
}
|
||||
|
||||
func (region *SRegion) CreateEIP(bwMbps int) (cloudprovider.ICloudEIP, error) {
|
||||
eip, err := region.AllocateEIP(bwMbps, InternetChargeByTraffic)
|
||||
return eip, err
|
||||
}
|
||||
|
||||
func (region *SRegion) DeallocateEIP(eipId string) (error) {
|
||||
params := make(map[string]string)
|
||||
params["AllocationId"] = eipId
|
||||
|
||||
_, err := region.ecsRequest("ReleaseEipAddress", params)
|
||||
if err != nil {
|
||||
log.Errorf("ReleaseEipAddress fail %s", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (region *SRegion) AssociateEip(eipId string, instanceId string) (error) {
|
||||
params := make(map[string]string)
|
||||
params["AllocationId"] = eipId
|
||||
params["InstanceId"] = instanceId
|
||||
|
||||
_, err := region.ecsRequest("AssociateEipAddress", params)
|
||||
if err != nil {
|
||||
log.Errorf("AssociateEipAddress fail %s", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (region *SRegion) DissociateEip(eipId string, instanceId string) (error) {
|
||||
params := make(map[string]string)
|
||||
params["AllocationId"] = eipId
|
||||
params["InstanceId"] = instanceId
|
||||
|
||||
_, err := region.ecsRequest("UnassociateEipAddress", params)
|
||||
if err != nil {
|
||||
log.Errorf("UnassociateEipAddress fail %s", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (region *SRegion) UpdateEipBandwidth(eipId string, bw int) error {
|
||||
params := make(map[string]string)
|
||||
params["AllocationId"] = eipId
|
||||
params["Bandwidth"] = fmt.Sprintf("%d", bw)
|
||||
|
||||
_, err := region.ecsRequest("ModifyEipAddressAttribute", params)
|
||||
if err != nil {
|
||||
log.Errorf("ModifyEipAddressAttribute fail %s", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
+20
-22
@@ -34,24 +34,6 @@ type SDedicatedHostAttribute struct {
|
||||
DedicatedHostName string
|
||||
}
|
||||
|
||||
type SEipAddress struct {
|
||||
AllocationId string
|
||||
InternetChargeType string
|
||||
IpAddress string
|
||||
}
|
||||
|
||||
func (self *SEipAddress) GetIP() string {
|
||||
return self.IpAddress
|
||||
}
|
||||
|
||||
func (self *SEipAddress) GetAllocationId() string {
|
||||
return self.AllocationId
|
||||
}
|
||||
|
||||
func (self *SEipAddress) GetChargeType() string {
|
||||
return self.GetChargeType()
|
||||
}
|
||||
|
||||
type SIpAddress struct {
|
||||
IpAddress []string
|
||||
}
|
||||
@@ -247,10 +229,6 @@ func (self *SInstance) GetINics() ([]cloudprovider.ICloudNic, error) {
|
||||
return nics, nil
|
||||
}
|
||||
|
||||
func (self *SInstance) GetEIP() cloudprovider.ICloudEIP {
|
||||
return &self.EipAddress
|
||||
}
|
||||
|
||||
func (self *SInstance) GetVcpuCount() int8 {
|
||||
return self.Cpu
|
||||
}
|
||||
@@ -723,3 +701,23 @@ func (self *SInstance) SyncSecurityGroup(secgroupId string, name string, rules [
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SInstance) GetIEIP() (cloudprovider.ICloudEIP, error) {
|
||||
if len(self.PublicIpAddress.IpAddress) > 0 {
|
||||
eip := SEipAddress{}
|
||||
eip.region = self.host.zone.region
|
||||
eip.IpAddress = self.PublicIpAddress.IpAddress[0]
|
||||
eip.InstanceId = self.InstanceId
|
||||
eip.InstanceType = EIP_INSTANCE_TYPE_ECS
|
||||
eip.Status = EIP_STATUS_INUSE
|
||||
eip.AllocationId = self.InstanceId // fixed
|
||||
eip.AllocationTime = self.CreationTime
|
||||
eip.Bandwidth = self.InternetMaxBandwidthOut
|
||||
eip.InternetChargeType = self.InternetChargeType
|
||||
return &eip, nil
|
||||
} else if len(self.EipAddress.IpAddress) > 0 {
|
||||
return self.host.zone.region.GetEip(self.EipAddress.AllocationId)
|
||||
} else {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
@@ -570,3 +570,37 @@ func (self *SRegion) updateInstance(instId string, name, desc, passwd, hostname
|
||||
func (self *SRegion) UpdateInstancePassword(instId string, passwd string) error {
|
||||
return self.updateInstance(instId, "", "", passwd, "")
|
||||
}
|
||||
|
||||
func (self *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
|
||||
eips, total, err := self.GetEips("", 0, 50)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for len(eips) < total {
|
||||
var parts []SEipAddress
|
||||
parts, total, err = self.GetEips("", len(eips), 50)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
eips = append(eips, parts...)
|
||||
}
|
||||
ret := make([]cloudprovider.ICloudEIP, len(eips))
|
||||
for i := 0; i < len(eips); i += 1 {
|
||||
ret[i] = &eips[i]
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (self *SRegion) GetIEipById(eipId string) (cloudprovider.ICloudEIP, error) {
|
||||
eips, total, err := self.GetEips(eipId, 0, 1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if total == 0 {
|
||||
return nil, cloudprovider.ErrNotFound
|
||||
}
|
||||
if total > 1 {
|
||||
return nil, cloudprovider.ErrDuplicateId
|
||||
}
|
||||
return &eips[0], nil
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package shell
|
||||
|
||||
import (
|
||||
"yunion.io/x/onecloud/pkg/util/aliyun"
|
||||
"yunion.io/x/onecloud/pkg/util/shellutils"
|
||||
)
|
||||
|
||||
func init() {
|
||||
type EipListOptions struct {
|
||||
Offset int `help:"List offset"`
|
||||
Limit int `help:"List limit"`
|
||||
}
|
||||
shellutils.R(&EipListOptions{}, "eip-list", "List eips", func(cli *aliyun.SRegion, args *EipListOptions) error {
|
||||
eips, total, e := cli.GetEips("", args.Offset, args.Limit)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
printList(eips, total, args.Offset, args.Limit, []string{})
|
||||
return nil
|
||||
})
|
||||
|
||||
type EipAllocateOptions struct {
|
||||
BW int `help:"Bandwidth limit in Mbps"`
|
||||
}
|
||||
shellutils.R(&EipAllocateOptions{}, "eip-create", "Allocate an EIP", func(cli *aliyun.SRegion, args *EipAllocateOptions) error {
|
||||
eip, err := cli.AllocateEIP(args.BW, aliyun.InternetChargeByTraffic)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printObject(eip)
|
||||
return nil
|
||||
})
|
||||
|
||||
type EipReleaseOptions struct {
|
||||
ID string `help:"EIP allocation ID"`
|
||||
}
|
||||
shellutils.R(&EipReleaseOptions{}, "eip-delete", "Release an EIP", func(cli *aliyun.SRegion, args *EipReleaseOptions) error {
|
||||
err := cli.DeallocateEIP(args.ID)
|
||||
return err
|
||||
})
|
||||
|
||||
type EipAssociateOptions struct {
|
||||
ID string `help:"EIP allocation ID"`
|
||||
INSTANCE string `help:"Instance ID"`
|
||||
}
|
||||
shellutils.R(&EipAssociateOptions{}, "eip-associate", "Associate an EIP", func(cli *aliyun.SRegion, args *EipAssociateOptions) error {
|
||||
err := cli.AssociateEip(args.ID, args.INSTANCE)
|
||||
return err
|
||||
})
|
||||
shellutils.R(&EipAssociateOptions{}, "eip-dissociate", "Dissociate an EIP", func(cli *aliyun.SRegion, args *EipAssociateOptions) error {
|
||||
err := cli.DissociateEip(args.ID, args.INSTANCE)
|
||||
return err
|
||||
})
|
||||
}
|
||||
@@ -109,8 +109,8 @@ func (self *SVirtualMachine) GetINics() ([]cloudprovider.ICloudNic, error) {
|
||||
return nil, cloudprovider.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (self *SVirtualMachine) GetEIP() cloudprovider.ICloudEIP {
|
||||
return nil
|
||||
func (self *SVirtualMachine) GetIEIP() (cloudprovider.ICloudEIP, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (self *SVirtualMachine) GetVcpuCount() int8 {
|
||||
|
||||
Reference in New Issue
Block a user