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

Support pagination on list apps for profile init #205

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .changelog/205.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
profile: Add support for listing all apps when configuring profile vault-secrets apps
```
39 changes: 27 additions & 12 deletions internal/commands/profile/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,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"
Expand Down Expand Up @@ -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:
Expand All @@ -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 }}`,
Expand All @@ -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)
},
}
Expand All @@ -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()
Expand Down
Loading