forked from openshift/origin
/
policy.go
83 lines (67 loc) · 2.12 KB
/
policy.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
79
80
81
82
83
package test
import (
"errors"
"fmt"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
klabels "github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
)
type PolicyRegistry struct {
Err error
MasterNamespace string
Policies []authorizationapi.Policy
DeletedPolicyName string
}
// ListPolicies obtains list of policys that match a selector.
func (r *PolicyRegistry) ListPolicies(ctx kapi.Context, labels, fields klabels.Selector) (*authorizationapi.PolicyList, error) {
if r.Err != nil {
return nil, r.Err
}
namespace := kapi.Namespace(ctx)
if len(namespace) == 0 {
return nil, errors.New("invalid request. Namespace parameter required.")
}
list := make([]authorizationapi.Policy, 0)
for _, curr := range r.Policies {
if curr.Namespace == namespace {
list = append(list, curr)
}
}
return &authorizationapi.PolicyList{
Items: list,
},
r.Err
}
// GetPolicy retrieves a specific policy.
func (r *PolicyRegistry) GetPolicy(ctx kapi.Context, id string) (*authorizationapi.Policy, error) {
if r.Err != nil {
return nil, r.Err
}
namespace := kapi.Namespace(ctx)
if len(namespace) == 0 {
return nil, errors.New("invalid request. Namespace parameter required.")
}
for _, curr := range r.Policies {
if curr.Namespace == namespace && id == curr.Name {
return &curr, nil
}
}
return nil, fmt.Errorf("Policy %v::%v not found", namespace, id)
}
// CreatePolicy creates a new policy.
func (r *PolicyRegistry) CreatePolicy(ctx kapi.Context, policy *authorizationapi.Policy) error {
return r.Err
}
// UpdatePolicy updates a policy.
func (r *PolicyRegistry) UpdatePolicy(ctx kapi.Context, policy *authorizationapi.Policy) error {
return r.Err
}
// DeletePolicy deletes a policy.
func (r *PolicyRegistry) DeletePolicy(ctx kapi.Context, id string) error {
r.DeletedPolicyName = id
return r.Err
}
func (r *PolicyRegistry) WatchPolicies(ctx kapi.Context, label, field klabels.Selector, resourceVersion string) (watch.Interface, error) {
return nil, r.Err
}