forked from openshift/origin
/
etcd.go
71 lines (60 loc) · 2.24 KB
/
etcd.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
package etcd
import (
"errors"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kerrs "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/generic"
etcdgeneric "github.com/GoogleCloudPlatform/kubernetes/pkg/registry/generic/etcd"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
"github.com/openshift/origin/pkg/user/api"
"github.com/openshift/origin/pkg/user/api/validation"
"github.com/openshift/origin/pkg/user/registry/user"
)
// rest implements a RESTStorage for users against etcd
type REST struct {
etcdgeneric.Etcd
}
const EtcdPrefix = "/registry/users"
// NewREST returns a RESTStorage object that will work against users
func NewREST(h tools.EtcdHelper) *REST {
store := &etcdgeneric.Etcd{
NewFunc: func() runtime.Object { return &api.User{} },
NewListFunc: func() runtime.Object { return &api.UserList{} },
KeyRootFunc: func(ctx kapi.Context) string {
return EtcdPrefix
},
KeyFunc: func(ctx kapi.Context, name string) (string, error) {
return etcdgeneric.NoNamespaceKeyFunc(ctx, EtcdPrefix, name)
},
ObjectNameFunc: func(obj runtime.Object) (string, error) {
return obj.(*api.User).Name, nil
},
PredicateFunc: func(label labels.Selector, field fields.Selector) generic.Matcher {
return user.MatchUser(label, field)
},
EndpointName: "users",
Helper: h,
}
store.CreateStrategy = user.Strategy
store.UpdateStrategy = user.Strategy
return &REST{*store}
}
// Get retrieves the item from etcd.
func (r *REST) Get(ctx kapi.Context, name string) (runtime.Object, error) {
// "~" means the currently authenticated user
if name == "~" {
user, ok := kapi.UserFrom(ctx)
if !ok || user.GetName() == "" {
return nil, kerrs.NewForbidden("user", "~", errors.New("Requests to ~ must be authenticated"))
}
name = user.GetName()
}
if ok, details := validation.ValidateUserName(name, false); !ok {
return nil, fielderrors.NewFieldInvalid("metadata.name", name, details)
}
return r.Etcd.Get(ctx, name)
}