Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(aliyun): support change vm billing type #1175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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