mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-06-24 02:15:50 +08:00
81 lines
3.0 KiB
Go
81 lines
3.0 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"
|
|
|
|
"yunion.io/x/jsonutils"
|
|
"yunion.io/x/log"
|
|
|
|
"yunion.io/x/onecloud/pkg/mcclient"
|
|
"yunion.io/x/onecloud/pkg/util/logclient"
|
|
"yunion.io/x/onecloud/pkg/util/rbacutils"
|
|
)
|
|
|
|
type SEnabledStatusStandaloneResourceBase struct {
|
|
SStatusStandaloneResourceBase
|
|
|
|
Enabled bool `nullable:"false" default:"false" list:"user" create:"optional"` // = Column(Boolean, nullable=False, default=False)
|
|
}
|
|
|
|
type SEnabledStatusStandaloneResourceBaseManager struct {
|
|
SStatusStandaloneResourceBaseManager
|
|
}
|
|
|
|
func NewEnabledStatusStandaloneResourceBaseManager(dt interface{}, tableName string, keyword string, keywordPlural string) SEnabledStatusStandaloneResourceBaseManager {
|
|
return SEnabledStatusStandaloneResourceBaseManager{SStatusStandaloneResourceBaseManager: NewStatusStandaloneResourceBaseManager(dt, tableName, keyword, keywordPlural)}
|
|
}
|
|
|
|
func (self *SEnabledStatusStandaloneResourceBase) AllowPerformEnable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
|
return IsAllowPerform(rbacutils.ScopeSystem, userCred, self, "enable")
|
|
}
|
|
|
|
func (self *SEnabledStatusStandaloneResourceBase) PerformEnable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
|
if !self.Enabled {
|
|
_, err := Update(self, func() error {
|
|
self.Enabled = true
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
log.Errorf("PerformEnable save update fail %s", err)
|
|
return nil, err
|
|
}
|
|
OpsLog.LogEvent(self, ACT_ENABLE, "", userCred)
|
|
logclient.AddSimpleActionLog(self, logclient.ACT_ENABLE, nil, userCred, true)
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (self *SEnabledStatusStandaloneResourceBase) AllowPerformDisable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
|
return IsAllowPerform(rbacutils.ScopeSystem, userCred, self, "disable")
|
|
}
|
|
|
|
func (self *SEnabledStatusStandaloneResourceBase) PerformDisable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
|
if self.Enabled {
|
|
_, err := Update(self, func() error {
|
|
self.Enabled = false
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
log.Errorf("PerformDisable save update fail %s", err)
|
|
return nil, err
|
|
}
|
|
OpsLog.LogEvent(self, ACT_DISABLE, "", userCred)
|
|
logclient.AddSimpleActionLog(self, logclient.ACT_DISABLE, nil, userCred, true)
|
|
}
|
|
return nil, nil
|
|
}
|