Skip to content

Commit

Permalink
fix(aliyun): support change vm billing type
Browse files Browse the repository at this point in the history
  • Loading branch information
ioito committed Feb 11, 2025
1 parent fc323d7 commit 7a81c58
Show file tree
Hide file tree
Showing 5 changed files with 39 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
24 changes: 24 additions & 0 deletions pkg/multicloud/aliyun/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,30 @@ 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"
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/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

0 comments on commit 7a81c58

Please sign in to comment.