From b33b2bd11fa2149cf28e25767366f5a9f18cc0dc Mon Sep 17 00:00:00 2001 From: Steve Ramage <49958178+steve-r-west@users.noreply.github.com> Date: Fri, 29 Dec 2023 12:32:06 -0800 Subject: [PATCH] Resolves #413 - Allow runbooks to skip alias processing (#414) --- cmd/create.go | 11 ++++++++--- cmd/docs.go | 2 +- cmd/get.go | 11 ++++++++--- cmd/login.go | 12 ++++++------ cmd/reset-store.go | 10 +++++----- cmd/root.go | 1 - cmd/update.go | 11 ++++++++--- external/aliases/aliases.go | 7 ------- 8 files changed, 36 insertions(+), 29 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index e048550..d2d5b0f 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -46,6 +46,7 @@ func NewCreateCommand(parentCmd *cobra.Command) func() { var setAlias = "" var ifAliasExists = "" var ifAliasDoesNotExist = "" + var skipAliases = false resetFunc := func() { autoFillOnCreate = false @@ -56,6 +57,7 @@ func NewCreateCommand(parentCmd *cobra.Command) func() { ifAliasDoesNotExist = "" overrides.OverrideUrlPath = "" overrides.QueryParameters = nil + skipAliases = false } for _, resource := range resources.GetPluralResources() { @@ -95,7 +97,7 @@ func NewCreateCommand(parentCmd *cobra.Command) func() { } } - body, err := createInternal(context.Background(), overrides, append([]string{resourceName}, args...), autoFillOnCreate, setAlias) + body, err := createInternal(context.Background(), overrides, append([]string{resourceName}, args...), autoFillOnCreate, setAlias, skipAliases) if err != nil { return err @@ -208,13 +210,14 @@ func NewCreateCommand(parentCmd *cobra.Command) func() { createCmd.PersistentFlags().StringVarP(&setAlias, "save-as-alias", "", "", "A name to save the created resource as") createCmd.PersistentFlags().StringVarP(&ifAliasExists, "if-alias-exists", "", "", "If the alias exists we will run this command, otherwise exit with no error") createCmd.PersistentFlags().StringVarP(&ifAliasDoesNotExist, "if-alias-does-not-exist", "", "", "If the alias does not exist we will run this command, otherwise exit with no error") + createCmd.PersistentFlags().BoolVarP(&skipAliases, "skip-alias-processing", "", false, "if set, we don't process the response for aliases") createCmd.MarkFlagsMutuallyExclusive("if-alias-exists", "if-alias-does-not-exist") _ = createCmd.RegisterFlagCompletionFunc("output-jq", jqCompletionFunc) return resetFunc } -func createInternal(ctx context.Context, overrides *httpclient.HttpParameterOverrides, args []string, autoFillOnCreate bool, aliasName string) (string, error) { +func createInternal(ctx context.Context, overrides *httpclient.HttpParameterOverrides, args []string, autoFillOnCreate bool, aliasName string, skipAliases bool) (string, error) { shutdown.OutstandingOpCounter.Add(1) defer shutdown.OutstandingOpCounter.Done() @@ -321,7 +324,9 @@ func createInternal(ctx context.Context, overrides *httpclient.HttpParameterOver // 204 is no content, so we will skip it. if resp.StatusCode != 204 { - aliases.SaveAliasesForResources(string(resBody)) + if !skipAliases { + aliases.SaveAliasesForResources(string(resBody)) + } } if aliasName != "" { diff --git a/cmd/docs.go b/cmd/docs.go index b4fdbd7..d21cc7f 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -108,7 +108,7 @@ func openDoc(resourceDoc resources.Resource, verb string) error { } if err != nil { - return err + return fmt.Errorf("could not open docs for resource '%s', action'%s': %w", resourceDoc.PluralName, verb, err) } return nil } diff --git a/cmd/get.go b/cmd/get.go index f7a8bf3..283a8f7 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -35,6 +35,7 @@ func NewGetCommand(parentCmd *cobra.Command) func() { var retryWhileJQMaxAttempts = uint16(1200) var ifAliasExists = "" var ifAliasDoesNotExist = "" + var skipAliases = false resetFunc := func() { overrides.QueryParameters = nil @@ -45,6 +46,7 @@ func NewGetCommand(parentCmd *cobra.Command) func() { retryWhileJQMaxAttempts = uint16(1200) ifAliasExists = "" ifAliasDoesNotExist = "" + skipAliases = false } var getCmd = &cobra.Command{ @@ -136,7 +138,7 @@ func NewGetCommand(parentCmd *cobra.Command) func() { retriesFailedError := fmt.Errorf("Maximum number of retries hit %d and condition [%s] always true", retryWhileJQMaxAttempts, retryWhileJQ) for attempt := uint16(0); attempt < retryWhileJQMaxAttempts; attempt++ { - body, err = getInternal(context.Background(), overrides, append([]string{resourceName}, args...)) + body, err = getInternal(context.Background(), overrides, append([]string{resourceName}, args...), skipAliases) if retryWhileJQ == "" { retriesFailedError = nil break @@ -268,6 +270,7 @@ func NewGetCommand(parentCmd *cobra.Command) func() { getCmd.PersistentFlags().Uint16VarP(&retryWhileJQMaxAttempts, "retry-while-jq-max-attempts", "", 1200, "The maximum number of attempts we will retry with jq") getCmd.PersistentFlags().StringVarP(&ifAliasExists, "if-alias-exists", "", "", "If the alias exists we will run this command, otherwise exit with no error") getCmd.PersistentFlags().StringVarP(&ifAliasDoesNotExist, "if-alias-does-not-exist", "", "", "If the alias does not exist we will run this command, otherwise exit with no error") + getCmd.PersistentFlags().BoolVarP(&skipAliases, "skip-alias-processing", "", false, "if set, we don't process the response for aliases") getCmd.MarkFlagsMutuallyExclusive("if-alias-exists", "if-alias-does-not-exist") _ = getCmd.RegisterFlagCompletionFunc("output-jq", jqCompletionFunc) @@ -276,7 +279,7 @@ func NewGetCommand(parentCmd *cobra.Command) func() { return resetFunc } -func getInternal(ctx context.Context, overrides *httpclient.HttpParameterOverrides, args []string) (string, error) { +func getInternal(ctx context.Context, overrides *httpclient.HttpParameterOverrides, args []string, skipAliases bool) (string, error) { resp, err := getResource(ctx, overrides, args) if err != nil { @@ -300,7 +303,9 @@ func getInternal(ctx context.Context, overrides *httpclient.HttpParameterOverrid return "", fmt.Errorf(resp.Status) } - aliases.SaveAliasesForResources(string(body)) + if !skipAliases { + aliases.SaveAliasesForResources(string(body)) + } return string(body), nil } else { diff --git a/cmd/login.go b/cmd/login.go index bd8db4e..e862a43 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -319,7 +319,7 @@ var loginCustomer = &cobra.Command{ newArgs = append(newArgs, "customer-token") newArgs = append(newArgs, args...) - body, err := createInternal(ctx, overrides, newArgs, false, "") + body, err := createInternal(ctx, overrides, newArgs, false, "", false) if err != nil { log.Warnf("Login not completed successfully") @@ -349,7 +349,7 @@ var loginCustomer = &cobra.Command{ if customerTokenResponse != nil { // Get the customer so we have aliases where we need the id. - getCustomerBody, err := getInternal(ctx, overrides, []string{"customer", customerTokenResponse.Data.CustomerId}) + getCustomerBody, err := getInternal(ctx, overrides, []string{"customer", customerTokenResponse.Data.CustomerId}, false) if err != nil { log.Warnf("Could not retrieve customer") @@ -448,7 +448,7 @@ var loginAccountManagement = &cobra.Command{ } // Populate an alias to get the authentication_realm. - _, err := getInternal(ctx, overrides, []string{"account-authentication-settings"}) + _, err := getInternal(ctx, overrides, []string{"account-authentication-settings"}, false) if err != nil { return fmt.Errorf("couldn't determine authentication realm: %w", err) @@ -473,7 +473,7 @@ var loginAccountManagement = &cobra.Command{ // Try and auto-detect the password profile id if passwordAuthentication { - resp, err := getInternal(ctx, overrides, []string{"password-profiles", "related_authentication_realm_for_account_authentication_settings_last_read=entity"}) + resp, err := getInternal(ctx, overrides, []string{"password-profiles", "related_authentication_realm_for_account_authentication_settings_last_read=entity"}, false) if err != nil { return fmt.Errorf("couldn't determine password profile: %w", err) @@ -528,7 +528,7 @@ var loginAccountManagement = &cobra.Command{ } // Do the login and get back a list of accounts - body, err := createInternal(ctx, overrides, loginArgs, false, "") + body, err := createInternal(ctx, overrides, loginArgs, false, "", false) if err != nil { log.Warnf("Login not completed successfully") @@ -588,7 +588,7 @@ var loginAccountManagement = &cobra.Command{ authentication.SaveAccountManagementAuthenticationToken(*selectedAccount) - accountMembers, err := getInternal(ctx, overrides, []string{"account-members"}) + accountMembers, err := getInternal(ctx, overrides, []string{"account-members"}, false) if err == nil { accountMemberId, _ := json.RunJQOnString(".data[0].id", accountMembers) diff --git a/cmd/reset-store.go b/cmd/reset-store.go index 6e4db44..4e222c8 100644 --- a/cmd/reset-store.go +++ b/cmd/reset-store.go @@ -56,25 +56,25 @@ var ResetStore = &cobra.Command{ // We would also need locking to go faster. // Get customer and account authentication settings to populate the aliases - _, err = getInternal(ctx, overrides, []string{"customer-authentication-settings"}) + _, err = getInternal(ctx, overrides, []string{"customer-authentication-settings"}, false) if err != nil { errors = append(errors, err.Error()) } - _, err = getInternal(ctx, overrides, []string{"account-authentication-settings"}) + _, err = getInternal(ctx, overrides, []string{"account-authentication-settings"}, false) if err != nil { errors = append(errors, err.Error()) } - _, err = getInternal(ctx, overrides, []string{"merchant-realm-mappings"}) + _, err = getInternal(ctx, overrides, []string{"merchant-realm-mappings"}, false) if err != nil { errors = append(errors, err.Error()) } - _, err = getInternal(ctx, overrides, []string{"authentication-realms"}) + _, err = getInternal(ctx, overrides, []string{"authentication-realms"}, false) if err != nil { errors = append(errors, err.Error()) @@ -180,7 +180,7 @@ func resetResourcesUndeletableResources(ctx context.Context, overrides *httpclie errors := make([]string, 0) for _, resetCmd := range resetCmds { - body, err := updateInternal(ctx, overrides, resetCmd) + body, err := updateInternal(ctx, overrides, false, resetCmd) if err != nil { errors = append(errors, fmt.Errorf("error resetting %s: %v", resetCmd[0], err).Error()) diff --git a/cmd/root.go b/cmd/root.go index 9c857d1..92ddc6f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -115,7 +115,6 @@ func InitializeCmd() { RootCmd.PersistentFlags().Float32VarP(&requestTimeout, "timeout", "", 60, "Request timeout in seconds (fractional values allowed)") RootCmd.PersistentFlags().Uint16VarP(&statisticsFrequency, "statistics-frequency", "", 15, "How often to print runtime statistics (0 turns them off)") - RootCmd.PersistentFlags().BoolVarP(&aliases.SkipAliasProcessing, "skip-alias-processing", "", false, "if set, we don't process the response for aliases") ResetStore.ResetFlags() ResetStore.PersistentFlags().BoolVarP(&DeleteApplicationKeys, "delete-application-keys", "", false, "if set, we delete application keys as well") diff --git a/cmd/update.go b/cmd/update.go index ed07de0..33b459d 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -28,6 +28,7 @@ func NewUpdateCommand(parentCmd *cobra.Command) func() { var noBodyPrint = false var ifAliasExists = "" var ifAliasDoesNotExist = "" + var skipAliases = false resetFunc := func() { overrides.QueryParameters = nil @@ -36,6 +37,7 @@ func NewUpdateCommand(parentCmd *cobra.Command) func() { noBodyPrint = false ifAliasExists = "" ifAliasDoesNotExist = "" + skipAliases = false } var updateCmd = &cobra.Command{ @@ -85,7 +87,7 @@ func NewUpdateCommand(parentCmd *cobra.Command) func() { } } - body, err := updateInternal(context.Background(), overrides, append([]string{resourceName}, args...)) + body, err := updateInternal(context.Background(), overrides, skipAliases, append([]string{resourceName}, args...)) if err != nil { return err @@ -180,6 +182,7 @@ func NewUpdateCommand(parentCmd *cobra.Command) func() { updateCmd.PersistentFlags().StringVarP(&ifAliasExists, "if-alias-exists", "", "", "If the alias exists we will run this command, otherwise exit with no error") updateCmd.PersistentFlags().StringVarP(&ifAliasDoesNotExist, "if-alias-does-not-exist", "", "", "If the alias does not exist we will run this command, otherwise exit with no error") updateCmd.MarkFlagsMutuallyExclusive("if-alias-exists", "if-alias-does-not-exist") + updateCmd.PersistentFlags().BoolVarP(&skipAliases, "skip-alias-processing", "", false, "if set, we don't process the response for aliases") _ = updateCmd.RegisterFlagCompletionFunc("output-jq", jqCompletionFunc) parentCmd.AddCommand(updateCmd) @@ -187,7 +190,7 @@ func NewUpdateCommand(parentCmd *cobra.Command) func() { } -func updateInternal(ctx context.Context, overrides *httpclient.HttpParameterOverrides, args []string) (string, error) { +func updateInternal(ctx context.Context, overrides *httpclient.HttpParameterOverrides, skipAliases bool, args []string) (string, error) { shutdown.OutstandingOpCounter.Add(1) defer shutdown.OutstandingOpCounter.Done() @@ -261,7 +264,9 @@ func updateInternal(ctx context.Context, overrides *httpclient.HttpParameterOver // 204 is no content, so we will skip it. if resp.StatusCode != 204 { - aliases.SaveAliasesForResources(string(resBody)) + if !skipAliases { + aliases.SaveAliasesForResources(string(resBody)) + } } return string(resBody), nil diff --git a/external/aliases/aliases.go b/external/aliases/aliases.go index 67d43db..3d95c17 100644 --- a/external/aliases/aliases.go +++ b/external/aliases/aliases.go @@ -26,7 +26,6 @@ var typeToAliasNameToIdMap = map[string]map[string]*id.IdableAttributes{} var dirtyAliases = map[string]bool{} -var SkipAliasProcessing = false var typeToIdToAliasNamesMap = map[string]map[string]map[string]bool{} func ClearAllAliasesForJsonApiType(jsonApiType string) error { @@ -161,9 +160,6 @@ func ResolveAliasValuesOrReturnIdentity(jsonApiType string, alternateJsonApiType } func SaveAliasesForResources(jsonTxt string) { - if SkipAliasProcessing { - return - } var jsonStruct = map[string]interface{}{} err := json.Unmarshal([]byte(jsonTxt), &jsonStruct) if err != nil { @@ -183,9 +179,6 @@ func SaveAliasesForResources(jsonTxt string) { } func SetAliasForResource(jsonTxt string, name string) { - if SkipAliasProcessing { - return - } var jsonResult = map[string]interface{}{} err := json.Unmarshal([]byte(jsonTxt), &jsonResult) if err != nil {