From cbf1a78c08c30398bac74ca34ada60cfe677ecd6 Mon Sep 17 00:00:00 2001 From: Bryan Karaffa Date: Tue, 24 Sep 2024 12:50:21 -0700 Subject: [PATCH] feat: improve error message for credential `not_found` scenario --- cmd/fpt/retrieve_data.go | 12 ++++++++++++ cmd/fpt/run.go | 21 ++++++++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/cmd/fpt/retrieve_data.go b/cmd/fpt/retrieve_data.go index 2a3e835..a9f125a 100644 --- a/cmd/fpt/retrieve_data.go +++ b/cmd/fpt/retrieve_data.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "os" + "strings" "github.com/rightscale/policy_sdk/client/policy" appliedpolicy "github.com/rightscale/policy_sdk/sdk/applied_policy" @@ -35,6 +36,17 @@ func policyTemplateRetrieveData(ctx context.Context, cli policy.Client, file str fmt.Printf("Retrieving Data from PolicyTemplate (%s)\n", pt.Href) rd, err := cli.RetrieveData(ctx, pt.ID, names, options, credentials) if err != nil { + // Instead of returning "not_found" error, return a more descriptive error message + // "not_found" is usually due to an issue with the credential ID(s) specified + if err.Error() == "not_found" { + // Get list of values from credentials map + var creds []string + for k := range credentials { + creds = append(creds, k) + } + // Update error message + err = fmt.Errorf("At least one credential identifier not found -- please check the credential ID(s) specified. " + strings.Join(creds, ", ")) + } return err } for _, d := range rd { diff --git a/cmd/fpt/run.go b/cmd/fpt/run.go index c70c9f1..ea5ad57 100644 --- a/cmd/fpt/run.go +++ b/cmd/fpt/run.go @@ -19,11 +19,11 @@ import ( ) // Steps: -// 1. Upload policy template. If it exists already, update it. -// 2. Apply policy template. -// 3. Print log as we go (if --tail option) -// 4. Print escalation status as we go (if --tail option) -// 5. Cleanup (stop applied policy, delete policy template) +// 1. Upload policy template. If it exists already, update it. +// 2. Apply policy template. +// 3. Print log as we go (if --tail option) +// 4. Print escalation status as we go (if --tail option) +// 5. Cleanup (stop applied policy, delete policy template) func policyTemplateRun(ctx context.Context, cli policy.Client, file string, runOptions []string, runCredentials []string, keep bool, dryRun bool, noLog bool) error { fmt.Printf("Running %s\n", file) pt, err := doUpload(ctx, cli, file) @@ -73,6 +73,17 @@ func policyTemplateRun(ctx context.Context, cli policy.Client, file string, runO } ap, err := cli.CreateAppliedPolicy(ctx, p) if err != nil { + // Instead of returning "not_found" error, return a more descriptive error message + // "not_found" is usually due to an issue with the credential ID(s) specified + if err.Error() == "not_found" { + // Get list of values from credentials map + var creds []string + for k := range credentials { + creds = append(creds, k) + } + // Update error message + err = fmt.Errorf("At least one credential identifier not found -- please check the credential ID(s) specified. " + strings.Join(creds, ", ")) + } return err } if !keep {