From c2b3f90efbe0130f323d2598f03a71bad0979fc2 Mon Sep 17 00:00:00 2001 From: Dan Enman Date: Fri, 13 Apr 2018 10:51:56 -0300 Subject: [PATCH 1/3] Use `project_id` for project exports and runs on credentials call --- cmd/alias.go | 2 +- cmd/export.go | 60 ++++++++++++++++++++++++++++++++---------- cmd/run.go | 30 ++++++++++++++++----- specs/marketplace.yaml | 11 +++++++- 4 files changed, 80 insertions(+), 23 deletions(-) diff --git a/cmd/alias.go b/cmd/alias.go index ca2992a..a51b0b6 100644 --- a/cmd/alias.go +++ b/cmd/alias.go @@ -114,7 +114,7 @@ func aliasCredentialCmd(cliCtx *cli.Context) error { } prompts.SpinStart("Fetching Credentials") - cMap, err := fetchCredentials(ctx, client.Marketplace, []*models.Resource{selectedResource}, false) + cMap, err := fetchResourceCredentials(ctx, client.Marketplace, []*models.Resource{selectedResource}, false) prompts.SpinStop() if err != nil { return cli.NewExitError("Could not retrieve credentials: "+err.Error(), -1) diff --git a/cmd/export.go b/cmd/export.go index 22c9f01..4281e8e 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -65,6 +65,7 @@ func export(cliCtx *cli.Context) error { return err } + // we need to fetch all the credentials for the rMap for resource naming, etc prompts.SpinStart("Fetching Resources") resources, err := clients.FetchResources(ctx, client.Marketplace, teamID, projectName) prompts.SpinStop() @@ -72,17 +73,26 @@ func export(cliCtx *cli.Context) error { return cli.NewExitError("Could not retrieve resources: "+err.Error(), -1) } - if projectName == "" { - resources = filterResourcesWithoutProjects(resources) - } - sort.Slice(resources, func(i, j int) bool { return resources[i].Body.Name < resources[j].Body.Name }) - cMap, err := fetchCredentials(ctx, client.Marketplace, resources, true) - if err != nil { - return cli.NewExitError("Could not retrieve credentials: "+err.Error(), -1) + cMap := make(map[manifold.ID][]*models.Credential) + if projectName == "" { + p, err := clients.FetchProjectByLabel(ctx, client.Marketplace, teamID, projectName) + if err != nil { + return cli.NewExitError(fmt.Sprintf("Could not retrieve project: %s", err), -1) + } + + cMap, err = fetchProjectCredentials(ctx, client.Marketplace, p, true) + if err != nil { + return cli.NewExitError(fmt.Sprintf("Could not retrieve credentials: %s", err), -1) + } + } else { + cMap, err = fetchResourceCredentials(ctx, client.Marketplace, resources, true) + if err != nil { + return cli.NewExitError("Could not retrieve credentials: "+err.Error(), -1) + } } params := map[string]string{ @@ -185,8 +195,8 @@ func indexResources(resources []*models.Resource) map[manifold.ID]*models.Resour return index } -func fetchCredentials(ctx context.Context, m *mClient.Marketplace, resources []*models.Resource, customNames bool) (map[manifold.ID][]*models.Credential, error) { - cMap := make(map[manifold.ID][]*models.Credential) +func fetchResourceCredentials(ctx context.Context, m *mClient.Marketplace, resources []*models.Resource, customNames bool) (map[manifold.ID][]*models.Credential, error) { + p := credential.NewGetCredentialsParamsWithContext(ctx) if customNames == false { noCustomNames := "false" @@ -197,19 +207,41 @@ func fetchCredentials(ctx context.Context, m *mClient.Marketplace, resources []* var resourceIDs []string for _, r := range resources { resourceIDs = append(resourceIDs, r.ID.String()) - if _, ok := cMap[r.ID]; !ok { - cMap[r.ID] = []*models.Credential{} - } } p.SetResourceID(resourceIDs) - // Get credentials for all the defined resources with one call - c, err := m.Credential.GetCredentials(p, nil) + return fetchCredentials(m, p) +} + +func fetchProjectCredentials(ctx context.Context, m *mClient.Marketplace, project *models.Project, customNames bool) (map[manifold.ID][]*models.Credential, error) { + _ = make(map[manifold.ID][]*models.Credential) + p := credential.NewGetCredentialsParamsWithContext(ctx) + + if customNames == false { + noCustomNames := "false" + p.SetCustomNames(&noCustomNames) + } + + pid := project.ID.String() + p.SetProjectID(&pid) + + return fetchCredentials(m, p) +} + +func fetchCredentials(m *mClient.Marketplace, params *credential.GetCredentialsParams) (map[manifold.ID][]*models.Credential, error) { + cMap := make(map[manifold.ID][]*models.Credential) + + // Get credentials for all the defined resources withring(call + c, err := m.Credential.GetCredentials(params, nil) if err != nil { return nil, err } // Append credential results to the map for _, credential := range c.Payload { + rid := credential.Body.ResourceID + if _, ok := cMap[rid]; !ok { + cMap[rid] = []*models.Credential{} + } cMap[credential.Body.ResourceID] = append(cMap[credential.Body.ResourceID], credential) } diff --git a/cmd/run.go b/cmd/run.go index b3572d4..651ab78 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -11,9 +11,11 @@ import ( "github.com/urfave/cli" + "github.com/manifoldco/go-manifold" "github.com/manifoldco/manifold-cli/api" "github.com/manifoldco/manifold-cli/clients" "github.com/manifoldco/manifold-cli/errs" + "github.com/manifoldco/manifold-cli/generated/marketplace/models" "github.com/manifoldco/manifold-cli/middleware" "github.com/manifoldco/manifold-cli/session" ) @@ -58,14 +60,28 @@ func run(cliCtx *cli.Context) error { return err } - rs, err := clients.FetchResources(ctx, client.Marketplace, teamID, projectName) - if err != nil { - return cli.NewExitError("Could not retrieve resources: "+err.Error(), -1) - } + cMap := make(map[manifold.ID][]*models.Credential) - cMap, err := fetchCredentials(ctx, client.Marketplace, rs, true) - if err != nil { - return cli.NewExitError("Could not retrieve credentials: "+err.Error(), -1) + if projectName != "" { + p, err := clients.FetchProjectByLabel(ctx, client.Marketplace, teamID, projectName) + if err != nil { + return cli.NewExitError(fmt.Sprintf("Could not retrieve project: %s", err), -1) + } + + cMap, err = fetchProjectCredentials(ctx, client.Marketplace, p, true) + if err != nil { + return cli.NewExitError(fmt.Sprintf("Could not retrieve credentials: %s", err), -1) + } + } else { + rs, err := clients.FetchResources(ctx, client.Marketplace, teamID, projectName) + if err != nil { + return cli.NewExitError("Could not retrieve resources: "+err.Error(), -1) + } + + cMap, err = fetchResourceCredentials(ctx, client.Marketplace, rs, true) + if err != nil { + return cli.NewExitError("Could not retrieve credentials: "+err.Error(), -1) + } } credentials, err := flattenCMap(cMap) diff --git a/specs/marketplace.yaml b/specs/marketplace.yaml index 563cfc7..70daa49 100644 --- a/specs/marketplace.yaml +++ b/specs/marketplace.yaml @@ -262,10 +262,19 @@ paths: base32 encoded 18 byte identifier. collectionFormat: multi type: array - required: true + required: false items: format: base32ID type: string + - name: project_id + in: query + description: | + ID of the Project to filter Credentials by, stored as a + base32 encoded 18 byte identifier. + type: string + pattern: '^[0-9abcdefghjkmnpqrtuvwxyz]{29}$' + format: base32ID + required: false tags: - Credential responses: From ce8c94887ab6912a29025bc83e0cff704f628981 Mon Sep 17 00:00:00 2001 From: Dan Enman Date: Fri, 13 Apr 2018 10:55:55 -0300 Subject: [PATCH 2/3] Use 'Fetch Credentials' event for tracking CLI analytics --- cmd/export.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/export.go b/cmd/export.go index 4281e8e..4e1c8b3 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -102,7 +102,7 @@ func export(cliCtx *cli.Context) error { params["project"] = projectName } - client.Analytics.Track(ctx, "Exported Credentials", ¶ms) + client.Analytics.Track(ctx, "Fetch Credentials", ¶ms) rMap := indexResources(resources) w := os.Stdout From 91af169ee3f624ffb263807b952e94e757257121 Mon Sep 17 00:00:00 2001 From: Dan Enman Date: Fri, 13 Apr 2018 10:58:22 -0300 Subject: [PATCH 3/3] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e2c145..51953ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Fixed - Export now only makes one call to retrieve all credentials +- Export not uses project_id for better server side credentials fetching ## [0.13.0] - 2017-12-13