forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
55 lines (43 loc) · 1.65 KB
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package roletemplatebinding
import (
"fmt"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
client "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
}