Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to retrieve only resource IDs #140

Merged
merged 1 commit into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if selecting just the id field is something that can be done at the API level...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? This is the API. :)


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))
}