|
| 1 | +// Copyright 2019 Yunion |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package disk |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "time" |
| 20 | + |
| 21 | + "yunion.io/x/cloudmux/pkg/cloudprovider" |
| 22 | + "yunion.io/x/jsonutils" |
| 23 | + "yunion.io/x/pkg/errors" |
| 24 | + |
| 25 | + "yunion.io/x/onecloud/pkg/apis" |
| 26 | + billing_api "yunion.io/x/onecloud/pkg/apis/billing" |
| 27 | + api "yunion.io/x/onecloud/pkg/apis/compute" |
| 28 | + "yunion.io/x/onecloud/pkg/cloudcommon/db" |
| 29 | + "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" |
| 30 | + "yunion.io/x/onecloud/pkg/compute/models" |
| 31 | + "yunion.io/x/onecloud/pkg/util/logclient" |
| 32 | +) |
| 33 | + |
| 34 | +type DiskChangeBillingTypeTask struct { |
| 35 | + SDiskBaseTask |
| 36 | +} |
| 37 | + |
| 38 | +func init() { |
| 39 | + taskman.RegisterTask(DiskChangeBillingTypeTask{}) |
| 40 | +} |
| 41 | + |
| 42 | +func (self *DiskChangeBillingTypeTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) { |
| 43 | + disk := obj.(*models.SDisk) |
| 44 | + |
| 45 | + idisk, err := disk.GetIDisk(ctx) |
| 46 | + if err != nil { |
| 47 | + self.taskFail(ctx, disk, errors.Wrapf(err, "GetIDisk")) |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + billType := "" |
| 52 | + switch disk.BillingType { |
| 53 | + case billing_api.BILLING_TYPE_POSTPAID: |
| 54 | + billType = billing_api.BILLING_TYPE_PREPAID |
| 55 | + case billing_api.BILLING_TYPE_PREPAID: |
| 56 | + billType = billing_api.BILLING_TYPE_POSTPAID |
| 57 | + } |
| 58 | + |
| 59 | + err = idisk.ChangeBillingType(billType) |
| 60 | + if err != nil { |
| 61 | + if errors.Cause(err) == cloudprovider.ErrNotImplemented { |
| 62 | + disk.SetStatus(ctx, self.GetUserCred(), api.DISK_READY, "") |
| 63 | + self.SetStageComplete(ctx, nil) |
| 64 | + return |
| 65 | + } |
| 66 | + self.taskFail(ctx, disk, errors.Wrapf(err, "ChangeBillingType")) |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + idisk.Refresh() |
| 71 | + |
| 72 | + db.Update(disk, func() error { |
| 73 | + disk.BillingType = billType |
| 74 | + disk.ExpiredAt = time.Time{} |
| 75 | + if disk.BillingType == billing_api.BILLING_TYPE_PREPAID { |
| 76 | + disk.AutoRenew = idisk.IsAutoRenew() |
| 77 | + disk.ExpiredAt = idisk.GetExpiredAt() |
| 78 | + } |
| 79 | + return nil |
| 80 | + }) |
| 81 | + |
| 82 | + self.taskComplete(ctx, disk) |
| 83 | +} |
| 84 | + |
| 85 | +func (self *DiskChangeBillingTypeTask) taskComplete(ctx context.Context, disk *models.SDisk) { |
| 86 | + disk.SetStatus(ctx, self.GetUserCred(), api.DISK_READY, "") |
| 87 | + logclient.AddActionLogWithStartable(self, disk, logclient.ACT_CHANGE_BILLING_TYPE, disk.BillingType, self.UserCred, true) |
| 88 | + self.SetStageComplete(ctx, nil) |
| 89 | +} |
| 90 | + |
| 91 | +func (self *DiskChangeBillingTypeTask) taskFail(ctx context.Context, disk *models.SDisk, err error) { |
| 92 | + disk.SetStatus(ctx, self.GetUserCred(), apis.STATUS_CHANGE_BILLING_TYPE_FAILED, "") |
| 93 | + logclient.AddActionLogWithStartable(self, disk, logclient.ACT_CHANGE_BILLING_TYPE, err, self.UserCred, false) |
| 94 | + self.SetStageFailed(ctx, jsonutils.NewString(err.Error())) |
| 95 | +} |
0 commit comments