37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
validator.go 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
Darren Shepherd 提交于 2018-07-19 13:50 . Update for new generated code
package roletemplatebinding
import (
"fmt"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/client/management/v3"
"github.com/rancher/types/config"
)
func NewPRTBValidator(management *config.ScaledContext) types.Validator {
return newValidator(management, client.ProjectRoleTemplateBindingFieldRoleTemplateID)
}
func NewCRTBValidator(management *config.ScaledContext) types.Validator {
return newValidator(management, client.ClusterRoleTemplateBindingFieldRoleTemplateID)
}
func newValidator(management *config.ScaledContext, field string) types.Validator {
validator := &Validator{
roleTemplateLister: management.Management.RoleTemplates("").Controller().Lister(),
field: field,
}
return validator.Validator
}
type Validator struct {
roleTemplateLister v3.RoleTemplateLister
field string
}
func (v *Validator) Validator(request *types.APIContext, schema *types.Schema, data map[string]interface{}) error {
return v.ValidateRoleTemplateBinding(data[v.field])
}
func (v *Validator) ValidateRoleTemplateBinding(obj interface{}) error {
roleTemplateID, ok := obj.(string)
if !ok {
return httperror.NewAPIError(httperror.MissingRequired, "Request does not have a valid roleTemplateId")
}
roleTemplate, err := v.roleTemplateLister.Get("", roleTemplateID)
if err != nil {
return httperror.NewAPIError(httperror.ServerError, fmt.Sprintf("Error getting role template: %v", err))
}
if roleTemplate.Locked {
return httperror.NewAPIError(httperror.InvalidState, "Role is locked and cannot be assigned")
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.8-rc6

搜索帮助