mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-10-24 23:41:52 +08:00
add interface freeze virtual resource
This commit is contained in:
@@ -47,6 +47,9 @@ const (
|
||||
ACT_BACKUP_START = "backup_start"
|
||||
ACT_BACKUP_START_FAILED = "backup_start_fail"
|
||||
|
||||
ACT_FREEZE = "freeze"
|
||||
ACT_UNFREEZE = "unfreeze"
|
||||
|
||||
ACT_RESTARING = "restarting"
|
||||
ACT_RESTART_FAIL = "restart_fail"
|
||||
|
||||
|
||||
@@ -64,6 +64,8 @@ type SVirtualResourceBase struct {
|
||||
PendingDeletedAt time.Time `json:"pending_deleted_at" list:"user"`
|
||||
// 资源是否处于回收站中
|
||||
PendingDeleted bool `nullable:"false" default:"false" index:"true" get:"user" list:"user" json:"pending_deleted"`
|
||||
|
||||
Freezed bool `nullable:"false" default:"false" get:"user" list:"user" json:"freezed"`
|
||||
}
|
||||
|
||||
func (model *SVirtualResourceBase) IsOwner(userCred mcclient.TokenCredential) bool {
|
||||
@@ -228,8 +230,21 @@ func (manager *SVirtualResourceBaseManager) FetchCustomizeColumns(
|
||||
return ret
|
||||
}
|
||||
|
||||
func (model *SVirtualResourceBase) PreCheckPerformAction(
|
||||
ctx context.Context, userCred mcclient.TokenCredential,
|
||||
action string, query jsonutils.JSONObject, data jsonutils.JSONObject,
|
||||
) error {
|
||||
if err := model.SStandaloneResourceBase.PreCheckPerformAction(ctx, userCred, action, query, data); err != nil {
|
||||
return err
|
||||
}
|
||||
if model.Freezed && action != "unfreeze" {
|
||||
return httperrors.NewBadRequestError("Virtual resource freezed, can't do %s", action)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (model *SVirtualResourceBase) AllowUpdateItem(ctx context.Context, userCred mcclient.TokenCredential) bool {
|
||||
return model.IsOwner(userCred) || IsAdminAllowUpdate(userCred, model)
|
||||
return !model.Freezed && (model.IsOwner(userCred) || IsAdminAllowUpdate(userCred, model))
|
||||
}
|
||||
|
||||
func (model *SVirtualResourceBase) AllowDeleteItem(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
||||
@@ -253,6 +268,47 @@ func (model *SVirtualResourceBase) GetTenantCache(ctx context.Context) (*STenant
|
||||
return TenantCacheManager.FetchTenantById(ctx, model.ProjectId)
|
||||
}
|
||||
|
||||
func (model *SVirtualResourceBase) AllowPerformFreeze(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformFreezeInput) bool {
|
||||
return IsAdminAllowPerform(userCred, model, "freeze")
|
||||
}
|
||||
|
||||
// freezed update and perform action operation except for unfreeze
|
||||
func (model *SVirtualResourceBase) PerformFreeze(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformFreezeInput) (jsonutils.JSONObject, error) {
|
||||
if model.Freezed {
|
||||
return nil, httperrors.NewBadRequestError("virtual resource already freezed")
|
||||
}
|
||||
_, err := Update(model, func() error {
|
||||
model.Freezed = true
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
OpsLog.LogEvent(model, ACT_FREEZE, "perform freeze", userCred)
|
||||
logclient.AddActionLogWithContext(ctx, model, logclient.ACT_FREEZE, "perform freeze", userCred, true)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (model *SVirtualResourceBase) AllowPerformUnfreeze(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformUnfreezeInput) bool {
|
||||
return IsAdminAllowPerform(userCred, model, "unfreeze")
|
||||
}
|
||||
|
||||
func (model *SVirtualResourceBase) PerformUnfreeze(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformUnfreezeInput) (jsonutils.JSONObject, error) {
|
||||
if !model.Freezed {
|
||||
return nil, httperrors.NewBadRequestError("virtual resource not freezed")
|
||||
}
|
||||
_, err := Update(model, func() error {
|
||||
model.Freezed = false
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
OpsLog.LogEvent(model, ACT_UNFREEZE, "perform unfreeze", userCred)
|
||||
logclient.AddActionLogWithContext(ctx, model, logclient.ACT_UNFREEZE, "perform unfreeze", userCred, true)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (model *SVirtualResourceBase) AllowPerformChangeOwner(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformChangeProjectOwnerInput) bool {
|
||||
return IsAdminAllowPerform(userCred, model, "change-owner")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user