Skip to content

Commit

Permalink
Merge pull request #52 from 1Password/eddy/get-vault-name-id
Browse files Browse the repository at this point in the history
Improve fetching a vault by either name or UUID
  • Loading branch information
edif2008 authored May 12, 2022
2 parents 8cf69f9 + b3c4cb2 commit 75cb1ce
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
33 changes: 30 additions & 3 deletions connect/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
type Client interface {
GetVaults() ([]onepassword.Vault, error)
GetVault(uuid string) (*onepassword.Vault, error)
GetVaultByUUID(uuid string) (*onepassword.Vault, error)
GetVaultsByTitle(uuid string) ([]onepassword.Vault, error)
GetItem(uuid string, vaultUUID string) (*onepassword.Item, error)
GetItems(vaultUUID string) ([]onepassword.Item, error)
Expand Down Expand Up @@ -139,13 +140,23 @@ func (rs *restClient) GetVaults() ([]onepassword.Vault, error) {
return vaults, nil
}

// GetVaults Get a list of all available vaults
func (rs *restClient) GetVault(uuid string) (*onepassword.Vault, error) {
// GetVault Get a vault based on its name or ID
func (rs *restClient) GetVault(vaultQuery string) (*onepassword.Vault, error) {
if vaultQuery == "" {
return nil, fmt.Errorf("Please provide either the vault name or its ID.")
}
if !isValidUUID(vaultQuery) {
return rs.GetVaultByTitle(vaultQuery)
}
return rs.GetVaultByUUID(vaultQuery)
}

func (rs *restClient) GetVaultByUUID(uuid string) (*onepassword.Vault, error) {
if !isValidUUID(uuid) {
return nil, vaultUUIDError
}

span := rs.tracer.StartSpan("GetVault")
span := rs.tracer.StartSpan("GetVaultByUUID")
defer span.Finish()

vaultURL := fmt.Sprintf("/v1/vaults/%s", uuid)
Expand All @@ -166,6 +177,22 @@ func (rs *restClient) GetVault(uuid string) (*onepassword.Vault, error) {
return &vault, nil
}

func (rs *restClient) GetVaultByTitle(vaultName string) (*onepassword.Vault, error) {
span := rs.tracer.StartSpan("GetVaultByTitle")
defer span.Finish()

vaults, err := rs.GetVaultsByTitle(vaultName)
if err != nil {
return nil, err
}

if len(vaults) != 1 {
return nil, fmt.Errorf("Found %d vaults with title %q", len(vaults), vaultName)
}

return &vaults[0], nil
}

func (rs *restClient) GetVaultsByTitle(title string) ([]onepassword.Vault, error) {
span := rs.tracer.StartSpan("GetVaultsByTitle")
defer span.Finish()
Expand Down
35 changes: 32 additions & 3 deletions connect/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,40 @@ func Test_restClient_GetVault(t *testing.T) {
assert.Equal(t, expectedVault, vault, "retrieved vault is not as expected")
}

func Test_restClient_GetVaultEmptyUUID(t *testing.T) {
func Test_restClient_GetVaultByID(t *testing.T) {
expectedVault := &onepassword.Vault{
Name: "Test vault",
Description: "Test Vault description",
ID: testID,
}

mockHTTPClient.Dofunc = getVault(expectedVault)
vault, err := testClient.GetVaultByUUID(expectedVault.ID)

assert.Nil(t, err)
assert.Equal(t, expectedVault, vault, "retrieved vault is not as expected")
}

func Test_restClient_GetVaultByTitle(t *testing.T) {
expectedVault := &onepassword.Vault{
Name: "Test vault",
Description: "Test Vault description",
ID: testID,
}

mockHTTPClient.Dofunc = listVaults
vault, err := testClient.GetVaultByTitle(expectedVault.Name)

assert.Nil(t, err)
assert.Equal(t, expectedVault, vault, "retrieved vault is not as expected")
}

func Test_restClient_GetVaultEmptyQuery(t *testing.T) {
errResult := apiError(http.StatusNotFound, "Vault not found")
mockHTTPClient.Dofunc = respondError(errResult)
_, err := testClient.GetVault("")

assert.EqualError(t, err, "malformed vault uuid provided")
assert.EqualError(t, err, "Please provide either the vault name or its ID.")
}

func Test_restClient_GetVaultError(t *testing.T) {
Expand Down Expand Up @@ -512,7 +540,8 @@ func respondError(apiErr *onepassword.Error) func(req *http.Request) (*http.Resp
func listVaults(req *http.Request) (*http.Response, error) {
vaults := []onepassword.Vault{
{
Description: "Test Vault",
Name: "Test vault",
Description: "Test Vault description",
ID: testID,
},
}
Expand Down

0 comments on commit 75cb1ce

Please sign in to comment.