Skip to content

Commit

Permalink
Support pagination for Vault OpenAppSecrets preview API (#1109)
Browse files Browse the repository at this point in the history
* Support pagination for Vault OpenAppSecrets preview API

* Add changelog

* fix lint error

* Fix retry loop

* fix lint

* remove break

* undo last commit

* address review feedback
  • Loading branch information
divyapola5 authored Oct 8, 2024
1 parent 1ed05ef commit efee191
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .changelog/1109.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
Add pagination support for vault OpenAppSecrets preview api.
```
41 changes: 24 additions & 17 deletions internal/clients/vault_secrets_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,34 @@ func OpenVaultSecretsAppSecrets(ctx context.Context, client *Client, loc *shared

var secrets *secret_service.OpenAppSecretsOK
var err error
for attempt := 0; attempt < retryCount; attempt++ {
secrets, err = client.VaultSecretsPreview.OpenAppSecrets(params, nil)
if err != nil {
var serviceErr *secret_service.OpenAppSecretDefault
ok := errors.As(err, &serviceErr)
if !ok {
var result []*secretmodels.Secrets20231128OpenSecret

for {
for attempt := 0; attempt < retryCount; attempt++ {
secrets, err = client.VaultSecretsPreview.OpenAppSecrets(params, nil)
if err != nil {
var serviceErr *secret_service.OpenAppSecretDefault
ok := errors.As(err, &serviceErr)
if !ok {
return nil, err
}
if shouldRetryWithSleep(ctx, serviceErr, attempt, []int{http.StatusTooManyRequests}) {
continue
}
return nil, err
}
if shouldRetryWithSleep(ctx, serviceErr, attempt, []int{http.StatusTooManyRequests}) {
continue
}
return nil, err
break
}
break
}

if secrets == nil {
return nil, errors.New("unable to get secrets")
if secrets == nil {
return nil, errors.New("unable to get secrets")
}
result = append(result, secrets.GetPayload().Secrets...)
pagination := secrets.GetPayload().Pagination
if pagination == nil || pagination.NextPageToken == "" {
return result, nil
}
params.PaginationNextPageToken = &pagination.NextPageToken
}

return secrets.GetPayload().Secrets, nil
}

func GetRotatingSecretState(ctx context.Context, client *Client, loc *sharedmodels.HashicorpCloudLocationLocation, appName, secretName string) (*secretmodels.Secrets20231128RotatingSecretState, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,46 @@ func TestAcc_dataSourceVaultSecretsApp(t *testing.T) {
})
}

func TestAcc_VaultSecretsOpenAppSecretsPagination(t *testing.T) {
testAppName := generateRandomSlug()
dataSourceAddress := "data.hcp_vault_secrets_app.foo"
secretCount := 12

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create 12 secrets and validate that all are returned and not just the default v2 api page size 10
{
PreConfig: func() {
createTestApp(t, testAppName)

for i := 1; i <= secretCount; i++ {
createTestAppSecret(t, testAppName, "secret"+fmt.Sprint(i), "value"+fmt.Sprint(i))
}
},
Config: fmt.Sprintf(`
data "hcp_vault_secrets_app" "foo" {
app_name = %q
}`, testAppName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceAddress, "organization_id"),
resource.TestCheckResourceAttrSet(dataSourceAddress, "project_id"),
// default page size for OpenAppSecrets v2 api is 10, validate that all secrets (pages) are retrieved and not just 1st page (10)
resource.TestCheckResourceAttr(dataSourceAddress, "secrets.%", fmt.Sprintf("%v", secretCount)),
),
},
},
CheckDestroy: func(s *terraform.State) error {
for i := 1; i <= secretCount; i++ {
deleteTestAppSecret(t, testAppName, "secret"+fmt.Sprint(i))
}
deleteTestApp(t, testAppName)
return nil
},
})
}

func createTestApp(t *testing.T, appName string) {
t.Helper()

Expand Down

0 comments on commit efee191

Please sign in to comment.