-
Notifications
You must be signed in to change notification settings - Fork 10
/
accounts.go
193 lines (173 loc) · 6.84 KB
/
accounts.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cluster
import (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericiooptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/templates"
"github.com/apecloud/kbcli/pkg/cmd/accounts"
"github.com/apecloud/kbcli/pkg/types"
"github.com/apecloud/kbcli/pkg/util"
)
var (
createUserExamples = templates.Examples(`
# create account with password
kbcli cluster create-account CLUSTERNAME --component COMPNAME --name USERNAME --password PASSWD
# create account without password
kbcli cluster create-account CLUSTERNAME --component COMPNAME --name USERNAME
# create account with default component
kbcli cluster create-account CLUSTERNAME --name USERNAME
# create account for instance
kbcli cluster create-account --instance INSTANCE --name USERNAME
`)
deleteUserExamples = templates.Examples(`
# delete account by name
kbcli cluster delete-account CLUSTERNAME --component COMPNAME --name USERNAME
# delete account with default component
kbcli cluster delete-account CLUSTERNAME --name USERNAME
# delete account for instance
kbcli cluster delete-account --instance INSTANCE --name USERNAME
`)
descUserExamples = templates.Examples(`
# describe account and show role information
kbcli cluster describe-account CLUSTERNAME --component COMPNAME --name USERNAME
# describe account with default component
kbcli cluster describe-account CLUSTERNAME --name USERNAME
# describe account for instance
kbcli cluster describe-account --instance INSTANCE --name USERNAME
`)
listUsersExample = templates.Examples(`
# list all users for component
kbcli cluster list-accounts CLUSTERNAME --component COMPNAME
# list all users with default component
kbcli cluster list-accounts CLUSTERNAME
# list all users from instance
kbcli cluster list-accounts --instance INSTANCE
`)
grantRoleExamples = templates.Examples(`
# grant role to user
kbcli cluster grant-role CLUSTERNAME --component COMPNAME --name USERNAME --role ROLENAME
# grant role to user with default component
kbcli cluster grant-role CLUSTERNAME --name USERNAME --role ROLENAME
# grant role to user for instance
kbcli cluster grant-role --instance INSTANCE --name USERNAME --role ROLENAME
`)
revokeRoleExamples = templates.Examples(`
# revoke role from user
kbcli cluster revoke-role CLUSTERNAME --component COMPNAME --name USERNAME --role ROLENAME
# revoke role from user with default component
kbcli cluster revoke-role CLUSTERNAME --name USERNAME --role ROLENAME
# revoke role from user for instance
kbcli cluster revoke-role --instance INSTANCE --name USERNAME --role ROLENAME
`)
)
func NewCreateAccountCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := accounts.NewCreateUserOptions(f, streams)
cmd := &cobra.Command{
Use: "create-account",
Short: "Create account for a cluster",
Example: createUserExamples,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()),
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.AccountBaseOptions.Validate(args))
cmdutil.CheckErr(o.Exec())
},
}
o.AddFlags(cmd)
return cmd
}
func NewDeleteAccountCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := accounts.NewDeleteUserOptions(f, streams)
cmd := &cobra.Command{
Use: "delete-account",
Short: "Delete account for a cluster",
Example: deleteUserExamples,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()),
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.AccountBaseOptions.Validate(args))
cmdutil.CheckErr(o.Exec())
},
}
o.AddFlags(cmd)
cmd.Flags().BoolVar(&o.AutoApprove, "auto-approve", false, "Skip interactive approval before deleting account")
return cmd
}
func NewDescAccountCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := accounts.NewDescribeUserOptions(f, streams)
cmd := &cobra.Command{
Use: "describe-account",
Short: "Describe account roles and related information",
Example: descUserExamples,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()),
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.AccountBaseOptions.Validate(args))
cmdutil.CheckErr(o.Validate())
cmdutil.CheckErr(o.Complete())
cmdutil.CheckErr(o.Run())
},
}
o.AddFlags(cmd)
return cmd
}
func NewListAccountsCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := accounts.NewListUserOptions(f, streams)
cmd := &cobra.Command{
Use: "list-accounts",
Short: "List accounts for a cluster",
Aliases: []string{"ls-accounts"},
Example: listUsersExample,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()),
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.Validate(args))
cmdutil.CheckErr(o.Exec())
},
}
o.AddFlags(cmd)
return cmd
}
func NewGrantOptions(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := accounts.NewGrantOptions(f, streams)
cmd := &cobra.Command{
Use: "grant-role",
Short: "Grant role to account",
Aliases: []string{"grant", "gr"},
Example: grantRoleExamples,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()),
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.AccountBaseOptions.Validate(args))
cmdutil.CheckErr(o.Exec())
},
}
o.AddFlags(cmd)
return cmd
}
func NewRevokeOptions(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := accounts.NewRevokeOptions(f, streams)
cmd := &cobra.Command{
Use: "revoke-role",
Short: "Revoke role from account",
Aliases: []string{"revoke", "rv"},
Example: revokeRoleExamples,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()),
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.Validate(args))
cmdutil.CheckErr(o.Complete(f))
cmdutil.CheckErr(o.Run(cmd, f, streams))
},
}
o.AddFlags(cmd)
return cmd
}