Skip to content

Commit

Permalink
feat(pools): add new flags (#47)
Browse files Browse the repository at this point in the history
Add query, sort, sorted-by flags to pool list command.
  • Loading branch information
mariiatuzovska authored Jan 16, 2023
1 parent d1a4ac2 commit 29120c4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 16 deletions.
8 changes: 8 additions & 0 deletions .changes/unreleased/🎉 Feature-20230113-221306.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
kind: "\U0001F389 Feature"
body: Add query, sort, sorted-by flags to pool list command.
time: 2023-01-13T22:13:06.626484054+02:00
custom:
azure-boards-workitemid-fixed: '481686'
azure-boards-workitemid-related: '481683'
github-contributor: ''
github-link: ''
2 changes: 1 addition & 1 deletion commands/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func engineList(vcli vaultcli.CLI, params *engineListParams) ([]byte, *errors.Ap
queryParams[cst.Sort] = params.sort
}
if params.sortedBy != "" {
queryParams[cst.SortedBy] = params.sortedBy
queryParams["sortedBy"] = params.sortedBy
}
uri := paths.CreateResourceURI(cst.NounEngines, "", "", false, queryParams)
return vcli.HTTPClient().DoRequest(http.MethodGet, uri, nil)
Expand Down
37 changes: 34 additions & 3 deletions commands/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ func GetPoolListCmd() (cli.Command, error) {
HelpText: `
Usage:
• pool list
• pool list --query mypool
• pool list --sort asc --sorted-by name
`,
FlagsPredictor: []*predictor.Params{
{Name: cst.Query, Shorthand: "q", Usage: "Partial search by pool name (optional)"},
{Name: cst.Sort, Usage: cst.SortHelpMessage, Default: "desc"},
{Name: cst.SortedBy, Usage: "Sort by name or created field (optional)", Default: "created"},
},
RunFunc: handlePoolList,
})
}
Expand Down Expand Up @@ -130,7 +137,15 @@ func handlePoolCreate(vcli vaultcli.CLI, args []string) int {
}

func handlePoolList(vcli vaultcli.CLI, args []string) int {
data, apiErr := poolList(vcli)
searchTerm := viper.GetString(cst.Query)
sort := viper.GetString(cst.Sort)
sortedBy := viper.GetString(cst.SortedBy)

data, apiErr := poolList(vcli, &poolListParams{
searchTerm: searchTerm,
sort: sort,
sortedBy: sortedBy,
})
vcli.Out().WriteResponse(data, apiErr)
return utils.GetExecStatus(apiErr)
}
Expand Down Expand Up @@ -186,7 +201,23 @@ func poolDelete(vcli vaultcli.CLI, name string) ([]byte, *errors.ApiError) {
return vcli.HTTPClient().DoRequest(http.MethodDelete, uri, nil)
}

func poolList(vcli vaultcli.CLI) ([]byte, *errors.ApiError) {
uri := paths.CreateResourceURI(cst.NounPools, "", "", false, nil)
type poolListParams struct {
searchTerm string
sort string
sortedBy string
}

func poolList(vcli vaultcli.CLI, params *poolListParams) ([]byte, *errors.ApiError) {
queryParams := map[string]string{}
if params.searchTerm != "" {
queryParams[cst.SearchTerm] = params.searchTerm
}
if params.sort != "" {
queryParams[cst.Sort] = params.sort
}
if params.sortedBy != "" {
queryParams["sortedBy"] = params.sortedBy
}
uri := paths.CreateResourceURI(cst.NounPools, "", "", false, queryParams)
return vcli.HTTPClient().DoRequest(http.MethodGet, uri, nil)
}
39 changes: 27 additions & 12 deletions tests/e2e/cmd_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
func TestPool(t *testing.T) {
e := newEnv()

poolName := makePoolName()
poolName1 := makePoolName()
poolName2 := makePoolName()

output := runWithAuth(t, e, "pool")
requireLine(t, output, "Work with engine pools")
Expand All @@ -23,46 +24,60 @@ func TestPool(t *testing.T) {
output = runWithAuth(t, e, "pool create --help")
requireContains(t, output, "Create a new empty pool of engines")

output = runWithAuth(t, e, fmt.Sprintf("pool create --name=%s", poolName))
output = runWithAuth(t, e, fmt.Sprintf("pool create --name=%s", poolName1))
requireContains(t, output, fmt.Sprintf(`"createdBy": "users:%s",`, e.username))
requireContains(t, output, fmt.Sprintf(`"name": "%s"`, poolName))
requireContains(t, output, fmt.Sprintf(`"name": "%s"`, poolName1))

output = runWithAuth(t, e, "pool read --help")
requireContains(t, output, "Get information on an existing pool of engines")

output = runWithAuth(t, e, "pool read")
requireContains(t, output, "error: must specify name")

output = runWithAuth(t, e, fmt.Sprintf("pool read --name=%s", poolName))
output = runWithAuth(t, e, fmt.Sprintf("pool read --name=%s", poolName1))
requireContains(t, output, fmt.Sprintf(`"createdBy": "users:%s",`, e.username))
requireContains(t, output, fmt.Sprintf(`"name": "%s"`, poolName1))

output = runWithAuth(t, e, fmt.Sprintf("pool read %s", poolName1))
requireContains(t, output, fmt.Sprintf(`"createdBy": "users:%s",`, e.username))
requireContains(t, output, fmt.Sprintf(`"name": "%s"`, poolName))
requireContains(t, output, fmt.Sprintf(`"name": "%s"`, poolName1))

output = runWithAuth(t, e, fmt.Sprintf("pool read %s", poolName))
output = runWithAuth(t, e, fmt.Sprintf("pool %s", poolName1))
requireContains(t, output, fmt.Sprintf(`"createdBy": "users:%s",`, e.username))
requireContains(t, output, fmt.Sprintf(`"name": "%s"`, poolName))
requireContains(t, output, fmt.Sprintf(`"name": "%s"`, poolName1))

output = runWithAuth(t, e, fmt.Sprintf("pool %s", poolName))
output = runWithAuth(t, e, fmt.Sprintf("pool create --name=%s", poolName2))
requireContains(t, output, fmt.Sprintf(`"createdBy": "users:%s",`, e.username))
requireContains(t, output, fmt.Sprintf(`"name": "%s"`, poolName))
requireContains(t, output, fmt.Sprintf(`"name": "%s"`, poolName2))

output = runWithAuth(t, e, "pool list --help")
requireContains(t, output, "List the names of all existing pools")

output = runWithAuth(t, e, "pool list")
requireContains(t, output, `"pools": [`)
requireContains(t, output, fmt.Sprintf(`"name": "%s"`, poolName))
requireContains(t, output, fmt.Sprintf(`"name": "%s"`, poolName1))
requireContains(t, output, fmt.Sprintf(`"name": "%s"`, poolName2))

output = runWithAuth(t, e, "pool list --query "+poolName1)
requireContains(t, output, `"pools": [`)
requireContains(t, output, fmt.Sprintf(`"name": "%s"`, poolName1))
requireNotContains(t, output, fmt.Sprintf(`"name": "%s"`, poolName2))

output = runWithAuth(t, e, "pool delete --help")
requireContains(t, output, "Delete an existing pool of engines")

output = runWithAuth(t, e, "pool delete")
requireContains(t, output, "error: must specify name")

output = runWithAuth(t, e, fmt.Sprintf("pool delete --name=%s", poolName))
output = runWithAuth(t, e, fmt.Sprintf("pool delete --name=%s", poolName1))
if output != "" {
t.Fatalf("Unexpected output: \n%s\n", output)
}
output = runWithAuth(t, e, fmt.Sprintf("pool delete --name=%s", poolName2))
if output != "" {
t.Fatalf("Unexpected output: \n%s\n", output)
}
output = runWithAuth(t, e, fmt.Sprintf("pool delete %s", poolName))
output = runWithAuth(t, e, fmt.Sprintf("pool delete %s", poolName1))
requireContains(t, output, `"message": "unable to find item with specified identifier"`)
}

Expand Down

0 comments on commit 29120c4

Please sign in to comment.