diff --git a/connect/client.go b/connect/client.go index 66019c5..daf7e7f 100644 --- a/connect/client.go +++ b/connect/client.go @@ -43,6 +43,7 @@ type Client interface { GetItemByUUID(uuid string, vaultQuery string) (*onepassword.Item, error) GetItemByTitle(title string, vaultQuery string) (*onepassword.Item, error) GetItemsByTitle(title string, vaultQuery string) ([]onepassword.Item, error) + GetItemsByFilter(filter string, vaultQuery string) ([]onepassword.Item, error) CreateItem(item *onepassword.Item, vaultQuery string) (*onepassword.Item, error) UpdateItem(item *onepassword.Item, vaultQuery string) (*onepassword.Item, error) DeleteItem(item *onepassword.Item, vaultQuery string) error @@ -309,6 +310,8 @@ func (rs *restClient) GetItemByTitle(title string, vaultQuery string) (*onepassw } func (rs *restClient) GetItemsByTitle(title string, vaultQuery string) ([]onepassword.Item, error) { + filter := fmt.Sprintf("title eq \"%s\"", title) + vaultUUID, err := rs.getVaultUUID(vaultQuery) if err != nil { return nil, err @@ -317,8 +320,23 @@ func (rs *restClient) GetItemsByTitle(title string, vaultQuery string) ([]onepas span := rs.tracer.StartSpan("GetItemsByTitle") defer span.Finish() - filter := url.QueryEscape(fmt.Sprintf("title eq \"%s\"", title)) - itemURL := fmt.Sprintf("/v1/vaults/%s/items?filter=%s", vaultUUID, filter) + return rs.getItemsByFilter(filter, vaultUUID, span) +} + +func (rs *restClient) GetItemsByFilter(filter string, vaultQuery string) ([]onepassword.Item, error) { + vaultUUID, err := rs.getVaultUUID(vaultQuery) + if err != nil { + return nil, err + } + + span := rs.tracer.StartSpan("GetItemsByFilter") + defer span.Finish() + + return rs.getItemsByFilter(filter, vaultUUID, span) +} + +func (rs *restClient) getItemsByFilter(filter string, vaultUUID string, span opentracing.Span) ([]onepassword.Item, error) { + itemURL := fmt.Sprintf("/v1/vaults/%s/items?filter=%s", vaultUUID, url.QueryEscape(filter)) request, err := rs.buildRequest(http.MethodGet, itemURL, http.NoBody, span) if err != nil { return nil, err diff --git a/connect/client_test.go b/connect/client_test.go index 2f3057b..537d046 100644 --- a/connect/client_test.go +++ b/connect/client_test.go @@ -354,6 +354,26 @@ func Test_restClient_GetItemsByTitle(t *testing.T) { assert.Equal(t, items[0].Fields[1].Value, "appleseed") } +func Test_restClient_GetItemsByFilter(t *testing.T) { + mockHTTPClient.Dofunc = listItemsOrGetItem + items, err := testClient.GetItemsByFilter("tag eq \"test-items\"", testVaultUUID) + + if err != nil { + t.Logf("Unable to get item: %s", err.Error()) + t.FailNow() + } + + if len(items) != 1 { + t.Logf("Expected 1 item to exist in vault, found %d", len(items)) + t.FailNow() + } + + assert.Equal(t, items[0].Title, "test-item") + assert.NotEqual(t, len(items[0].Fields), 0) + assert.Equal(t, items[0].Fields[0].Value, "wendy") + assert.Equal(t, items[0].Fields[1].Value, "appleseed") +} + func Test_restClient_GetItemByTitle(t *testing.T) { defer reset()