-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from galasa-dev/ash-cli-delete-command
CLI now supports users delete command
- Loading branch information
Showing
14 changed files
with
565 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.