Skip to content

Commit

Permalink
Merge pull request #140 from cyberark/add-resource-ids-method
Browse files Browse the repository at this point in the history
Add method to retrieve only resource IDs
  • Loading branch information
jtuttle authored Nov 22, 2022
2 parents 31bfa2a + a6fa893 commit dbc8cbd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
16 changes: 16 additions & 0 deletions conjurapi/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,19 @@ func (c *Client) Resources(filter *ResourceFilter) (resources []map[string]inter

return
}

func (c *Client) ResourceIDs(filter *ResourceFilter) ([]string, error) {
resources, err := c.Resources(filter)

if err != nil {
return nil, err
}

resourceIDs := make([]string, 0)

for _, element := range resources {
resourceIDs = append(resourceIDs, element["id"].(string))
}

return resourceIDs, nil
}
21 changes: 21 additions & 0 deletions conjurapi/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,24 @@ func TestClient_Resource(t *testing.T) {

t.Run("Shows a resource", showResource(conjur, "cucumber:variable:db-password"))
}

func TestClient_ResourceIDs(t *testing.T) {
listResourceIDs := func(conjur *Client, filter *ResourceFilter, expected int) func(t *testing.T) {
return func(t *testing.T) {
resources, err := conjur.ResourceIDs(filter)
assert.NoError(t, err)
assert.Len(t, resources, expected)
}
}

conjur, err := conjurSetup()
assert.NoError(t, err)

t.Run("Lists all resources", listResourceIDs(conjur, nil, 12))
t.Run("Lists resources by kind", listResourceIDs(conjur, &ResourceFilter{Kind: "variable"}, 7))
t.Run("Lists resources that start with db", listResourceIDs(conjur, &ResourceFilter{Search: "db"}, 2))
t.Run("Lists variables that start with prod/database", listResourceIDs(conjur, &ResourceFilter{Search: "prod/database", Kind: "variable"}, 2))
t.Run("Lists variables that start with prod", listResourceIDs(conjur, &ResourceFilter{Search: "prod", Kind: "variable"}, 4))
t.Run("Lists resources and limit result to 1", listResourceIDs(conjur, &ResourceFilter{Limit: 1}, 1))
t.Run("Lists resources after the first", listResourceIDs(conjur, &ResourceFilter{Offset: 1}, 10))
}

0 comments on commit dbc8cbd

Please sign in to comment.