mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-06-20 10:22:22 +08:00
60 lines
1.8 KiB
Go
60 lines
1.8 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/yunionio/jsonutils"
|
|
"github.com/yunionio/mcclient"
|
|
)
|
|
|
|
type SStatusStandaloneResourceBase struct {
|
|
SStandaloneResourceBase
|
|
|
|
Status string `width:"36" charset:"ascii" nullable:"false" default:"init" list:"user"`
|
|
}
|
|
|
|
type SStatusStandaloneResourceBaseManager struct {
|
|
SStandaloneResourceBaseManager
|
|
}
|
|
|
|
func NewStatusStandaloneResourceBaseManager(dt interface{}, tableName string, keyword string, keywordPlural string) SStatusStandaloneResourceBaseManager {
|
|
return SStatusStandaloneResourceBaseManager{SStandaloneResourceBaseManager: NewStandaloneResourceBaseManager(dt, tableName, keyword, keywordPlural)}
|
|
}
|
|
|
|
func (model *SStatusStandaloneResourceBase) SetStatus(userCred mcclient.TokenCredential, status string, reason string) error {
|
|
if model.Status == status {
|
|
return nil
|
|
}
|
|
oldStatus := model.Status
|
|
_, err := model.GetModelManager().TableSpec().Update(model, func() error {
|
|
model.Status = status
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if userCred != nil {
|
|
notes := fmt.Sprintf("%s=>%s", oldStatus, status)
|
|
if len(reason) > 0 {
|
|
notes = fmt.Sprintf("%s: %s", notes, reason)
|
|
}
|
|
OpsLog.LogEvent(model, ACT_UPDATE_STATUS, notes, userCred)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (model *SStatusStandaloneResourceBase) AllowPerformStatus(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
|
return userCred.IsSystemAdmin()
|
|
}
|
|
|
|
func (model *SStatusStandaloneResourceBase) PerformStatus(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
|
status, err := data.GetString("status")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
reason, _ := data.GetString("reason")
|
|
err = model.SetStatus(userCred, status, reason)
|
|
return nil, err
|
|
}
|