37 Star 396 Fork 71

GVPrancher/rancher

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
validator.go 1.64 KB
Copy Edit Raw Blame History
Darren Shepherd authored 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.2.0-alpha4

Search

344bd9b3 5694891 D2dac590 5694891