Skip to content

Commit

Permalink
Merge pull request #306 from galasa-dev/ash-cli-delete-command
Browse files Browse the repository at this point in the history
CLI now supports users delete command
  • Loading branch information
aashir21 authored Nov 15, 2024
2 parents d26fdb5 + 12c9613 commit 91982fe
Show file tree
Hide file tree
Showing 14 changed files with 565 additions and 39 deletions.
10 changes: 9 additions & 1 deletion docs/generated/errors-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ The `galasactl` tool can generate the following errors:
- GAL1163E: The run named '{}' could not be deleted because it was not found by the Galasa service. Try listing runs using 'galasactl runs get' to identify the one you wish to delete
- GAL1164E: An attempt to delete a run named '{}' failed. Unexpected http status code {} received from the server. Error details from the server are not in the json format.
- GAL1165E: '{}' is not supported as a valid login ID. Login ID should not contain spaces.
- GAL1166E: The loginId provided by the --user field cannot be an empty string.
- GAL1166E: The loginId provided by the --login-id field cannot be an empty string.
- GAL1167E: An attempt to delete a secret named '{}' failed. Unexpected http status code {} received from the server.
- GAL1168E: An attempt to delete a secret named '{}' failed. Unexpected http status code {} received from the server. Error details from the server could not be read. Cause: {}
- GAL1169E: An attempt to delete a secret named '{}' failed. Unexpected http status code {} received from the server. Error details from the server are not in a valid json format. Cause: '{}'
Expand Down Expand Up @@ -190,6 +190,14 @@ The `galasactl` tool can generate the following errors:
- GAL1192E: Failed to set a secret named '{}'. Sending the put request to the Galasa service failed. Cause is {}
- GAL1193E: Invalid flag combination provided. --username cannot be provided with --base64-username, --password cannot be provided with --base64-password, and --token cannot be provided with --base64-token. Use the --help flag for more information, or refer to the documentation at https://galasa.dev/docs/reference/cli-commands.
- GAL1194E: Invalid secret description provided. The description provided with the --description flag cannot be an empty string, and must only contain characters in the Latin-1 character set.
- GAL1195E: Failed to delete user from database by user number.
- GAL1196E: The user could not be deleted by login ID because it was not found by the Galasa service. Try listing users using 'galasactl users get' to identify the one you wish to delete
- GAL1197E: An attempt to delete a user failed. Sending the delete request to the Galasa service failed. Cause is {}
- GAL1198E: An attempt to delete a user numbered '{}' failed. Unexpected http status code {} received from the server.
- GAL1199E: An attempt to delete a user numbered '{}' failed. Unexpected http status code {} received from the server. Error details from the server could not be read. Cause: {}
- GAL1200E: An attempt to delete a user numbered '{}' failed. Unexpected http status code {} received from the server. Error details from the server are not in a valid json format. Cause: '{}'
- GAL1201E: An attempt to delete a user numbered '{}' failed. Unexpected http status code {} received from the server. Error details from the server are: '{}'
- GAL1202E: An attempt to delete a user numbered '{}' failed. Unexpected http status code {} received from the server. Error details from the server are not in the json format.
- 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_users.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ Allows interaction with the user servlet to return information about users.
### SEE ALSO

* [galasactl](galasactl.md) - CLI for Galasa
* [galasactl users delete](galasactl_users_delete.md) - Deletes a user by login ID
* [galasactl users get](galasactl_users_get.md) - Get a list of users

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

Deletes a user by login ID

### Synopsis

Deletes a single user by their login ID from the ecosystem

```
galasactl users delete [flags]
```

### Options

```
-h, --help Displays the options for the 'users delete' command.
--login-id string A mandatory field indicating the login ID of a user.
```

### 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

2 changes: 1 addition & 1 deletion docs/generated/galasactl_users_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ galasactl users get [flags]

```
-h, --help Displays the options for the 'users get' command.
--login-id string Optional. Retrieves a list of users in an ecosystem.
--login-id string An optional field indicating the login ID of a user.
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion pkg/auth/authTokensGet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestMissingLoginIdFlagReturnsBadRequest(t *testing.T) {
defer server.Close()

console := utils.NewMockConsole()
expectedOutput := `GAL1166E: The loginId provided by the --user field cannot be an empty string.`
expectedOutput := `GAL1166E: The loginId provided by the --login-id field cannot be an empty string.`

//When
err := GetTokens(apiClient, console, " ")
Expand Down
5 changes: 5 additions & 0 deletions pkg/cmd/commandCollection.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const (
COMMAND_NAME_SECRETS_DELETE = "secrets delete"
COMMAND_NAME_USERS = "users"
COMMAND_NAME_USERS_GET = "users get"
COMMAND_NAME_USERS_DELETE = "users delete"
)

// -----------------------------------------------------------------
Expand Down Expand Up @@ -420,16 +421,20 @@ func (commands *commandCollectionImpl) addUsersCommands(factory spi.Factory, roo
var err error
var usersCommand spi.GalasaCommand
var usersGetCommand spi.GalasaCommand
var usersDeleteCommand spi.GalasaCommand

usersCommand, err = NewUsersCommand(rootCommand)
usersDeleteCommand, err = NewUsersCommand(rootCommand)

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

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

return err
Expand Down
10 changes: 8 additions & 2 deletions pkg/cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,16 @@ func (cmd *UsersCommand) createCobraCommand(
return usersCobraCmd
}

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

flagName := "login-id"
var description = "Optional. Retrieves a list of users in an ecosystem."
var description string

if isMandatory{
description = "A mandatory field indicating the login ID of a user."
} else {
description = "An optional field indicating the login ID of a user."
}

cmd.Flags().StringVar(&userCmdValues.name, flagName, "", description)

Expand Down
145 changes: 145 additions & 0 deletions pkg/cmd/usersDelete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* 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 delete
type UsersDeleteCommand struct {
cobraCommand *cobra.Command
}

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

cmd := new(UsersDeleteCommand)

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

// ------------------------------------------------------------------------------------------------
// Public methods
// ------------------------------------------------------------------------------------------------
func (cmd *UsersDeleteCommand) Name() string {
return COMMAND_NAME_USERS_DELETE
}

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

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

// ------------------------------------------------------------------------------------------------
// Private methods
// ------------------------------------------------------------------------------------------------
func (cmd *UsersDeleteCommand) 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 *UsersDeleteCommand) createCobraCmd(
factory spi.Factory,
usersCommand,
rootCmd spi.GalasaCommand,
) (*cobra.Command, error) {

var err error

userCommandValues := usersCommand.Values().(*UsersCmdValues)
usersDeleteCobraCmd := &cobra.Command{
Use: "delete",
Short: "Deletes a user by login ID",
Long: "Deletes a single user by their login ID from the ecosystem",
Aliases: []string{COMMAND_NAME_USERS_DELETE},
RunE: func(cobraCommand *cobra.Command, args []string) error {
return cmd.executeUsersDelete(factory, usersCommand.Values().(*UsersCmdValues), rootCmd.Values().(*RootCmdValues))
},
}

addLoginIdFlag(usersDeleteCobraCmd, true, userCommandValues)

usersCommand.CobraCommand().AddCommand(usersDeleteCobraCmd)

return usersDeleteCobraCmd, err
}

func (cmd *UsersDeleteCommand) executeUsersDelete(
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()
byteReader := factory.GetByteReader()

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

if err == nil {
rootCmdValues.isCapturingLogs = true

log.Println("Galasa CLI - Delete user 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 {

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.DeleteUser(userCmdValues.name, apiClient, byteReader)
}
}
}
}

return err
}
60 changes: 60 additions & 0 deletions pkg/cmd/usersDelete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright contributors to the Galasa project
*
* SPDX-License-Identifier: EPL-2.0
*/
package cmd

import (
"testing"

"github.com/galasa-dev/cli/pkg/utils"
"github.com/stretchr/testify/assert"
)

func TestUsersDeleteCommandInCommandCollectionHasName(t *testing.T) {

factory := utils.NewMockFactory()
commands, _ := NewCommandCollection(factory)

UsersDeleteCommand, err := commands.GetCommand(COMMAND_NAME_USERS_DELETE)
assert.Nil(t, err)

assert.Equal(t, COMMAND_NAME_USERS_DELETE, UsersDeleteCommand.Name())
assert.NotNil(t, UsersDeleteCommand.CobraCommand())
}

func TestUsersDeleteHelpFlagSetCorrectly(t *testing.T) {
// Given...
factory := utils.NewMockFactory()

var args []string = []string{"users", "delete", "--help"}

// When...
err := Execute(factory, args)

// Then...
// Check what the user saw is reasonable.
checkOutput("Displays the options for the 'users delete' command.", "", factory, t)

assert.Nil(t, err)
}

func TestUsersDeleteNamespaceNameFlagsReturnsOk(t *testing.T) {
// Given...
factory := utils.NewMockFactory()
commandCollection, _ := setupTestCommandCollection(COMMAND_NAME_USERS_DELETE, factory, t)

var args []string = []string{"users", "delete", "--login-id", "admin"}

// When...
err := commandCollection.Execute(args)

// Then...
assert.Nil(t, err)

// Check what the user saw was reasonable
checkOutput("", "", factory, t)

assert.Nil(t, err)
}
2 changes: 1 addition & 1 deletion pkg/cmd/usersGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (cmd *UsersGetCommand) createCobraCmd(
},
}

addLoginIdFlag(usersGetCobraCmd, userCommandValues)
addLoginIdFlag(usersGetCobraCmd, false, userCommandValues)

usersCommand.CobraCommand().AddCommand(usersGetCobraCmd)

Expand Down
Loading

0 comments on commit 91982fe

Please sign in to comment.