forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
changemembership.go
147 lines (113 loc) · 3.35 KB
/
changemembership.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
139
140
141
142
143
144
145
146
147
package groups
import (
"errors"
"fmt"
"io"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"github.com/spf13/cobra"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
usertypedclient "github.com/openshift/origin/pkg/user/generated/internalclientset/typed/user/internalversion"
)
const (
AddRecommendedName = "add-users"
RemoveRecommendedName = "remove-users"
)
var (
addLong = templates.LongDesc(`
Add users to a group.
This command will append unique users to the list of members for a group.`)
addExample = templates.Examples(`
# Add user1 and user2 to my-group
%[1]s my-group user1 user2`)
removeLong = templates.LongDesc(`
Remove users from a group.
This command will remove users from the list of members for a group.`)
removeExample = templates.Examples(`
# Remove user1 and user2 from my-group
%[1]s my-group user1 user2`)
)
type GroupModificationOptions struct {
GroupClient usertypedclient.GroupInterface
Group string
Users []string
}
func NewCmdAddUsers(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &GroupModificationOptions{}
cmd := &cobra.Command{
Use: name + " GROUP USER [USER ...]",
Short: "Add users to a group",
Long: addLong,
Example: fmt.Sprintf(addExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
if err := options.Complete(f, args); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
kcmdutil.CheckErr(options.AddUsers())
},
}
return cmd
}
func NewCmdRemoveUsers(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
options := &GroupModificationOptions{}
cmd := &cobra.Command{
Use: name + " GROUP USER [USER ...]",
Short: "Remove users from a group",
Long: removeLong,
Example: fmt.Sprintf(removeExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
if err := options.Complete(f, args); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
kcmdutil.CheckErr(options.RemoveUsers())
},
}
return cmd
}
func (o *GroupModificationOptions) Complete(f *clientcmd.Factory, args []string) error {
if len(args) < 2 {
return errors.New("you must specify at least two arguments: GROUP USER [USER ...]")
}
o.Group = args[0]
o.Users = append(o.Users, args[1:]...)
userClient, err := f.OpenshiftInternalUserClient()
if err != nil {
return err
}
o.GroupClient = userClient.User().Groups()
return nil
}
func (o *GroupModificationOptions) AddUsers() error {
group, err := o.GroupClient.Get(o.Group, metav1.GetOptions{})
if err != nil {
return err
}
existingUsers := sets.NewString(group.Users...)
for _, user := range o.Users {
if existingUsers.Has(user) {
continue
}
group.Users = append(group.Users, user)
}
_, err = o.GroupClient.Update(group)
return err
}
func (o *GroupModificationOptions) RemoveUsers() error {
group, err := o.GroupClient.Get(o.Group, metav1.GetOptions{})
if err != nil {
return err
}
toDelete := sets.NewString(o.Users...)
newUsers := []string{}
for _, user := range group.Users {
if toDelete.Has(user) {
continue
}
newUsers = append(newUsers, user)
}
group.Users = newUsers
_, err = o.GroupClient.Update(group)
return err
}