Skip to content

Commit

Permalink
CLI now shous list of users on console
Browse files Browse the repository at this point in the history
Signed-off-by: Aashir Siddiqui <[email protected]>
  • Loading branch information
aashir21 committed Oct 31, 2024
1 parent 3e710bc commit 1220b29
Show file tree
Hide file tree
Showing 17 changed files with 482 additions and 167 deletions.
10 changes: 10 additions & 0 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@
"type": "Hex High Entropy String",
"verified_result": null
}
],
"pkg/users/usersGet_test.go": [
{
"hashed_secret": "0c60ced8eef0402874353606d68af16aa72869a8",
"is_secret": false,
"is_verified": false,
"line_number": 88,
"type": "Hex High Entropy String",
"verified_result": null
}
]
},
"version": "0.13.1+ibm.62.dss",
Expand Down
4 changes: 2 additions & 2 deletions docs/generated/errors-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +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'.
- GAL1155E: The loginId provided by the --login-id field cannot be an empty string.
- GAL1156E: Could not get list of users from API server. Reason: '{}'. Ensure you have allocated a personal access token and configured your client program by setting your GALASA_TOKEN as an environment variable or by storing it in your galasactl.properties file
- GAL1157E: An attempt to delete a run named '{}' failed. Cause is {}
- GAL1158E: An attempt to delete a run named '{}' failed. Sending the delete request to the Galasa service failed. Cause is {}
- GAL1159E: An attempt to delete a run named '{}' failed. Unexpected http status code {} received from the server.
Expand Down
4 changes: 2 additions & 2 deletions docs/generated/galasactl_users_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ 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
-h, --help Displays the options for the 'users get' command.
--login-id string Optional. Retrieves a list of users in an ecosystem.
```

### Options inherited from parent commands
Expand Down
19 changes: 5 additions & 14 deletions pkg/cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,11 @@ func (cmd *UsersCommand) createCobraCommand(
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"
func addLoginIdFlag(cmd *cobra.Command, userCmdValues *UsersCmdValues) {

cmd.PersistentFlags().StringVarP(&userCmdValues.name, flagName, "i", "", description)
flagName := "login-id"
var description = "Optional. Retrieves a list of users in an ecosystem."

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

if isMandatory {
cmd.MarkPersistentFlagRequired(flagName)
}
}
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, true, userCommandValues)
addLoginIdFlag(usersGetCobraCmd, userCommandValues)

usersCommand.CobraCommand().AddCommand(usersGetCobraCmd)

Expand Down
32 changes: 1 addition & 31 deletions pkg/cmd/usersGet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,12 @@ func TestUsersGetHelpFlagSetCorrectly(t *testing.T) {
assert.Nil(t, err)
}

func TestUsersGetNoArgsReturnsError(t *testing.T) {
// Given...
factory := utils.NewMockFactory()
var args []string = []string{"users", "get"}
// When...
err := Execute(factory, args)

// Then...
checkOutput("", "Error: required flag(s) \"id\" not set", factory, t)

assert.NotNil(t, err)
assert.Contains(t, err.Error(), "required flag(s) \"id\" not set")
}

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

var args []string = []string{"users", "get", "--id", "me"}
var args []string = []string{"users", "get", "--login-id", "me"}

// When...
err := commandCollection.Execute(args)
Expand All @@ -69,19 +55,3 @@ func TestUsersGetNamespaceNameFlagsReturnsOk(t *testing.T) {
assert.Nil(t, err)
assert.Contains(t, parentCmd.Values().(*UsersCmdValues).name, "me")
}

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

var args []string = []string{"users", "get"}

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

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

assert.ErrorContains(t, err, "required flag(s) \"id\" not set")
}
25 changes: 13 additions & 12 deletions pkg/errors/errorMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,19 @@ var (
" manually delete the file and authenticate against the server again with the 'galasactl auth login' command. If the problem persists, contact your Galasa system administrator. Detailed cause of this problem: '%s'", 1147, STACK_TRACE_NOT_WANTED)
GALASA_JWT_HAS_NO_EXPIRATION_DATETIME = NewMessageType("GAL1148E: The cache of access tokens contains a java web token (jwt) in file '%s', from which an expiration time could not be extracted."+
" This could indicate a problem with the authentication configuration on the Galasa server. Contect your Galasa system administrator. Detailed problem : '%s'\n", 1148, STACK_TRACE_NOT_WANTED)
GALASA_JWT_ENCRYPTION_INVALID_GALASA_TOKEN = NewMessageType("GAL1149E: Programming logic error: Cannot encrypt because the length of the key is too small.", 1149, STACK_TRACE_NOT_WANTED)
GALASA_JWT_DECRYPTION_FAILED_BASE64 = NewMessageType("GAL1150E: Programming logic error: Decryption of cached bearer token failed. Reason: %s", 1150, STACK_TRACE_NOT_WANTED)
GALASA_JWT_DECRYPTION_FAILED_BLOCK_TOO_SMALL = NewMessageType("GAL1151E: Programming logic error: Decryption of cached bearer token failed. Cipher is not long enough. Cipher size: %d, AES block size: %d", 1151, STACK_TRACE_NOT_WANTED)
GALASA_ENCRYPTION_DATA_TOO_LONG = NewMessageType("GAL1152E: Programming logic error: Too much data passed to the encryption process. Please contact your Galasa systems administrator.", 1152, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_REVOKE_TOKEN_FAILED = NewMessageType("GAL1153E: Failed to revoke the token with ID '%v'. Reason: '%s'.", 1153, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_INVALID_TOKEN_ID_FORMAT = NewMessageType("GAL1154E: The provided token ID, '%s', 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.", 1154, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_MISSING_USER_LOGIN_ID_FLAG = NewMessageType("GAL1155E: The id provided by the --id field cannot be an empty string.", 1155, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_LOGIN_ID_NOT_SUPPORTED = NewMessageType("GAL1156E: '%s' is not supported as a valid value. Valid values are 'me'.", 1156, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_RUN_FAILED = NewMessageType("GAL1157E: An attempt to delete a run named '%s' failed. Cause is %s", 1157, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_SERVER_DELETE_RUNS_FAILED = NewMessageType("GAL1158E: An attempt to delete a run named '%s' failed. Sending the delete request to the Galasa service failed. Cause is %v", 1158, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_INVALID_LOGIN_ID = NewMessageType("GAL1165E: '%s' is not supported as a valid login ID. Login ID should not contain spaces.", 1165, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_INVALID_USER_FLAG_VALUE = NewMessageType("GAL1166E: The loginId provided by the --user field cannot be an empty string.", 1166, STACK_TRACE_NOT_WANTED)
GALASA_JWT_ENCRYPTION_INVALID_GALASA_TOKEN = NewMessageType("GAL1149E: Programming logic error: Cannot encrypt because the length of the key is too small.", 1149, STACK_TRACE_NOT_WANTED)
GALASA_JWT_DECRYPTION_FAILED_BASE64 = NewMessageType("GAL1150E: Programming logic error: Decryption of cached bearer token failed. Reason: %s", 1150, STACK_TRACE_NOT_WANTED)
GALASA_JWT_DECRYPTION_FAILED_BLOCK_TOO_SMALL = NewMessageType("GAL1151E: Programming logic error: Decryption of cached bearer token failed. Cipher is not long enough. Cipher size: %d, AES block size: %d", 1151, STACK_TRACE_NOT_WANTED)
GALASA_ENCRYPTION_DATA_TOO_LONG = NewMessageType("GAL1152E: Programming logic error: Too much data passed to the encryption process. Please contact your Galasa systems administrator.", 1152, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_REVOKE_TOKEN_FAILED = NewMessageType("GAL1153E: Failed to revoke the token with ID '%v'. Reason: '%s'.", 1153, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_INVALID_TOKEN_ID_FORMAT = NewMessageType("GAL1154E: The provided token ID, '%s', 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.", 1154, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_MISSING_USER_LOGIN_ID_FLAG = NewMessageType("GAL1155E: The loginId provided by the --login-id field cannot be an empty string.", 1155, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_RETRIEVING_USER_LIST_FROM_API_SERVER = NewMessageType("GAL1156E: Could not get list of users from API server. Reason: '%s'."+
" Ensure you have allocated a personal access token and configured your client program by setting your GALASA_TOKEN as an environment variable or by storing it in your galasactl.properties file", 1156, STACK_TRACE_WANTED)
GALASA_ERROR_DELETE_RUN_FAILED = NewMessageType("GAL1157E: An attempt to delete a run named '%s' failed. Cause is %s", 1157, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_SERVER_DELETE_RUNS_FAILED = NewMessageType("GAL1158E: An attempt to delete a run named '%s' failed. Sending the delete request to the Galasa service failed. Cause is %v", 1158, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_INVALID_LOGIN_ID = NewMessageType("GAL1165E: '%s' is not supported as a valid login ID. Login ID should not contain spaces.", 1165, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_INVALID_USER_FLAG_VALUE = NewMessageType("GAL1166E: The loginId provided by the --user field cannot be an empty string.", 1166, STACK_TRACE_NOT_WANTED)

// 4 related but slightly different errors, when an HTTP response arrives from the Galasa server, and we can/can't parse the payload to get the message details out.
GALASA_ERROR_DELETE_RUNS_NO_RESPONSE_CONTENT = NewMessageType("GAL1159E: An attempt to delete a run named '%s' failed. Unexpected http status code %v received from the server.", 1159, STACK_TRACE_NOT_WANTED)
Expand Down
3 changes: 2 additions & 1 deletion pkg/tokensformatter/summaryFormatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/galasa-dev/cli/pkg/galasaapi"
"github.com/galasa-dev/cli/pkg/utils"
)

// -----------------------------------------------------
Expand Down Expand Up @@ -44,7 +45,7 @@ func (*TokenSummaryFormatter) FormatTokens(authTokens []galasaapi.AuthToken) (st
for _, token := range authTokens {
var line []string
id := token.GetTokenId()
creationTime := formatTimeToNearestDate(token.GetCreationTime())
creationTime := utils.FormatTimeToNearestDate(token.GetCreationTime())
owner := token.GetOwner()
ownerLoginId := owner.GetLoginId()
description := token.GetDescription()
Expand Down
12 changes: 0 additions & 12 deletions pkg/tokensformatter/tokensFormatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,3 @@ func writeFormattedTableToStringBuilder(table [][]string, buff *strings.Builder,
buff.WriteString("\n")
}
}

// -----------------------------------------------------
// Functions for time formats and duration
func formatTimeToNearestDate(rawTime string) string {
var formattedTimeString string
if len(rawTime) < 19 {
formattedTimeString = ""
} else {
formattedTimeString = rawTime[0:10]
}
return formattedTimeString
}
7 changes: 0 additions & 7 deletions pkg/users/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ func validateLoginIdFlag(loginId string) (string, error) {
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_MISSING_USER_LOGIN_ID_FLAG)
}

if err == nil {

if loginId != "me" {
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_LOGIN_ID_NOT_SUPPORTED, loginId)
}
}

return loginId, err

}
61 changes: 43 additions & 18 deletions pkg/users/usersGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,34 @@ package users

import (
"context"
"fmt"
"log"
"net/http"

"github.com/galasa-dev/cli/pkg/embedded"
galasaErrors "github.com/galasa-dev/cli/pkg/errors"
"github.com/galasa-dev/cli/pkg/galasaapi"
"github.com/galasa-dev/cli/pkg/spi"
"github.com/galasa-dev/cli/pkg/usersformatter"
)

func GetUsers(loginId string, apiClient *galasaapi.APIClient, console spi.Console) error {
var err error
var userData galasaapi.UserData

loginId, err = validateLoginIdFlag(loginId)
userData, err := getUserDataFromRestApi(loginId, apiClient)

if err == nil {
userData, err = getUserDataFromRestApi(loginId, apiClient)
err = formatFetchedUsersAndWriteToConsole(userData, console)
}

if err == nil {
extractedUserId := userData.GetLoginId()
console.WriteString(fmt.Sprintf("id: %s\n", extractedUserId))
}
return err
}

func formatFetchedUsersAndWriteToConsole(users []galasaapi.UserData, console spi.Console) error {

summaryFormatter := usersformatter.NewUserSummaryFormatter()

outputText, err := summaryFormatter.FormatUsers(users)

if err == nil {
console.WriteString(outputText)
}

return err
Expand All @@ -36,25 +44,42 @@ func GetUsers(loginId string, apiClient *galasaapi.APIClient, console spi.Consol
func getUserDataFromRestApi(
loginId string,
apiClient *galasaapi.APIClient,
) (galasaapi.UserData, error) {
) ([]galasaapi.UserData, error) {

var err error
var context context.Context = nil
var users []galasaapi.UserData
var err error

var restApiVersion string
apiCall := apiClient.UsersAPIApi.GetUserByLoginId(context)

var userProperties = make([]galasaapi.UserData, 0)
if loginId != "" {

restApiVersion, err = embedded.GetGalasactlRestApiVersion()
loginId, err = validateLoginIdFlag(loginId)

if err == nil {
apiCall = apiCall.LoginId(loginId)
}
}

if err == nil {

apiCall := apiClient.UsersAPIApi.GetUserByLoginId(context).LoginId(loginId).ClientApiVersion(restApiVersion)
userProperties, _, err = apiCall.Execute()
var usersIn []galasaapi.UserData
var resp *http.Response

usersIn, resp, err = apiCall.Execute()

if err != nil {
log.Println("getUserDataFromRestApi - Failed to retrieve list of users from API server")
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_RETRIEVING_USER_LIST_FROM_API_SERVER, err.Error())
} else {
defer resp.Body.Close()
users = usersIn
log.Printf("getUserDataFromRestApi - %v users collected", len(users))
}

}

//Since we have the loginId filter, we can return the first index.
//It will always be the currently logged in user
return userProperties[0], err
return users, err
}
Loading

0 comments on commit 1220b29

Please sign in to comment.