37 Star 396 Fork 71

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
store.go 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
kinarashah 提交于 2018-04-16 16:50 . System project
package scoped
import (
"fmt"
"strings"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/store/transform"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/types/client/management/v3"
mgmtclient "github.com/rancher/types/client/management/v3"
)
type Store struct {
types.Store
key string
}
func NewScopedStore(key string, store types.Store) *Store {
return &Store{
Store: &transform.Store{
Store: store,
Transformer: func(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, opt *types.QueryOptions) (map[string]interface{}, error) {
if data == nil {
return data, nil
}
v := convert.ToString(data[key])
if !strings.HasSuffix(v, ":"+convert.ToString(data[client.ProjectFieldNamespaceId])) {
data[key] = data[client.ProjectFieldNamespaceId]
}
data[client.ProjectFieldNamespaceId] = nil
return data, nil
},
},
key: key,
}
}
func (s *Store) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
if data != nil {
parts := strings.Split(convert.ToString(data[s.key]), ":")
data["namespaceId"] = parts[len(parts)-1]
}
return s.Store.Create(apiContext, schema, data)
}
func (s *Store) Delete(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
var project mgmtclient.Project
if err := access.ByID(apiContext, apiContext.Version, apiContext.Type, apiContext.ID, &project); err == nil {
if project.Labels["authz.management.cattle.io/system-project"] == "true" {
return nil, httperror.NewAPIError(httperror.MethodNotAllowed, "System Project cannot be deleted")
}
} else {
return nil, httperror.NewAPIError(httperror.ServerError, fmt.Sprintf("Error accessing project [%s]: %v", id, err))
}
return s.Store.Delete(apiContext, schema, id)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.15-rc2

搜索帮助

344bd9b3 5694891 D2dac590 5694891