github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/cluster/accounts.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package cluster 21 22 import ( 23 "github.com/spf13/cobra" 24 "k8s.io/cli-runtime/pkg/genericiooptions" 25 cmdutil "k8s.io/kubectl/pkg/cmd/util" 26 "k8s.io/kubectl/pkg/util/templates" 27 28 "github.com/1aal/kubeblocks/pkg/cli/cmd/accounts" 29 "github.com/1aal/kubeblocks/pkg/cli/types" 30 "github.com/1aal/kubeblocks/pkg/cli/util" 31 ) 32 33 var ( 34 createUserExamples = templates.Examples(` 35 # create account with password 36 kbcli cluster create-account CLUSTERNAME --component COMPNAME --name USERNAME --password PASSWD 37 # create account without password 38 kbcli cluster create-account CLUSTERNAME --component COMPNAME --name USERNAME 39 # create account with default component 40 kbcli cluster create-account CLUSTERNAME --name USERNAME 41 # create account for instance 42 kbcli cluster create-account --instance INSTANCE --name USERNAME 43 `) 44 45 deleteUserExamples = templates.Examples(` 46 # delete account by name 47 kbcli cluster delete-account CLUSTERNAME --component COMPNAME --name USERNAME 48 # delete account with default component 49 kbcli cluster delete-account CLUSTERNAME --name USERNAME 50 # delete account for instance 51 kbcli cluster delete-account --instance INSTANCE --name USERNAME 52 `) 53 54 descUserExamples = templates.Examples(` 55 # describe account and show role information 56 kbcli cluster describe-account CLUSTERNAME --component COMPNAME --name USERNAME 57 # describe account with default component 58 kbcli cluster describe-account CLUSTERNAME --name USERNAME 59 # describe account for instance 60 kbcli cluster describe-account --instance INSTANCE --name USERNAME 61 `) 62 63 listUsersExample = templates.Examples(` 64 # list all users for component 65 kbcli cluster list-accounts CLUSTERNAME --component COMPNAME 66 # list all users with default component 67 kbcli cluster list-accounts CLUSTERNAME 68 # list all users from instance 69 kbcli cluster list-accounts --instance INSTANCE 70 `) 71 grantRoleExamples = templates.Examples(` 72 # grant role to user 73 kbcli cluster grant-role CLUSTERNAME --component COMPNAME --name USERNAME --role ROLENAME 74 # grant role to user with default component 75 kbcli cluster grant-role CLUSTERNAME --name USERNAME --role ROLENAME 76 # grant role to user for instance 77 kbcli cluster grant-role --instance INSTANCE --name USERNAME --role ROLENAME 78 `) 79 revokeRoleExamples = templates.Examples(` 80 # revoke role from user 81 kbcli cluster revoke-role CLUSTERNAME --component COMPNAME --name USERNAME --role ROLENAME 82 # revoke role from user with default component 83 kbcli cluster revoke-role CLUSTERNAME --name USERNAME --role ROLENAME 84 # revoke role from user for instance 85 kbcli cluster revoke-role --instance INSTANCE --name USERNAME --role ROLENAME 86 `) 87 ) 88 89 func NewCreateAccountCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command { 90 o := accounts.NewCreateUserOptions(f, streams) 91 cmd := &cobra.Command{ 92 Use: "create-account", 93 Short: "Create account for a cluster", 94 Example: createUserExamples, 95 ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()), 96 Run: func(cmd *cobra.Command, args []string) { 97 cmdutil.CheckErr(o.Validate(args)) 98 cmdutil.CheckErr(o.Complete(f)) 99 cmdutil.CheckErr(o.Run(cmd, f, streams)) 100 }, 101 } 102 o.AddFlags(cmd) 103 return cmd 104 } 105 106 func NewDeleteAccountCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command { 107 o := accounts.NewDeleteUserOptions(f, streams) 108 cmd := &cobra.Command{ 109 Use: "delete-account", 110 Short: "Delete account for a cluster", 111 Example: deleteUserExamples, 112 ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()), 113 Run: func(cmd *cobra.Command, args []string) { 114 cmdutil.CheckErr(o.Validate(args)) 115 cmdutil.CheckErr(o.Complete(f)) 116 cmdutil.CheckErr(o.Run(cmd, f, streams)) 117 }, 118 } 119 o.AddFlags(cmd) 120 cmd.Flags().BoolVar(&o.AutoApprove, "auto-approve", false, "Skip interactive approval before deleting account") 121 return cmd 122 } 123 124 func NewDescAccountCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command { 125 o := accounts.NewDescribeUserOptions(f, streams) 126 cmd := &cobra.Command{ 127 Use: "describe-account", 128 Short: "Describe account roles and related information", 129 Example: descUserExamples, 130 ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()), 131 Run: func(cmd *cobra.Command, args []string) { 132 cmdutil.CheckErr(o.Validate(args)) 133 cmdutil.CheckErr(o.Complete(f)) 134 cmdutil.CheckErr(o.Run(cmd, f, streams)) 135 }, 136 } 137 o.AddFlags(cmd) 138 return cmd 139 } 140 141 func NewListAccountsCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command { 142 o := accounts.NewListUserOptions(f, streams) 143 144 cmd := &cobra.Command{ 145 Use: "list-accounts", 146 Short: "List accounts for a cluster", 147 Aliases: []string{"ls-accounts"}, 148 Example: listUsersExample, 149 ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()), 150 Run: func(cmd *cobra.Command, args []string) { 151 cmdutil.CheckErr(o.Validate(args)) 152 cmdutil.CheckErr(o.Complete(f)) 153 cmdutil.CheckErr(o.Run(cmd, f, streams)) 154 }, 155 } 156 o.AddFlags(cmd) 157 return cmd 158 } 159 160 func NewGrantOptions(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command { 161 o := accounts.NewGrantOptions(f, streams) 162 163 cmd := &cobra.Command{ 164 Use: "grant-role", 165 Short: "Grant role to account", 166 Aliases: []string{"grant", "gr"}, 167 Example: grantRoleExamples, 168 ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()), 169 Run: func(cmd *cobra.Command, args []string) { 170 cmdutil.CheckErr(o.Validate(args)) 171 cmdutil.CheckErr(o.Complete(f)) 172 cmdutil.CheckErr(o.Run(cmd, f, streams)) 173 }, 174 } 175 o.AddFlags(cmd) 176 return cmd 177 } 178 179 func NewRevokeOptions(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command { 180 o := accounts.NewRevokeOptions(f, streams) 181 182 cmd := &cobra.Command{ 183 Use: "revoke-role", 184 Short: "Revoke role from account", 185 Aliases: []string{"revoke", "rv"}, 186 Example: revokeRoleExamples, 187 ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()), 188 Run: func(cmd *cobra.Command, args []string) { 189 cmdutil.CheckErr(o.Validate(args)) 190 cmdutil.CheckErr(o.Complete(f)) 191 cmdutil.CheckErr(o.Run(cmd, f, streams)) 192 }, 193 } 194 o.AddFlags(cmd) 195 return cmd 196 }