Skip to content

Commit

Permalink
[CLOUDGA-13804] Users CLI support (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshitha-yb authored May 23, 2023
1 parent 6b8bf67 commit ad9bc77
Show file tree
Hide file tree
Showing 12 changed files with 577 additions and 18 deletions.
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/yugabyte/ybm-cli/cmd/role"
"github.com/yugabyte/ybm-cli/cmd/signup"
"github.com/yugabyte/ybm-cli/cmd/tools"
"github.com/yugabyte/ybm-cli/cmd/user"
"github.com/yugabyte/ybm-cli/cmd/util"
"github.com/yugabyte/ybm-cli/cmd/vpc"

Expand Down Expand Up @@ -129,6 +130,7 @@ func init() {
rootCmd.AddCommand(region.CloudRegionsCmd)
rootCmd.AddCommand(role.RoleCmd)
rootCmd.AddCommand(api_key.ApiKeyCmd)
rootCmd.AddCommand(user.UserCmd)
util.AddCommandIfFeatureFlag(rootCmd, tools.ToolsCmd, util.TOOLS)
util.AddCommandIfFeatureFlag(rootCmd, cdc.CdcCmd, util.CDC)

Expand Down
222 changes: 222 additions & 0 deletions cmd/user/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
// Licensed to Yugabyte, Inc. under one or more contributor license
// agreements. See the NOTICE file distributed with this work for
// additional information regarding copyright ownership. Yugabyte
// licenses this file to you 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.

package user

import (
"fmt"
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/yugabyte/ybm-cli/cmd/util"
ybmAuthClient "github.com/yugabyte/ybm-cli/internal/client"
"github.com/yugabyte/ybm-cli/internal/formatter"
ybmclient "github.com/yugabyte/yugabytedb-managed-go-client-internal"
)

var UserCmd = &cobra.Command{
Use: "user",
Short: "Manage users",
Long: "Manage users in your YugabyteDB Managed account",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

var listUsersCmd = &cobra.Command{
Use: "list",
Short: "List users",
Long: `List users in your YugabyteDB Managed account`,
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
logrus.Fatalf("could not initiate api client: %s", err.Error())
}
authApi.GetInfo("", "")

userListRequest := authApi.ListAccountUsers()

// if user filters by email, add it to the request
email, _ := cmd.Flags().GetString("email")
if email != "" {
userListRequest = userListRequest.Email(email)
}

resp, r, err := userListRequest.Execute()

if err != nil {
logrus.Debugf("Full HTTP response: %v", r)
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err))
}

userCtx := formatter.Context{
Output: os.Stdout,
Format: formatter.NewUserFormat(viper.GetString("output")),
}

if len(resp.GetData()) < 1 {
logrus.Info("No users found")
return
}

formatter.UserWrite(userCtx, resp.GetData())
},
}

var inviteUserCmd = &cobra.Command{
Use: "invite",
Short: "Invite a user",
Long: "Invite a user to your YugabyteDB Managed account",
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
logrus.Fatalf("could not initiate api client: %s", err.Error())
}
authApi.GetInfo("", "")

email, _ := cmd.Flags().GetString("email")
roleName, _ := cmd.Flags().GetString("role-name")

usersSpec, err := authApi.CreateBatchInviteUserSpec(email, roleName)
if err != nil {
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err))
}

resp, r, err := authApi.BatchInviteAccountUsers().BatchInviteUserSpec(*usersSpec).Execute()
if err != nil {
logrus.Debugf("Full HTTP response: %v", r)
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err))
}

if resp.Data.GetUserList()[0].GetIsSuccessful() {
email := resp.Data.GetUserList()[0].GetInviteUserData().Spec.GetEmail()
role := resp.Data.GetUserList()[0].GetInviteUserData().Info.GetRoleList()[0].GetRoles()[0].Info.GetDisplayName()
fmt.Printf("The user %s has been successfully invited with role: %s.\n", formatter.Colorize(email, formatter.GREEN_COLOR), formatter.Colorize(role, formatter.GREEN_COLOR))
} else {
fmt.Printf("%s \n", resp.Data.GetUserList()[0].GetErrorMessage())
}

},
}

var updateUserCmd = &cobra.Command{
Use: "update",
Short: "Modify role of a user",
Long: "Modify role of a user in your YugabyteDB Managed account",
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag("force", cmd.Flags().Lookup("force"))
email, _ := cmd.Flags().GetString("email")
err := util.ConfirmCommand(fmt.Sprintf("Are you sure you want to modify the role of user with %s: %s", "email", email), viper.GetBool("force"))
if err != nil {
logrus.Fatal(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
logrus.Fatalf("could not initiate api client: %s", err.Error())
}
authApi.GetInfo("", "")

email, _ := cmd.Flags().GetString("email")
userId, err := authApi.GetUserIdByEmail(email)
if err != nil {
logrus.Infof("Could not find user with email: %s.\n", email)
logrus.Fatal(err)
}

roleName, _ := cmd.Flags().GetString("role-name")
roleId, err := authApi.GetRoleIdByName(roleName)
if err != nil {
logrus.Fatal(err)
}

modifyUserRoleRequest := *ybmclient.NewModifyUserRoleRequest(roleId)

request := authApi.ModifyUserRole(userId)
request = request.ModifyUserRoleRequest(modifyUserRoleRequest)

r, err := request.Execute()

if err != nil {
logrus.Debugf("Full HTTP response: %v", r)
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err))
}

fmt.Printf("The role of user %s has been successfully modified.\n", formatter.Colorize(email, formatter.GREEN_COLOR))
},
}

var deleteUserCmd = &cobra.Command{
Use: "delete",
Short: "Delete a user",
Long: "Delete a user from your YugabyteDB Managed account",
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag("force", cmd.Flags().Lookup("force"))
email, _ := cmd.Flags().GetString("email")
err := util.ConfirmCommand(fmt.Sprintf("Are you sure you want to delete the user with %s: %s", "email", email), viper.GetBool("force"))
if err != nil {
logrus.Fatal(err)
}
},
Run: func(cmd *cobra.Command, args []string) {
authApi, err := ybmAuthClient.NewAuthApiClient()
if err != nil {
logrus.Fatalf("could not initiate api client: %s", err.Error())
}
authApi.GetInfo("", "")

email, _ := cmd.Flags().GetString("email")
userId, err := authApi.GetUserIdByEmail(email)
if err != nil {
logrus.Infof("Could not find user with email: %s.\n", email)
logrus.Fatal(err)
}

response, err := authApi.RemoveAccountUser(userId).Execute()

if err != nil {
logrus.Debugf("Full HTTP response: %v", response)
logrus.Fatalf(ybmAuthClient.GetApiErrorDetails(err))
}

fmt.Printf("The user %s has been successfully deleted.\n", formatter.Colorize(email, formatter.GREEN_COLOR))
},
}

func init() {
UserCmd.AddCommand(listUsersCmd)
listUsersCmd.Flags().String("email", "", "[OPTIONAL] To filter by user email.")

UserCmd.AddCommand(inviteUserCmd)
inviteUserCmd.Flags().String("email", "", "[REQUIRED] The email of the user to be invited.")
inviteUserCmd.MarkFlagRequired("email")
inviteUserCmd.Flags().String("role-name", "", "[REQUIRED] The name of the role to be assigned to the user.")
inviteUserCmd.MarkFlagRequired("role-name")

UserCmd.AddCommand(updateUserCmd)
updateUserCmd.Flags().String("email", "", "[REQUIRED] The email of the user whose role is to be modified.")
updateUserCmd.MarkFlagRequired("email")
updateUserCmd.Flags().String("role-name", "", "[REQUIRED] The name of the role to be assigned to the user.")
updateUserCmd.MarkFlagRequired("role-name")
updateUserCmd.Flags().BoolP("force", "f", false, "Bypass the prompt for non-interactive usage")

UserCmd.AddCommand(deleteUserCmd)
deleteUserCmd.Flags().String("email", "", "[REQUIRED] The email of the user.")
deleteUserCmd.MarkFlagRequired("email")
deleteUserCmd.Flags().BoolP("force", "f", false, "Bypass the prompt for non-interactive usage")
}
1 change: 1 addition & 0 deletions docs/ybm.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ ybm [flags]
* [ybm role](ybm_role.md) - Manage roles
* [ybm signup](ybm_signup.md) - Open a browser to sign up for YugabyteDB Managed
* [ybm tools](ybm_tools.md) - Tools command
* [ybm user](ybm_user.md) - Manage users
* [ybm vpc](ybm_vpc.md) - Manage VPCs

39 changes: 39 additions & 0 deletions docs/ybm_user.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## ybm user

Manage users

### Synopsis

Manage users in your YugabyteDB Managed account

```
ybm user [flags]
```

### Options

```
-h, --help help for user
```

### Options inherited from parent commands

```
-a, --apiKey string YBM Api Key
--config string config file (default is $HOME/.ybm-cli.yaml)
--debug Use debug mode, same as --logLevel debug
-l, --logLevel string Select the desired log level format(info). Default to info
--no-color Disable colors in output , default to false
-o, --output string Select the desired output format (table, json, pretty). Default to table
--timeout duration Wait command timeout, example: 5m, 1h. (default 168h0m0s)
--wait Wait until the task is completed, otherwise it will exit immediately, default to false
```

### SEE ALSO

* [ybm](ybm.md) - ybm - Effortlessly manage your DB infrastructure on YugabyteDB Managed (DBaaS) from command line!
* [ybm user delete](ybm_user_delete.md) - Delete a user
* [ybm user invite](ybm_user_invite.md) - Invite a user
* [ybm user list](ybm_user_list.md) - List users
* [ybm user update](ybm_user_update.md) - Modify role of a user

37 changes: 37 additions & 0 deletions docs/ybm_user_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## ybm user delete

Delete a user

### Synopsis

Delete a user from your YugabyteDB Managed account

```
ybm user delete [flags]
```

### Options

```
--email string [REQUIRED] The email of the user.
-f, --force Bypass the prompt for non-interactive usage
-h, --help help for delete
```

### Options inherited from parent commands

```
-a, --apiKey string YBM Api Key
--config string config file (default is $HOME/.ybm-cli.yaml)
--debug Use debug mode, same as --logLevel debug
-l, --logLevel string Select the desired log level format(info). Default to info
--no-color Disable colors in output , default to false
-o, --output string Select the desired output format (table, json, pretty). Default to table
--timeout duration Wait command timeout, example: 5m, 1h. (default 168h0m0s)
--wait Wait until the task is completed, otherwise it will exit immediately, default to false
```

### SEE ALSO

* [ybm user](ybm_user.md) - Manage users

37 changes: 37 additions & 0 deletions docs/ybm_user_invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## ybm user invite

Invite a user

### Synopsis

Invite a user to your YugabyteDB Managed account

```
ybm user invite [flags]
```

### Options

```
--email string [REQUIRED] The email of the user to be invited.
-h, --help help for invite
--role-name string [REQUIRED] The name of the role to be assigned to the user.
```

### Options inherited from parent commands

```
-a, --apiKey string YBM Api Key
--config string config file (default is $HOME/.ybm-cli.yaml)
--debug Use debug mode, same as --logLevel debug
-l, --logLevel string Select the desired log level format(info). Default to info
--no-color Disable colors in output , default to false
-o, --output string Select the desired output format (table, json, pretty). Default to table
--timeout duration Wait command timeout, example: 5m, 1h. (default 168h0m0s)
--wait Wait until the task is completed, otherwise it will exit immediately, default to false
```

### SEE ALSO

* [ybm user](ybm_user.md) - Manage users

36 changes: 36 additions & 0 deletions docs/ybm_user_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## ybm user list

List users

### Synopsis

List users in your YugabyteDB Managed account

```
ybm user list [flags]
```

### Options

```
--email string [OPTIONAL] To filter by user email.
-h, --help help for list
```

### Options inherited from parent commands

```
-a, --apiKey string YBM Api Key
--config string config file (default is $HOME/.ybm-cli.yaml)
--debug Use debug mode, same as --logLevel debug
-l, --logLevel string Select the desired log level format(info). Default to info
--no-color Disable colors in output , default to false
-o, --output string Select the desired output format (table, json, pretty). Default to table
--timeout duration Wait command timeout, example: 5m, 1h. (default 168h0m0s)
--wait Wait until the task is completed, otherwise it will exit immediately, default to false
```

### SEE ALSO

* [ybm user](ybm_user.md) - Manage users

Loading

0 comments on commit ad9bc77

Please sign in to comment.