Skip to content

Commit

Permalink
Resolves #413 - Allow runbooks to skip alias processing (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-r-west authored Dec 29, 2023
1 parent c829f68 commit b33b2bd
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 29 deletions.
11 changes: 8 additions & 3 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func NewCreateCommand(parentCmd *cobra.Command) func() {
var setAlias = ""
var ifAliasExists = ""
var ifAliasDoesNotExist = ""
var skipAliases = false

resetFunc := func() {
autoFillOnCreate = false
Expand All @@ -56,6 +57,7 @@ func NewCreateCommand(parentCmd *cobra.Command) func() {
ifAliasDoesNotExist = ""
overrides.OverrideUrlPath = ""
overrides.QueryParameters = nil
skipAliases = false
}

for _, resource := range resources.GetPluralResources() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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 != "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 8 additions & 3 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,6 +46,7 @@ func NewGetCommand(parentCmd *cobra.Command) func() {
retryWhileJQMaxAttempts = uint16(1200)
ifAliasExists = ""
ifAliasDoesNotExist = ""
skipAliases = false
}

var getCmd = &cobra.Command{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions cmd/reset-store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
11 changes: 8 additions & 3 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,6 +37,7 @@ func NewUpdateCommand(parentCmd *cobra.Command) func() {
noBodyPrint = false
ifAliasExists = ""
ifAliasDoesNotExist = ""
skipAliases = false
}

var updateCmd = &cobra.Command{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -180,14 +182,15 @@ 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)

return resetFunc

}

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()

Expand Down Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions external/aliases/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit b33b2bd

Please sign in to comment.