Skip to content

Commit

Permalink
Merge pull request #1175 from ioito/hotfix/qx-change-billing-type
Browse files Browse the repository at this point in the history
fix(aliyun): support change vm billing type
  • Loading branch information
ioito authored Feb 12, 2025
2 parents a8836a4 + 05a591f commit c4099ce
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/cloudprovider/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type IBillingResource interface {
GetExpiredAt() time.Time
SetAutoRenew(bc billing.SBillingCycle) error
Renew(bc billing.SBillingCycle) error
ChangeBillingType(billType string) error
IsAutoRenew() bool
}

Expand Down
26 changes: 26 additions & 0 deletions pkg/multicloud/aliyun/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,32 @@ func (self *SInstance) GetBillingType() string {
return convertChargeType(self.InstanceChargeType)
}

func (self *SInstance) ChangeBillingType(billingType string) error {
return self.host.zone.region.ModifyInstanceChargeType(self.InstanceId, billingType)
}

func (region *SRegion) ModifyInstanceChargeType(vmId string, billingType string) error {
params := map[string]string{
"RegionId": region.RegionId,
"IncludeDataDisks": "true",
"AutoPay": "true",
"ClientToken": utils.GenRequestId(20),
"InstanceIds": jsonutils.Marshal([]string{vmId}).String(),
}
switch billingType {
case billing_api.BILLING_TYPE_POSTPAID:
params["InstanceChargeType"] = "PostPaid"
case billing_api.BILLING_TYPE_PREPAID:
params["InstanceChargeType"] = "PrePaid"
params["Period"] = "1"
params["PeriodUnit"] = "Month"
default:
return fmt.Errorf("invalid billing_type %s", billingType)
}
_, err := region.ecsRequest("ModifyInstanceChargeType", params)
return err
}

func (self *SInstance) GetCreatedAt() time.Time {
return self.CreationTime
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/multicloud/aliyun/shell/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,13 @@ func init() {
return nil
})

type InstanceChangeBillingType struct {
ID string
BillingType string `choices:"postpaid|prepaid" default:"prepaid"`
}

shellutils.R(&InstanceChangeBillingType{}, "instance-change-billing-type", "change instance billing type", func(cli *aliyun.SRegion, args *InstanceChangeBillingType) error {
return cli.ModifyInstanceChargeType(args.ID, args.BillingType)
})

}
4 changes: 4 additions & 0 deletions pkg/multicloud/billing_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ func (self *SBillingBase) IsAutoRenew() bool {
func (self *SBillingBase) Renew(bc billing.SBillingCycle) error {
return errors.Wrap(cloudprovider.ErrNotImplemented, "Renew")
}

func (self *SBillingBase) ChangeBillingType(billType string) error {
return errors.Wrap(cloudprovider.ErrNotImplemented, "ChangeBillingType")
}
1 change: 1 addition & 0 deletions pkg/multicloud/hcso/modelarts.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
type SModelartsPool struct {
region *SRegion
multicloud.SResourceBase
multicloud.SBillingBase

Metadata SModelartsPoolMetadata `json:"metadata"`
Spec SModelartsPoolSpec `json:"spec"`
Expand Down
1 change: 1 addition & 0 deletions pkg/multicloud/huawei/modelarts_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
type SModelartsPool struct {
region *SRegion
multicloud.SResourceBase
multicloud.SBillingBase

Metadata SModelartsPoolMetadata `json:"metadata"`
Spec SModelartsPoolSpec `json:"spec"`
Expand Down
24 changes: 24 additions & 0 deletions pkg/multicloud/qcloud/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,30 @@ func (self *SInstance) GetBillingType() string {
}
}

func (self *SInstance) ChangeBillingType(billingType string) error {
return self.host.zone.region.ModifyInstanceChargeType(self.InstanceId, billingType)
}

func (region *SRegion) ModifyInstanceChargeType(vmId string, billingType string) error {
params := map[string]string{
"Region": region.Region,
"ModifyPortableDataDisk": "true",
"InstanceIds.0": vmId,
}
switch billingType {
case billing_api.BILLING_TYPE_POSTPAID:
params["InstanceChargeType"] = "POSTPAID_BY_HOUR"
case billing_api.BILLING_TYPE_PREPAID:
params["InstanceChargeType"] = "PREPAID"
params["InstanceChargePrepaid.Period"] = "1"
params["InstanceChargePrepaid.RenewFlag"] = "NOTIFY_AND_AUTO_RENEW"
default:
return fmt.Errorf("invalid billing_type %s", billingType)
}
_, err := region.cvmRequest("ModifyInstancesChargeType", params, false)
return err
}

func (self *SInstance) GetCreatedAt() time.Time {
return self.CreatedTime
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/multicloud/qcloud/shell/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"yunion.io/x/pkg/util/shellutils"

"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/cloudmux/pkg/multicloud/aliyun"
"yunion.io/x/cloudmux/pkg/multicloud/qcloud"
)

Expand Down Expand Up @@ -239,4 +240,13 @@ func init() {
return cli.UpdateInstanceBandwidth(args.ID, args.BANDWIDTH, args.InternetChargeType)
})

type InstanceChangeBillingType struct {
ID string
BillingType string `choices:"postpaid|prepaid" default:"prepaid"`
}

shellutils.R(&InstanceChangeBillingType{}, "instance-change-billing-type", "change instance billing type", func(cli *aliyun.SRegion, args *InstanceChangeBillingType) error {
return cli.ModifyInstanceChargeType(args.ID, args.BillingType)
})

}

0 comments on commit c4099ce

Please sign in to comment.