Skip to content

Commit

Permalink
Resolves #462 - Checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-r-west committed Aug 11, 2024
1 parent 7ce9bf7 commit ad73a7e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
47 changes: 43 additions & 4 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"net/http"
"net/url"
"strings"
"time"
)

func NewCreateCommand(parentCmd *cobra.Command) func() {
Expand All @@ -42,6 +43,8 @@ func NewCreateCommand(parentCmd *cobra.Command) func() {
// Ensure that any new options here are added to the resetFunc
var autoFillOnCreate = false
var noBodyPrint = false
var retryWhileJQ = ""
var retryWhileJQMaxAttempts = uint16(1200)
var outputJq = ""
var compactOutput = true
var setAlias = ""
Expand Down Expand Up @@ -105,8 +108,42 @@ func NewCreateCommand(parentCmd *cobra.Command) func() {
}
}

body, err := createInternal(context.Background(), overrides, append([]string{resourceName}, args...), autoFillOnCreate, setAlias, skipAliases)
var body string
var err error
if retryWhileJQMaxAttempts == 0 {
return fmt.Errorf("--retry-while-jq-max-attempts must be greater than 0")
}

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 = createInternal(context.Background(), overrides, append([]string{resourceName}, args...), autoFillOnCreate, setAlias, skipAliases)

if retryWhileJQ == "" {
retriesFailedError = nil
break
}

resultOfRetryWhileJQ, err := json.RunJQOnStringWithArray(retryWhileJQ, body)

if err != nil {
break
}

if len(resultOfRetryWhileJQ) > 0 {
if result, ok := resultOfRetryWhileJQ[0].(bool); ok {
if result {
time.Sleep(3 * time.Second)
continue
}
}
}

log.Infof("Result of JQ [%s] was: %v, retries complete", retryWhileJQ, resultOfRetryWhileJQ)
retriesFailedError = nil
break

}
if err != nil {
return err
}
Expand All @@ -132,11 +169,11 @@ func NewCreateCommand(parentCmd *cobra.Command) func() {
}
}

return nil
return retriesFailedError
}

if noBodyPrint {
return nil
return retriesFailedError
} else {
if compactOutput {
body, err = json.Compact(body)
Expand Down Expand Up @@ -228,6 +265,8 @@ func NewCreateCommand(parentCmd *cobra.Command) func() {
createCmd.PersistentFlags().StringVarP(&outputJq, "output-jq", "", "", "A jq expression, if set we will restrict output to only this")
createCmd.PersistentFlags().BoolVarP(&compactOutput, "compact", "", false, "Hides some of the boiler plate keys and empty fields, etc...")
createCmd.PersistentFlags().BoolVarP(&ignoreErrors, "ignore-errors", "", false, "Don't return non zero on an error")
createCmd.PersistentFlags().StringVarP(&retryWhileJQ, "retry-while-jq", "", "", "A jq expression, if set and returns true we will retry the get command (see manual for examples)")
createCmd.PersistentFlags().Uint16VarP(&retryWhileJQMaxAttempts, "retry-while-jq-max-attempts", "", 1200, "The maximum number of attempts we will retry with jq")
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")
Expand Down Expand Up @@ -343,7 +382,7 @@ func createInternal(ctx context.Context, overrides *httpclient.HttpParameterOver
// Check if error response
if resp.StatusCode >= 400 && resp.StatusCode <= 600 {
json.PrintJson(string(resBody))
return "", fmt.Errorf(resp.Status)
return string(resBody), fmt.Errorf(resp.Status)
}

// 204 is no content, so we will skip it.
Expand Down
10 changes: 9 additions & 1 deletion external/runbooks/pxm-how-to.epcc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ description:
long: "Creates the Getting Started PXM Catalog (https://documentation.elasticpath.com/commerce-cloud/docs/developer/how-to/get-started-pcm.html)."
short: "Create and manipulate the Getting Started PXM Catalog"
actions:
create-pricebooks:
description:
short: "Test"
commands:
- |
{{- range untilStep 0 10000 1}}
epcc create -s pcm-pricebook name "Test 12434{{.}}" description "foo"
{{- end -}}
create-catalog-and-publish:
description:
short: "Create the sample catalog and publish"
Expand All @@ -25,7 +33,7 @@ actions:
epcc create pcm-product-price name=Preferred_Pricing currencies.USD.amount 300000 currencies.USD.includes_tax false currencies.GBP.amount 250000 currencies.GBP.includes_tax false sku BE-Electric-Range-1a1a
epcc create pcm-product-price name=Preferred_Pricing currencies.USD.amount 350000 currencies.USD.includes_tax false currencies.GBP.amount 300000 currencies.GBP.includes_tax false sku BE-Gas-Range-2b2b
- epcc create pcm-catalog name "Ranges Catalog" description "Ranges Catalog" pricebook_id name=Preferred_Pricing hierarchy_ids[0] name=Major_Appliances
- epcc create pcm-catalog-release --save-as-alias pxm-how-to-create-catalog-and-publish-release name=Ranges_Catalog
- epcc create pcm-catalog-release --retry-while-jq '.errors[0].status == "422"' --save-as-alias pxm-how-to-create-catalog-and-publish-release name=Ranges_Catalog
# Wait for Catalog to be Published
- epcc get pcm-catalog-release --retry-while-jq '.data.meta.release_status != "PUBLISHED"' name=Ranges_Catalog pxm-how-to-create-catalog-and-publish-release
create-catalog-rule:
Expand Down

0 comments on commit ad73a7e

Please sign in to comment.