forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rest.go
78 lines (64 loc) · 2.68 KB
/
rest.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package rolebinding
import (
"fmt"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
"github.com/openshift/origin/pkg/authorization/api/validation"
)
// TODO add get and list
// REST implements the RESTStorage interface in terms of an Registry.
type REST struct {
registry Registry
}
// NewREST creates a new REST for policies.
func NewREST(registry Registry) apiserver.RESTStorage {
return &REST{registry}
}
// New creates a new RoleBinding object
func (r *REST) New() runtime.Object {
return &authorizationapi.RoleBinding{}
}
// Delete asynchronously deletes the PolicyBinding specified by its name.
func (r *REST) Delete(ctx kapi.Context, name string) (runtime.Object, error) {
return &kapi.Status{Status: kapi.StatusSuccess}, r.registry.DeleteRoleBinding(ctx, name)
}
// Create registers a given new RoleBinding inside the PolicyBinding instance to r.bindingRegistry.
func (r *REST) Create(ctx kapi.Context, obj runtime.Object) (runtime.Object, error) {
roleBinding, ok := obj.(*authorizationapi.RoleBinding)
if !ok {
return nil, fmt.Errorf("not a roleBinding: %#v", obj)
}
if !kapi.ValidNamespace(ctx, &roleBinding.ObjectMeta) {
return nil, kerrors.NewConflict("roleBinding", roleBinding.Namespace, fmt.Errorf("RoleBinding.Namespace does not match the provided context"))
}
kapi.FillObjectMetaSystemFields(ctx, &roleBinding.ObjectMeta)
if errs := validation.ValidateRoleBinding(roleBinding); len(errs) > 0 {
return nil, kerrors.NewInvalid("roleBinding", roleBinding.Name, errs)
}
err := r.registry.CreateRoleBinding(ctx, roleBinding)
if err != nil {
return nil, err
}
return roleBinding, nil
}
// Update replaces a given RoleBinding inside the PolicyBinding instance with an existing instance in r.bindingRegistry.
func (r *REST) Update(ctx kapi.Context, obj runtime.Object) (runtime.Object, bool, error) {
roleBinding, ok := obj.(*authorizationapi.RoleBinding)
if !ok {
return nil, false, fmt.Errorf("not a roleBinding: %#v", obj)
}
if !kapi.ValidNamespace(ctx, &roleBinding.ObjectMeta) {
return nil, false, kerrors.NewConflict("roleBinding", roleBinding.Namespace, fmt.Errorf("RoleBinding.Namespace does not match the provided context"))
}
if errs := validation.ValidateRoleBinding(roleBinding); len(errs) > 0 {
return nil, false, kerrors.NewInvalid("roleBinding", roleBinding.Name, errs)
}
err := r.registry.UpdateRoleBinding(ctx, roleBinding)
if err != nil {
return nil, false, err
}
return roleBinding, false, nil
}