Skip to content

Commit

Permalink
Merge pull request #271 from galasa-dev/Iss1949
Browse files Browse the repository at this point in the history
Added new users command to cli
  • Loading branch information
aashir21 authored Aug 22, 2024
2 parents ff10202 + f2a8e9e commit 9f4368b
Show file tree
Hide file tree
Showing 14 changed files with 685 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/generated/errors-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ The `galasactl` tool can generate the following errors:
- GAL1152E: Programming logic error: Too much data passed to the encryption process. Please contact your Galasa systems administrator.
- GAL1153E: Failed to revoke the token with ID '{}'. Reason: '{}'.
- GAL1154E: The provided token ID, '{}', does not match formatting requirements. The token ID can contain any character in the 'a'-'z', 'A'-'Z', '0'-'9', '-' (dash), or '_' (underscore) ranges only.
- GAL1155E: The id provided by the --id field cannot be an empty string.
- GAL1156E: '{}' is not supported as a valid value. Valid values are 'me'.
- GAL1225E: Failed to open file '{}' cause: {}. Check that this file exists, and that you have read permissions.
- GAL1226E: Internal failure. Contents of gzip could be read, but not decoded. New gzip reader failed: file: {} error: {}
- GAL1227E: Internal failure. Contents of gzip could not be decoded. {} error: {}
Expand Down
1 change: 1 addition & 0 deletions docs/generated/galasactl.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ A tool for controlling Galasa resources using the command-line.
* [galasactl properties](galasactl_properties.md) - Manages properties in an ecosystem
* [galasactl resources](galasactl_resources.md) - Manages resources in an ecosystem
* [galasactl runs](galasactl_runs.md) - Manage test runs in the ecosystem
* [galasactl users](galasactl_users.md) - Manages users in an ecosystem

27 changes: 27 additions & 0 deletions docs/generated/galasactl_users.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## galasactl users

Manages users in an ecosystem

### Synopsis

Allows interaction with the user servlet to return information about users.

### Options

```
-b, --bootstrap string Bootstrap URL. Should start with 'http://' or 'file://'. If it starts with neither, it is assumed to be a fully-qualified path. If missing, it defaults to use the 'bootstrap.properties' file in your GALASA_HOME. Example: http://example.com/bootstrap, file:///user/myuserid/.galasa/bootstrap.properties , file://C:/Users/myuserid/.galasa/bootstrap.properties
-h, --help Displays the options for the 'users' command.
```

### Options inherited from parent commands

```
--galasahome string Path to a folder where Galasa will read and write files and configuration settings. The default is '${HOME}/.galasa'. This overrides the GALASA_HOME environment variable which may be set instead.
-l, --log string File to which log information will be sent. Any folder referred to must exist. An existing file will be overwritten. Specify "-" to log to stderr. Defaults to not logging.
```

### SEE ALSO

* [galasactl](galasactl.md) - CLI for Galasa
* [galasactl users get](galasactl_users_get.md) - Get a list of users

31 changes: 31 additions & 0 deletions docs/generated/galasactl_users_get.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## galasactl users get

Get a list of users

### Synopsis

Get a list of users stored in the Galasa API server

```
galasactl users get [flags]
```

### Options

```
-h, --help Displays the options for the 'users get' command.
-i, --id string A mandatory flag that is required to return the currently logged in user.The input must be a string
```

### Options inherited from parent commands

```
-b, --bootstrap string Bootstrap URL. Should start with 'http://' or 'file://'. If it starts with neither, it is assumed to be a fully-qualified path. If missing, it defaults to use the 'bootstrap.properties' file in your GALASA_HOME. Example: http://example.com/bootstrap, file:///user/myuserid/.galasa/bootstrap.properties , file://C:/Users/myuserid/.galasa/bootstrap.properties
--galasahome string Path to a folder where Galasa will read and write files and configuration settings. The default is '${HOME}/.galasa'. This overrides the GALASA_HOME environment variable which may be set instead.
-l, --log string File to which log information will be sent. Any folder referred to must exist. An existing file will be overwritten. Specify "-" to log to stderr. Defaults to not logging.
```

### SEE ALSO

* [galasactl users](galasactl_users.md) - Manages users in an ecosystem

26 changes: 26 additions & 0 deletions pkg/cmd/commandCollection.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const (
COMMAND_NAME_RESOURCES_CREATE = "resources create"
COMMAND_NAME_RESOURCES_UPDATE = "resources update"
COMMAND_NAME_RESOURCES_DELETE = "resources delete"
COMMAND_NAME_USERS = "users"
COMMAND_NAME_USERS_GET = "users get"
)

// -----------------------------------------------------------------
Expand Down Expand Up @@ -139,6 +141,10 @@ func (commands *commandCollectionImpl) init(factory spi.Factory) error {
err = commands.addResourcesCommands(factory, rootCommand)
}

if err == nil {
err = commands.addUsersCommands(factory, rootCommand)
}

if err == nil {
commands.setHelpFlags()
}
Expand Down Expand Up @@ -363,6 +369,26 @@ func (commands *commandCollectionImpl) addResourcesCommands(factory spi.Factory,
return err
}

func (commands *commandCollectionImpl) addUsersCommands(factory spi.Factory, rootCommand spi.GalasaCommand) error {

var err error
var usersCommand spi.GalasaCommand
var usersGetCommand spi.GalasaCommand

usersCommand, err = NewUsersCommand(rootCommand)

if err == nil {
usersGetCommand, err = NewUsersGetCommand(factory, usersCommand, rootCommand)
}

if err == nil {
commands.commandMap[usersCommand.Name()] = usersCommand
commands.commandMap[usersGetCommand.Name()] = usersGetCommand
}

return err
}

func (commands *commandCollectionImpl) setHelpFlags() {
for _, command := range commands.commandMap {
command.CobraCommand().Flags().BoolP("help", "h", false, "Displays the options for the '"+command.Name()+"' command.")
Expand Down
96 changes: 96 additions & 0 deletions pkg/cmd/users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright contributors to the Galasa project
*
* SPDX-License-Identifier: EPL-2.0
*/

package cmd

import (
"github.com/galasa-dev/cli/pkg/spi"
"github.com/spf13/cobra"
)

type UsersCmdValues struct {
ecosystemBootstrap string
name string
}

type UsersCommand struct {
values *UsersCmdValues
cobraCommand *cobra.Command
}

// ------------------------------------------------------------------------------------------------
// Constructors methods
// ------------------------------------------------------------------------------------------------
func NewUsersCommand(rootCmd spi.GalasaCommand) (spi.GalasaCommand, error) {

cmd := new(UsersCommand)
err := cmd.init(rootCmd)
return cmd, err
}

// ------------------------------------------------------------------------------------------------
// Public methods
// ------------------------------------------------------------------------------------------------
func (cmd *UsersCommand) Name() string {
return COMMAND_NAME_USERS
}

func (cmd *UsersCommand) CobraCommand() *cobra.Command {
return cmd.cobraCommand
}

func (cmd *UsersCommand) Values() interface{} {
return cmd.values
}

// ------------------------------------------------------------------------------------------------
// Private methods
// ------------------------------------------------------------------------------------------------

func (cmd *UsersCommand) init(rootCmd spi.GalasaCommand) error {

var err error

cmd.values = &UsersCmdValues{}
cmd.cobraCommand = cmd.createCobraCommand(rootCmd)

return err
}

func (cmd *UsersCommand) createCobraCommand(
rootCommand spi.GalasaCommand,
) *cobra.Command {

usersCobraCmd := &cobra.Command{
Use: "users",
Short: "Manages users in an ecosystem",
Long: "Allows interaction with the user servlet to return information about users.",
}

addBootstrapFlag(usersCobraCmd, &cmd.values.ecosystemBootstrap)

rootCommand.CobraCommand().AddCommand(usersCobraCmd)

return usersCobraCmd
}

func addLoginIdFlag(cmd *cobra.Command, isMandatory bool, userCmdValues *UsersCmdValues) {

flagName := "id"
var description string
if isMandatory {
description = "A mandatory flag that is required to return the currently logged in user."
} else {
description = "An optional flag that is required to return the currently logged in user."
}
description += "The input must be a string"

cmd.PersistentFlags().StringVarP(&userCmdValues.name, flagName, "i", "", description)

if isMandatory {
cmd.MarkPersistentFlagRequired(flagName)
}
}
146 changes: 146 additions & 0 deletions pkg/cmd/usersGet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright contributors to the Galasa project
*
* SPDX-License-Identifier: EPL-2.0
*/
package cmd

import (
"log"

"github.com/galasa-dev/cli/pkg/api"
"github.com/galasa-dev/cli/pkg/galasaapi"
"github.com/galasa-dev/cli/pkg/spi"
"github.com/galasa-dev/cli/pkg/users"
"github.com/galasa-dev/cli/pkg/utils"
"github.com/spf13/cobra"
)

// Objective: Allow user to do this:
//
// users get
type UsersGetCommand struct {
cobraCommand *cobra.Command
}

// ------------------------------------------------------------------------------------------------
// Constructors methods
// ------------------------------------------------------------------------------------------------
func NewUsersGetCommand(
factory spi.Factory,
usersGetCommand spi.GalasaCommand,
rootCmd spi.GalasaCommand,
) (spi.GalasaCommand, error) {

cmd := new(UsersGetCommand)

err := cmd.init(factory, usersGetCommand, rootCmd)
return cmd, err
}

// ------------------------------------------------------------------------------------------------
// Public methods
// ------------------------------------------------------------------------------------------------
func (cmd *UsersGetCommand) Name() string {
return COMMAND_NAME_USERS_GET
}

func (cmd *UsersGetCommand) CobraCommand() *cobra.Command {
return cmd.cobraCommand
}

func (cmd *UsersGetCommand) Values() interface{} {
// There are no values.
return nil
}

// ------------------------------------------------------------------------------------------------
// Private methods
// ------------------------------------------------------------------------------------------------
func (cmd *UsersGetCommand) init(factory spi.Factory, usersCommand spi.GalasaCommand, rootCmd spi.GalasaCommand) error {
var err error

cmd.cobraCommand, err = cmd.createCobraCmd(factory, usersCommand, rootCmd)

return err
}

func (cmd *UsersGetCommand) createCobraCmd(
factory spi.Factory,
usersCommand,
rootCmd spi.GalasaCommand,
) (*cobra.Command, error) {

var err error

userCommandValues := usersCommand.Values().(*UsersCmdValues)
usersGetCobraCmd := &cobra.Command{
Use: "get",
Short: "Get a list of users",
Long: "Get a list of users stored in the Galasa API server",
Aliases: []string{COMMAND_NAME_USERS_GET},
RunE: func(cobraCommand *cobra.Command, args []string) error {
return cmd.executeUsersGet(factory, usersCommand.Values().(*UsersCmdValues), rootCmd.Values().(*RootCmdValues))
},
}

addLoginIdFlag(usersGetCobraCmd, true, userCommandValues)

usersCommand.CobraCommand().AddCommand(usersGetCobraCmd)

return usersGetCobraCmd, err
}

func (cmd *UsersGetCommand) executeUsersGet(
factory spi.Factory,
userCmdValues *UsersCmdValues,
rootCmdValues *RootCmdValues,
) error {

var err error
// Operations on the file system will all be relative to the current folder.
fileSystem := factory.GetFileSystem()

err = utils.CaptureLog(fileSystem, rootCmdValues.logFileName)

if err == nil {
rootCmdValues.isCapturingLogs = true

log.Println("Galasa CLI - Get users from the ecosystem")

// Get the ability to query environment variables.
env := factory.GetEnvironment()

var galasaHome spi.GalasaHome
galasaHome, err = utils.NewGalasaHome(fileSystem, env, rootCmdValues.CmdParamGalasaHomePath)
if err == nil {

// Read the bootstrap users.
var urlService *api.RealUrlResolutionService = new(api.RealUrlResolutionService)
var bootstrapData *api.BootstrapData
bootstrapData, err = api.LoadBootstrap(galasaHome, fileSystem, env, userCmdValues.ecosystemBootstrap, urlService)
if err == nil {

var console = factory.GetStdOutConsole()

apiServerUrl := bootstrapData.ApiServerURL
log.Printf("The API server is at '%s'\n", apiServerUrl)

authenticator := factory.GetAuthenticator(
apiServerUrl,
galasaHome,
)

var apiClient *galasaapi.APIClient
apiClient, err = authenticator.GetAuthenticatedAPIClient()

if err == nil {
// Call to process the command in a unit-testable way.
err = users.GetUsers(userCmdValues.name, apiClient, console)
}
}
}
}

return err
}
Loading

0 comments on commit 9f4368b

Please sign in to comment.