/
group.go
138 lines (119 loc) · 3.21 KB
/
group.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* This file is part of the Dicot project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2017 Red Hat, Inc.
*
*/
package identity
import (
"k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"github.com/dicot-project/dicot-api/pkg/api/identity/v1"
)
func NewGroupClient(cl rest.Interface, namespace string) GroupInterface {
return &groups{cl: cl, ns: namespace}
}
type groups struct {
cl rest.Interface
ns string
}
type GroupGetter interface {
Groups(namespace string) GroupInterface
}
type GroupInterface interface {
Create(obj *v1.Group) (*v1.Group, error)
Update(obj *v1.Group) (*v1.Group, error)
Delete(name string, options *meta_v1.DeleteOptions) error
Get(name string) (*v1.Group, error)
GetByUID(id string) (*v1.Group, error)
Exists(name string) (bool, error)
List() (*v1.GroupList, error)
NewListWatch() *cache.ListWatch
}
func (pc *groups) Create(obj *v1.Group) (*v1.Group, error) {
var result v1.Group
err := pc.cl.Post().
Namespace(pc.ns).Resource("groups").
Body(obj).Do().Into(&result)
if err != nil {
return nil, err
}
return &result, nil
}
func (pc *groups) Update(obj *v1.Group) (*v1.Group, error) {
var result v1.Group
name := obj.GetObjectMeta().GetName()
err := pc.cl.Put().
Namespace(pc.ns).Resource("groups").
Name(name).Body(obj).Do().Into(&result)
if err != nil {
return nil, err
}
return &result, nil
}
func (pc *groups) Delete(name string, options *meta_v1.DeleteOptions) error {
return pc.cl.Delete().
Namespace(pc.ns).Resource("groups").
Name(name).Body(options).Do().
Error()
}
func (pc *groups) Get(name string) (*v1.Group, error) {
var result v1.Group
err := pc.cl.Get().
Namespace(pc.ns).Resource("groups").
Name(name).Do().Into(&result)
if err != nil {
return nil, err
}
return &result, nil
}
func (pc *groups) GetByUID(uid string) (*v1.Group, error) {
list, err := pc.List()
if err != nil {
return nil, err
}
for _, group := range list.Items {
if string(group.ObjectMeta.UID) == uid {
return &group, nil
}
}
return nil, errors.NewNotFound(v1.Resource("group"), uid)
}
func (pc *groups) Exists(name string) (bool, error) {
_, err := pc.Get(name)
if err != nil {
if errors.IsNotFound(err) {
return false, nil
}
return false, err
}
return true, nil
}
func (pc *groups) List() (*v1.GroupList, error) {
var result v1.GroupList
err := pc.cl.Get().
Namespace(pc.ns).Resource("groups").
Do().Into(&result)
if err != nil {
return nil, err
}
return &result, nil
}
func (pc *groups) NewListWatch() *cache.ListWatch {
return cache.NewListWatchFromClient(pc.cl, "groups", pc.ns, fields.Everything())
}