Skip to content

Commit

Permalink
refactor: update user and user group queries to use machinery
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Dec 5, 2023
1 parent f1a8c0d commit 0ddc43a
Show file tree
Hide file tree
Showing 22 changed files with 318 additions and 46 deletions.
179 changes: 164 additions & 15 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/uselagoon/lagoon-cli/pkg/output"
l "github.com/uselagoon/machinery/api/lagoon"
lclient "github.com/uselagoon/machinery/api/lagoon/client"
ls "github.com/uselagoon/machinery/api/schema"
)

// ListFlags .
Expand Down Expand Up @@ -366,24 +367,168 @@ var listTasksCmd = &cobra.Command{
}

var listUsersCmd = &cobra.Command{
//@TODO: once individual user interaction comes in, this will need to be adjusted
Use: "users",
Aliases: []string{"u"},
Short: "List all users in groups (alias: u)",
Long: `List all users in groups in lagoon, this only shows users that are in groups.`,
Run: func(cmd *cobra.Command, args []string) {
returnedJSON, err := uClient.ListUsers(groupName)
handleError(err)
Use: "group-users",
Aliases: []string{"gu"},
Short: "List all users in groups",
Long: `List all users in groups in lagoon, this only shows users that are in groups.
If no group name is provided, all groups are queried.
Without a group name, this query may time out in large Lagoon installs.`,
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(cmdLagoon)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
groupName, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
&token,
debug)
data := []output.Data{}
if groupName != "" {
// if a groupName is provided, use the groupbyname resolver
groupMembers, err := l.ListGroupMembers(context.TODO(), groupName, lc)
if err != nil {
return err
}
for _, member := range groupMembers.Members {
data = append(data, []string{
returnNonEmptyString(fmt.Sprintf("%s", groupMembers.ID)),
returnNonEmptyString(fmt.Sprintf("%s", groupMembers.Name)),
returnNonEmptyString(fmt.Sprintf("%s", member.User.Email)),
returnNonEmptyString(fmt.Sprintf("%s", member.Role)),
})
}
} else {
// otherwise allgroups query
groupMembers, err := l.ListAllGroupMembers(context.TODO(), groupName, lc)
if err != nil {
return err
}
for _, group := range *groupMembers {
for _, member := range group.Members {
data = append(data, []string{
returnNonEmptyString(fmt.Sprintf("%s", group.ID)),
returnNonEmptyString(fmt.Sprintf("%s", group.Name)),
returnNonEmptyString(fmt.Sprintf("%s", member.User.Email)),
returnNonEmptyString(fmt.Sprintf("%s", member.Role)),
})
}
}
}
dataMain := output.Table{
Header: []string{"ID", "GroupName", "Email", "Role"},
Data: data,
}
output.RenderOutput(dataMain, outputOptions)
return nil
},
}

var dataMain output.Table
err = json.Unmarshal([]byte(returnedJSON), &dataMain)
handleError(err)
if len(dataMain.Data) == 0 {
output.RenderInfo("There are no users in any groups", outputOptions)
os.Exit(0)
var listAllUsersCmd = &cobra.Command{
Use: "all-users",
Aliases: []string{"au"},
Short: "List all users",
Long: `List all users.
This query can take a long time to run if there are a lot of users.`,
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(cmdLagoon)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
emailAddress, err := cmd.Flags().GetString("email-address")
if err != nil {
return err
}
current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
&token,
debug)
allUsers, err := l.AllUsers(context.TODO(), ls.AllUsersFilter{
Email: emailAddress,
}, lc)
if err != nil {
return err
}
data := []output.Data{}
for _, user := range *allUsers {
data = append(data, []string{
returnNonEmptyString(fmt.Sprintf("%s", user.ID)),
returnNonEmptyString(fmt.Sprintf("%s", user.Email)),
returnNonEmptyString(fmt.Sprintf("%s", user.FirstName)),
returnNonEmptyString(fmt.Sprintf("%s", user.LastName)),
returnNonEmptyString(fmt.Sprintf("%s", user.Comment)),
})
}
dataMain := output.Table{
Header: []string{"ID", "Email", "FirstName", "LastName", "Comment"},
Data: data,
}
output.RenderOutput(dataMain, outputOptions)
return nil
},
}

var listUsersGroupsCmd = &cobra.Command{
Use: "user-groups",
Aliases: []string{"ug"},
Short: "List a single users groups and roles",
Long: `List a single users groups and roles`,
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(cmdLagoon)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
emailAddress, err := cmd.Flags().GetString("email-address")
if err != nil {
return err
}
if emailAddress == "" {
return fmt.Errorf("Missing arguments: email address is not defined")
}
current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
&token,
debug)
allUsers, err := l.GetUserByEmail(context.TODO(), emailAddress, lc)
if err != nil {
return err
}
data := []output.Data{}
for _, grouprole := range allUsers.GroupRoles {
data = append(data, []string{
returnNonEmptyString(fmt.Sprintf("%s", allUsers.ID)),
returnNonEmptyString(fmt.Sprintf("%s", allUsers.Email)),
returnNonEmptyString(fmt.Sprintf("%s", grouprole.Name)),
returnNonEmptyString(fmt.Sprintf("%s", grouprole.Role)),
})
}
dataMain := output.Table{
Header: []string{"ID", "Email", "GroupName", "GroupRole"},
Data: data,
}
output.RenderOutput(dataMain, outputOptions)
return nil
},
}

Expand Down Expand Up @@ -450,8 +595,12 @@ func init() {
listCmd.AddCommand(listInvokableTasks)
listCmd.AddCommand(listBackupsCmd)
listCmd.AddCommand(listDeployTargetConfigsCmd)
listCmd.AddCommand(listAllUsersCmd)
listCmd.AddCommand(listUsersGroupsCmd)
listAllUsersCmd.Flags().StringP("email-address", "E", "", "The email address of a user")
listUsersGroupsCmd.Flags().StringP("email-address", "E", "", "The email address of a user")
listCmd.Flags().BoolVarP(&listAllProjects, "all-projects", "", false, "All projects (if supported)")
listUsersCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list users in (if not specified, will default to all groups)")
listUsersCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list users in")
listGroupProjectsCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group to list projects in")
listVariablesCmd.Flags().BoolP("reveal", "", false, "Reveal the variable values")
}
2 changes: 1 addition & 1 deletion docs/commands/lagoon.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ lagoon [flags]
* [lagoon get](lagoon_get.md) - Get info on a resource
* [lagoon import](lagoon_import.md) - Import a config from a yaml file
* [lagoon kibana](lagoon_kibana.md) - Launch the kibana interface
* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications
* [lagoon login](lagoon_login.md) - Log into a Lagoon instance
* [lagoon retrieve](lagoon_retrieve.md) - Trigger a retrieval operation on backups
* [lagoon run](lagoon_run.md) - Run a task against an environment
Expand Down
8 changes: 5 additions & 3 deletions docs/commands/lagoon_list.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## lagoon list

List projects, deployments, variables or notifications
List projects, environments, deployments, variables or notifications

### Synopsis

List projects, deployments, variables or notifications
List projects, environments, deployments, variables or notifications

### Options

Expand Down Expand Up @@ -33,18 +33,20 @@ List projects, deployments, variables or notifications
### SEE ALSO

* [lagoon](lagoon.md) - Command line integration for Lagoon
* [lagoon list all-users](lagoon_list_all-users.md) - List all users
* [lagoon list backups](lagoon_list_backups.md) - List an environments backups
* [lagoon list deployments](lagoon_list_deployments.md) - List deployments for an environment (alias: d)
* [lagoon list deploytarget-configs](lagoon_list_deploytarget-configs.md) - List deploytarget configs for a project
* [lagoon list deploytargets](lagoon_list_deploytargets.md) - List all DeployTargets in Lagoon
* [lagoon list environments](lagoon_list_environments.md) - List environments for a project (alias: e)
* [lagoon list group-projects](lagoon_list_group-projects.md) - List projects in a group (alias: gp)
* [lagoon list group-users](lagoon_list_group-users.md) - List all users in groups
* [lagoon list groups](lagoon_list_groups.md) - List groups you have access to (alias: g)
* [lagoon list invokable-tasks](lagoon_list_invokable-tasks.md) - Print a list of invokable tasks
* [lagoon list notification](lagoon_list_notification.md) - List all notifications or notifications on projects
* [lagoon list projects](lagoon_list_projects.md) - List all projects you have access to (alias: p)
* [lagoon list projects-by-metadata](lagoon_list_projects-by-metadata.md) - List projects by a given metadata key or key:value
* [lagoon list tasks](lagoon_list_tasks.md) - List tasks for an environment (alias: t)
* [lagoon list users](lagoon_list_users.md) - List all users in groups (alias: u)
* [lagoon list user-groups](lagoon_list_user-groups.md) - List a single users groups and roles
* [lagoon list variables](lagoon_list_variables.md) - List variables for a project or environment (alias: v)

41 changes: 41 additions & 0 deletions docs/commands/lagoon_list_all-users.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## lagoon list all-users

List all users

### Synopsis

List all users.
This query can take a long time to run if there are a lot of users.

```
lagoon list all-users [flags]
```

### Options

```
-E, --email-address string The email address of a user
-h, --help help for all-users
```

### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
-l, --lagoon string The Lagoon instance to interact with
--no-header No header on table (if supported)
--output-csv Output as CSV (if supported)
--output-json Output as JSON (if supported)
--pretty Make JSON pretty (if supported)
-p, --project string Specify a project to use
--skip-update-check Skip checking for updates
-i, --ssh-key string Specify path to a specific SSH key to use for lagoon authentication
```

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_backups.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ lagoon list backups [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_deployments.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ lagoon list deployments [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_deploytarget-configs.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ lagoon list deploytarget-configs [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_deploytargets.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ lagoon list deploytargets [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ lagoon list environments [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_group-projects.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ lagoon list group-projects [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

42 changes: 42 additions & 0 deletions docs/commands/lagoon_list_group-users.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## lagoon list group-users

List all users in groups

### Synopsis

List all users in groups in lagoon, this only shows users that are in groups.
If no group name is provided, all groups are queried.
Without a group name, this query may time out in large Lagoon installs.

```
lagoon list group-users [flags]
```

### Options

```
-h, --help help for group-users
-N, --name string Name of the group to list users in
```

### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
-l, --lagoon string The Lagoon instance to interact with
--no-header No header on table (if supported)
--output-csv Output as CSV (if supported)
--output-json Output as JSON (if supported)
--pretty Make JSON pretty (if supported)
-p, --project string Specify a project to use
--skip-update-check Skip checking for updates
-i, --ssh-key string Specify path to a specific SSH key to use for lagoon authentication
```

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ lagoon list groups [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_invokable-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ lagoon list invokable-tasks [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ List all notifications or notifications on projects

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications
* [lagoon list notification email](lagoon_list_notification_email.md) - List all email notification details (alias: e)
* [lagoon list notification microsoftteams](lagoon_list_notification_microsoftteams.md) - List all Microsoft Teams notification details (alias: m)
* [lagoon list notification project-email](lagoon_list_notification_project-email.md) - List email details about a project (alias: pe)
Expand Down
Loading

0 comments on commit 0ddc43a

Please sign in to comment.