mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-06-20 13:22:14 +08:00
92 lines
3.2 KiB
Go
92 lines
3.2 KiB
Go
// Copyright 2019 Yunion
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"yunion.io/x/jsonutils"
|
|
"yunion.io/x/pkg/utils"
|
|
|
|
"yunion.io/x/onecloud/pkg/httperrors"
|
|
"yunion.io/x/onecloud/pkg/mcclient"
|
|
"yunion.io/x/onecloud/pkg/util/rbacutils"
|
|
)
|
|
|
|
type SStatusStandaloneResourceBase struct {
|
|
SStandaloneResourceBase
|
|
|
|
Status string `width:"36" charset:"ascii" nullable:"false" default:"init" list:"user" create:"optional"`
|
|
}
|
|
|
|
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 := 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 IsAllowPerform(rbacutils.ScopeSystem, userCred, model, "status")
|
|
}
|
|
|
|
func (model *SStatusStandaloneResourceBase) PerformStatus(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
|
status, _ := data.GetString("status")
|
|
if len(status) == 0 {
|
|
return nil, httperrors.NewMissingParameterError("status")
|
|
}
|
|
reason, _ := data.GetString("reason")
|
|
err := model.SetStatus(userCred, status, reason)
|
|
return nil, err
|
|
}
|
|
|
|
func (model *SStatusStandaloneResourceBase) IsInStatus(status ...string) bool {
|
|
return utils.IsInStringArray(model.Status, status)
|
|
}
|
|
|
|
func (model *SStatusStandaloneResourceBase) AllowGetDetailsStatus(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool {
|
|
return IsAllowGetSpec(rbacutils.ScopeSystem, userCred, model, "status")
|
|
}
|
|
|
|
func (model *SStatusStandaloneResourceBase) GetDetailsStatus(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
|
ret := jsonutils.NewDict()
|
|
ret.Add(jsonutils.NewString(model.Status), "status")
|
|
return ret, nil
|
|
}
|