diff --git a/cmd/climc/shell/compute/natgateways.go b/cmd/climc/shell/compute/natgateways.go index 1b3682cbcc..704848d8ed 100644 --- a/cmd/climc/shell/compute/natgateways.go +++ b/cmd/climc/shell/compute/natgateways.go @@ -29,4 +29,6 @@ func init() { cmd.Show(&compute.NatGatewayIdOptions{}) cmd.Delete(&compute.NatGatewayDeleteOption{}) cmd.Perform("syncstauts", &compute.NatGatewayIdOptions{}) + cmd.Perform("cancel-expire", &compute.NatGatewayIdOptions{}) + cmd.Perform("postpaid-expire", &compute.NatPostpaidExpireOptions{}) } diff --git a/pkg/apis/input.go b/pkg/apis/input.go index cb9a13d97c..3e8692f545 100644 --- a/pkg/apis/input.go +++ b/pkg/apis/input.go @@ -355,7 +355,7 @@ type DistinctFieldInput struct { type PostpaidExpireInput struct { Duration string `json:"duration"` - ExpireTime time.Time `json:"expireType"` + ExpireTime time.Time `json:"expire_time"` } type AutoRenewInput struct { diff --git a/pkg/compute/models/natgateways.go b/pkg/compute/models/natgateways.go index 1b9e044444..93b48b4d44 100644 --- a/pkg/compute/models/natgateways.go +++ b/pkg/compute/models/natgateways.go @@ -889,6 +889,32 @@ func (manager *SNatEntryManager) FetchCustomizeColumns( return rows } +func (self *SNatGateway) AllowPerformCancelExpire(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool { + return self.IsOwner(userCred) || db.IsAdminAllowPerform(userCred, self, "cancel-expire") +} + +func (self *SNatGateway) PerformCancelExpire(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) { + return nil, self.CancelExpireTime(ctx, userCred) +} + +func (self *SNatGateway) CancelExpireTime(ctx context.Context, userCred mcclient.TokenCredential) error { + if self.BillingType != billing_api.BILLING_TYPE_POSTPAID { + return httperrors.NewBadRequestError("nat billing type %s not support cancel expire", self.BillingType) + } + + _, err := sqlchemy.GetDB().Exec( + fmt.Sprintf( + "update %s set expired_at = NULL and billing_cycle = NULL where id = ?", + NatGatewayManager.TableSpec().Name(), + ), self.Id, + ) + if err != nil { + return errors.Wrap(err, "nat cancel expire time") + } + db.OpsLog.LogEvent(self, db.ACT_RENEW, "nat cancel expire time", userCred) + return nil +} + func (self *SNatGateway) AllowPerformPostpaidExpire(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool { return self.IsOwner(userCred) || db.IsAdminAllowPerform(userCred, self, "postpaid-expire") } diff --git a/pkg/mcclient/options/compute/natgateways.go b/pkg/mcclient/options/compute/natgateways.go index 858a12d459..6528d9daf5 100644 --- a/pkg/mcclient/options/compute/natgateways.go +++ b/pkg/mcclient/options/compute/natgateways.go @@ -17,6 +17,7 @@ package compute import ( "yunion.io/x/jsonutils" + "yunion.io/x/onecloud/pkg/apis" "yunion.io/x/onecloud/pkg/mcclient/options" ) @@ -91,3 +92,12 @@ type NatSCreateOptions struct { SourceCIDR string `help:"Source CIDR"` NetworkID string `help:"Network id"` } + +type NatPostpaidExpireOptions struct { + NatGatewayIdOptions + apis.PostpaidExpireInput +} + +func (opts *NatPostpaidExpireOptions) Params() (jsonutils.JSONObject, error) { + return jsonutils.Marshal(opts.PostpaidExpireInput), nil +}