Merge pull request #8685 from ioito/automated-cherry-pick-of-#8683-upstream-release-3.4

Automated cherry pick of #8683: fix: aliyun eip api
This commit is contained in:
yunion-ci-robot
2020-11-06 15:04:20 +08:00
committed by GitHub
2 changed files with 26 additions and 12 deletions

View File

@@ -72,6 +72,7 @@ type SEipAddress struct {
region *SRegion
multicloud.SEipBase
Name string
AllocationId string
InternetChargeType TInternetChargeType
@@ -83,7 +84,13 @@ type SEipAddress struct {
InstanceId string
Bandwidth int /* Mbps */
AllocationTime time.Time
BusinessStatus string
AllocationTime time.Time
DeletionProtection bool
Descritpion string
ISP string
Mode string
Netmode string
OperationLocks string
@@ -97,6 +104,9 @@ func (self *SEipAddress) GetId() string {
}
func (self *SEipAddress) GetName() string {
if len(self.Name) > 0 {
return self.Name
}
return self.IpAddress
}
@@ -257,7 +267,7 @@ func (region *SRegion) GetEips(eipId string, associatedId string, offset int, li
}
}
body, err := region.ecsRequest("DescribeEipAddresses", params)
body, err := region.vpcRequest("DescribeEipAddresses", params)
if err != nil {
log.Errorf("DescribeEipAddresses fail %s", err)
return nil, 0, err
@@ -287,9 +297,12 @@ func (region *SRegion) GetEip(eipId string) (*SEipAddress, error) {
return &eips[0], nil
}
func (region *SRegion) AllocateEIP(bwMbps int, chargeType TInternetChargeType, projectId string) (*SEipAddress, error) {
func (region *SRegion) AllocateEIP(name string, bwMbps int, chargeType TInternetChargeType, projectId string) (*SEipAddress, error) {
params := make(map[string]string)
if len(name) > 0 {
params["Name"] = name
}
params["Bandwidth"] = fmt.Sprintf("%d", bwMbps)
params["InternetChargeType"] = string(chargeType)
params["InstanceChargeType"] = "PostPaid"
@@ -298,7 +311,7 @@ func (region *SRegion) AllocateEIP(bwMbps int, chargeType TInternetChargeType, p
params["ResourceGroupId"] = projectId
}
body, err := region.ecsRequest("AllocateEipAddress", params)
body, err := region.vpcRequest("AllocateEipAddress", params)
if err != nil {
log.Errorf("AllocateEipAddress fail %s", err)
return nil, err
@@ -313,22 +326,22 @@ func (region *SRegion) AllocateEIP(bwMbps int, chargeType TInternetChargeType, p
return region.GetEip(eipId)
}
func (region *SRegion) CreateEIP(eip *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
func (region *SRegion) CreateEIP(opts *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
var ctype TInternetChargeType
switch eip.ChargeType {
switch opts.ChargeType {
case api.EIP_CHARGE_TYPE_BY_TRAFFIC:
ctype = InternetChargeByTraffic
case api.EIP_CHARGE_TYPE_BY_BANDWIDTH:
ctype = InternetChargeByBandwidth
}
return region.AllocateEIP(eip.BandwidthMbps, ctype, eip.ProjectId)
return region.AllocateEIP(opts.Name, opts.BandwidthMbps, ctype, opts.ProjectId)
}
func (region *SRegion) DeallocateEIP(eipId string) error {
params := make(map[string]string)
params["AllocationId"] = eipId
_, err := region.ecsRequest("ReleaseEipAddress", params)
_, err := region.vpcRequest("ReleaseEipAddress", params)
if err != nil {
log.Errorf("ReleaseEipAddress fail %s", err)
}
@@ -345,7 +358,7 @@ func (region *SRegion) AssociateEip(eipId string, instanceId string) error {
}
}
_, err := region.ecsRequest("AssociateEipAddress", params)
_, err := region.vpcRequest("AssociateEipAddress", params)
if err != nil {
log.Errorf("AssociateEipAddress fail %s", err)
}
@@ -362,7 +375,7 @@ func (region *SRegion) DissociateEip(eipId string, instanceId string) error {
}
}
_, err := region.ecsRequest("UnassociateEipAddress", params)
_, err := region.vpcRequest("UnassociateEipAddress", params)
if err != nil {
log.Errorf("UnassociateEipAddress fail %s", err)
}
@@ -374,7 +387,7 @@ func (region *SRegion) UpdateEipBandwidth(eipId string, bw int) error {
params["AllocationId"] = eipId
params["Bandwidth"] = fmt.Sprintf("%d", bw)
_, err := region.ecsRequest("ModifyEipAddressAttribute", params)
_, err := region.vpcRequest("ModifyEipAddressAttribute", params)
if err != nil {
log.Errorf("ModifyEipAddressAttribute fail %s", err)
}

View File

@@ -35,11 +35,12 @@ func init() {
})
type EipAllocateOptions struct {
Name string
BW int `help:"Bandwidth limit in Mbps"`
ResourceGroupId string `help:"Resource group Id"`
}
shellutils.R(&EipAllocateOptions{}, "eip-create", "Allocate an EIP", func(cli *aliyun.SRegion, args *EipAllocateOptions) error {
eip, err := cli.AllocateEIP(args.BW, aliyun.InternetChargeByTraffic, args.ResourceGroupId)
eip, err := cli.AllocateEIP(args.Name, args.BW, aliyun.InternetChargeByTraffic, args.ResourceGroupId)
if err != nil {
return err
}