From b806a63d55067ffc4fbdc8f5ee3c7f402294b18c Mon Sep 17 00:00:00 2001 From: Mercedes Hall Date: Wed, 18 Dec 2024 12:32:22 -0600 Subject: [PATCH 1/3] support pagination on list apps for profile init --- internal/commands/profile/init.go | 39 +++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/internal/commands/profile/init.go b/internal/commands/profile/init.go index a19f76e3..abb91d89 100644 --- a/internal/commands/profile/init.go +++ b/internal/commands/profile/init.go @@ -7,6 +7,7 @@ import ( "context" "errors" "fmt" + "github.com/hashicorp/hcp-sdk-go/clients/cloud-vault-secrets/stable/2023-11-28/models" "io" "net/http" "strings" @@ -169,16 +170,30 @@ func (i *InitOpts) configureOrgAndProject() error { } func (i *InitOpts) configureVaultSecrets() error { - // Retrieve apps associated with org and project ID - listAppReq := preview_secret_service.NewListAppsParamsWithContext(i.Ctx) - listAppReq.OrganizationID = i.Profile.OrganizationID - listAppReq.ProjectID = i.Profile.ProjectID - listAppResp, err := i.SecretService.ListApps(listAppReq, nil) - if err != nil { - return err + params := &preview_secret_service.ListAppsParams{ + Context: i.Ctx, + ProjectID: i.Profile.ProjectID, + OrganizationID: i.Profile.OrganizationID, + } + + var apps []*models.Secrets20231128App + for { + resp, err := i.SecretService.ListApps(params, nil) + + if err != nil { + return fmt.Errorf("failed to list apps: %w", err) + } + + apps = append(apps, resp.Payload.Apps...) + if resp.Payload.Pagination == nil || resp.Payload.Pagination.NextPageToken == "" { + break + } + + next := resp.Payload.Pagination.NextPageToken + params.PaginationNextPageToken = &next } - appCount := len(listAppResp.Payload.Apps) + appCount := len(apps) if appCount <= 0 { appsCreateDoc := heredoc.New(i.IO, heredoc.WithPreserveNewlines()).Must(` No Vault Secrets application found. Create one and set on the active profile by issuing: @@ -190,11 +205,11 @@ No Vault Secrets application found. Create one and set on the active profile by return nil } - appName := listAppResp.Payload.Apps[0].Name + appName := apps[0].Name if appCount > 1 { prompt := promptui.Select{ Label: "Multiple apps found. Please select the one you would like to configure.", - Items: listAppResp.Payload.Apps, + Items: apps, Templates: &promptui.SelectTemplates{ Active: `> {{ .Name }}`, Inactive: `{{ .Name }}`, @@ -210,7 +225,7 @@ No Vault Secrets application found. Create one and set on the active profile by Stdout: iostreams.NopWriteCloser(i.IO.Err()), Searcher: func(term string, index int) bool { term = strings.ToLower(term) - name := strings.ToLower(listAppResp.Payload.Apps[index].Name) + name := strings.ToLower(apps[index].Name) return strings.Contains(name, term) }, } @@ -219,7 +234,7 @@ No Vault Secrets application found. Create one and set on the active profile by if err != nil { return fmt.Errorf("prompt failed: %w", err) } - appName = listAppResp.Payload.Apps[i].Name + appName = apps[i].Name } cs := i.IO.ColorScheme() From efea02f5bb4d237b15885331fec6c75efcf541c8 Mon Sep 17 00:00:00 2001 From: Mercedes Hall Date: Wed, 18 Dec 2024 13:25:10 -0600 Subject: [PATCH 2/3] lints --- internal/commands/profile/init.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/commands/profile/init.go b/internal/commands/profile/init.go index abb91d89..4837cc54 100644 --- a/internal/commands/profile/init.go +++ b/internal/commands/profile/init.go @@ -7,7 +7,6 @@ import ( "context" "errors" "fmt" - "github.com/hashicorp/hcp-sdk-go/clients/cloud-vault-secrets/stable/2023-11-28/models" "io" "net/http" "strings" @@ -17,6 +16,7 @@ import ( "github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/stable/2019-12-10/client/project_service" resources "github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/stable/2019-12-10/models" preview_secret_service "github.com/hashicorp/hcp-sdk-go/clients/cloud-vault-secrets/stable/2023-11-28/client/secret_service" + "github.com/hashicorp/hcp-sdk-go/clients/cloud-vault-secrets/stable/2023-11-28/models" "github.com/hashicorp/hcp/internal/pkg/cmd" "github.com/hashicorp/hcp/internal/pkg/flagvalue" "github.com/hashicorp/hcp/internal/pkg/heredoc" From 295dcc38fd8ff1c4ba59022a6d660e776af2faae Mon Sep 17 00:00:00 2001 From: Mercedes Hall Date: Wed, 18 Dec 2024 14:16:32 -0600 Subject: [PATCH 3/3] updates changelog --- .changelog/205.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/205.txt diff --git a/.changelog/205.txt b/.changelog/205.txt new file mode 100644 index 00000000..cdbfecfe --- /dev/null +++ b/.changelog/205.txt @@ -0,0 +1,3 @@ +```release-note:bug +profile: Add support for listing all apps when configuring profile vault-secrets apps +```