Skip to content

Commit

Permalink
redis&rds add postpaid duration
Browse files Browse the repository at this point in the history
  • Loading branch information
tangbin committed May 28, 2020
1 parent 575f1c2 commit 346d558
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
10 changes: 9 additions & 1 deletion pkg/apis/compute/dbinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

package compute

import "yunion.io/x/onecloud/pkg/apis"
import (
"time"

"yunion.io/x/onecloud/pkg/apis"
)

type DBInstanceCreateInput struct {
apis.VirtualResourceCreateInput
Expand Down Expand Up @@ -75,6 +79,10 @@ type DBInstanceCreateInput struct {
Duration string `json:"duration"`

// swagger:ignore
ExpiredAt time.Time `json:"expired_at"`

// 计费方式
// enum: postpaid, prepaid
BillingType string
// swagger:ignore
BillingCycle string
Expand Down
3 changes: 2 additions & 1 deletion pkg/compute/models/billingresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ func (manager *SBillingResourceBaseManager) OrderByExtraFields(
func ListExpiredPostpaidResources(
q *sqlchemy.SQuery, limit int) *sqlchemy.SQuery {
q = q.Equals("billing_type", api.BILLING_TYPE_POSTPAID)
q = q.GE("expired_at", time.Now())
q = q.IsNotNull("expired_at")
q = q.LT("expired_at", time.Now())
if limit > 0 {
q = q.Limit(limit)
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/compute/models/dbinstances.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,14 @@ func (man *SDBInstanceManager) ValidateCreateData(ctx context.Context, userCred
if !region.GetDriver().IsSupportedBillingCycle(billingCycle, man.KeywordPlural()) {
return nil, httperrors.NewInputParameterError("unsupported duration %s", input.Duration)
}
input.BillingType = billing_api.BILLING_TYPE_PREPAID

if !utils.IsInStringArray(input.BillingType, []string{billing_api.BILLING_TYPE_PREPAID, billing_api.BILLING_TYPE_POSTPAID}) {
input.BillingType = billing_api.BILLING_TYPE_PREPAID
}

tm := time.Time{}
input.BillingCycle = billingCycle.String()
input.ExpiredAt = billingCycle.EndAt(tm)
}

if len(input.InstanceType) == 0 && (input.VcpuCount == 0 || input.VmemSizeMb == 0) {
Expand Down
16 changes: 16 additions & 0 deletions pkg/compute/models/elasticcache_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,22 @@ func (manager *SElasticcacheManager) ValidateCreateData(ctx context.Context, use
return nil, fmt.Errorf("getting region failed")
}

// postpiad billing cycle
billingType, _ := data.GetString("billing_type")
if billingType == billing_api.BILLING_TYPE_POSTPAID {
billingCycle, _ := data.GetString("duration")
if len(billingCycle) > 0 {
cycle, err := bc.ParseBillingCycle(billingCycle)
if err != nil {
return nil, httperrors.NewInputParameterError("invalid billing_cycle %s", billingCycle)
}

tm := time.Time{}
data.Set("billing_cycle", jsonutils.NewString(cycle.String()))
data.Set("expired_at", jsonutils.NewString(cycle.EndAt(tm).Format("2006-01-02 15:04:05")))
}
}

input := apis.VirtualResourceCreateInput{}
var err error
err = data.Unmarshal(&input)
Expand Down
12 changes: 7 additions & 5 deletions pkg/compute/models/guests.go
Original file line number Diff line number Diff line change
Expand Up @@ -4356,8 +4356,11 @@ func (manager *SGuestManager) CleanPendingDeleteServers(ctx context.Context, use
}

func (manager *SGuestManager) getExpiredPrepaidGuests() []SGuest {
q := ListExpiredPostpaidResources(manager.Query(), options.Options.ExpiredPrepaidMaxCleanBatchSize)
q = q.IsFalse("pending_deleted")
deadline := time.Now().Add(time.Duration(options.Options.PrepaidExpireCheckSeconds*-1) * time.Second)

q := manager.Query()
q = q.Equals("billing_type", billing_api.BILLING_TYPE_PREPAID).LT("expired_at", deadline).
IsFalse("pending_deleted").Limit(options.Options.ExpiredPrepaidMaxCleanBatchSize)

guests := make([]SGuest, 0)
err := db.FetchModelObjects(GuestManager, q, &guests)
Expand Down Expand Up @@ -4386,9 +4389,8 @@ func (manager *SGuestManager) getNeedRenewPrepaidGuests() ([]SGuest, error) {
}

func (manager *SGuestManager) getExpiredPostpaidGuests() []SGuest {
deadline := time.Now()
q := manager.Query().Equals("billing_type", billing_api.BILLING_TYPE_POSTPAID).IsFalse("pending_deleted").
LT("expired_at", deadline).Limit(options.Options.ExpiredPrepaidMaxCleanBatchSize)
q := ListExpiredPostpaidResources(manager.Query(), options.Options.ExpiredPrepaidMaxCleanBatchSize)
q = q.IsFalse("pending_deleted")
guests := make([]SGuest, 0)
err := db.FetchModelObjects(GuestManager, q, &guests)
if err != nil {
Expand Down

0 comments on commit 346d558

Please sign in to comment.