Skip to content

Commit

Permalink
feat: improve error message for credential not_found scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
bryankaraffa committed Sep 24, 2024
1 parent 9c983ee commit cbf1a78
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
12 changes: 12 additions & 0 deletions cmd/fpt/retrieve_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 16 additions & 5 deletions cmd/fpt/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit cbf1a78

Please sign in to comment.