forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
changemembership.go
142 lines (109 loc) · 3.04 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
package groups
import (
"errors"
"fmt"
"io"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/util/sets"
"github.com/spf13/cobra"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
const (
AddRecommendedName = "add-users"
addLong = `
Add users to a group.
This command will append unique users to the list of members for a group.`
addExample = ` # Add user1 and user2 to my-group
$ %[1]s my-group user1 user2`
)
const (
RemoveRecommendedName = "remove-users"
removeLong = `
Remove users from a group.
This command will remove users from the list of members for a group.`
removeExample = ` # Remove user1 and user2 from my-group
$ %[1]s my-group user1 user2`
)
type GroupModificationOptions struct {
GroupClient client.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:]...)
osClient, _, err := f.Clients()
if err != nil {
return err
}
o.GroupClient = osClient.Groups()
return nil
}
func (o *GroupModificationOptions) AddUsers() error {
group, err := o.GroupClient.Get(o.Group)
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)
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
}