Skip to content

Commit

Permalink
Resolves #404 - Add template support for attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-r-west committed Aug 11, 2024
1 parent 03f6784 commit 2eb6572
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 29 deletions.
48 changes: 24 additions & 24 deletions docs/runbook-development.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,30 +170,30 @@ name: hello-world
description:
short: "A hello world runbook"
actions:
sequential-sleeps:
variables:
count:
type: INT
default: 2
description:
short: "The number of sleeps"
commands:
- |2
{{- range untilStep 0 .count 1}}
- sleep 1
{{- end -}}
concurrent-sleeps:
variables:
count:
type: INT
default: 2
description:
short: "The number of sleeps"
commands:
- |
{{- range untilStep 0 .count 1}}
sleep 1
{{- end -}}
sequential-sleeps:
variables:
count:
type: INT
default: 2
description:
short: "The number of sleeps"
commands:
- |2
{{- range untilStep 0 .count 1}}
- sleep 1
{{- end -}}
concurrent-sleeps:
variables:
count:
type: INT
default: 2
description:
short: "The number of sleeps"
commands:
- |
{{- range untilStep 0 .count 1}}
sleep 1
{{- end -}}
```

It's important for all commands to be indended the same amount, and start with a `-` this will cause the entire string to be valid as a Yaml array.
Expand Down
71 changes: 71 additions & 0 deletions external/completion/completion_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package completion

import (
"github.com/elasticpath/epcc-cli/external/resources"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
"testing"
Expand Down Expand Up @@ -72,3 +73,73 @@ func TestHeaderValueWithNonNilValueCompletes(t *testing.T) {
require.Equal(t, compDir, cobra.ShellCompDirectiveNoFileComp)
require.Contains(t, completions, "USD")
}

func TestAttributeValueWithNoTemplating(t *testing.T) {
// Fixture Setup
toComplete := ""
acct := resources.MustGetResourceByName("password-profiles")
request := Request{
Type: CompleteAttributeValue,
Verb: Create,
ToComplete: toComplete,
Attribute: "username_format",
Resource: acct,
}

// Exercise SUT
completions, compDir := Complete(request)

// Verify Results
require.Equal(t, compDir, cobra.ShellCompDirectiveNoFileComp)
require.Contains(t, completions, "any")
require.Contains(t, completions, "email")
require.Equal(t, 2, len(completions))
}

func TestAttributeValueWithTemplating(t *testing.T) {
// Fixture Setup
toComplete := ""
acct := resources.MustGetResourceByName("password-profiles")
request := Request{
Type: CompleteAttributeValue,
Verb: Create,
ToComplete: toComplete,
Attribute: "username_format",
Resource: acct,
AllowTemplates: true,
}

// Exercise SUT
completions, compDir := Complete(request)

// Verify Results
require.Equal(t, compDir, cobra.ShellCompDirectiveNoFileComp)
require.Contains(t, completions, "any")
require.Contains(t, completions, "email")
require.Contains(t, completions, `{{\ randAlphaNum\ |`)
require.Contains(t, completions, `{{\ randAlphaNum\ }}`)
}

func TestAttributeValueWithTemplatingAndPipe(t *testing.T) {
// Fixture Setup
toComplete := "{{ randAlphaNum 3 | "
acct := resources.MustGetResourceByName("password-profiles")
request := Request{
Type: CompleteAttributeValue,
Verb: Create,
ToComplete: toComplete,
Attribute: "username_format",
Resource: acct,
AllowTemplates: true,
}

// Exercise SUT
completions, compDir := Complete(request)

// Verify Results
require.Equal(t, compDir, cobra.ShellCompDirectiveNoFileComp)
require.Contains(t, completions, "any")
require.Contains(t, completions, "email")
require.Contains(t, completions, `{{\ randAlphaNum\ 3\ |\ upper\ |`)
require.Contains(t, completions, `{{\ randAlphaNum\ 3\ |\ lower\ }}`)
}
1 change: 0 additions & 1 deletion external/json/to_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,5 @@ func formatValue(v string) string {
v = strings.ReplaceAll(v, `"`, `\"`)

return fmt.Sprintf("\"%s\"", v)
//return fmt.Sprintf("\"%s\"", v)
}
}
19 changes: 19 additions & 0 deletions external/resources/yaml/resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,25 @@ account-addresses:
country:
type: STRING
autofill: FUNC:CountryAbr
account-cart-associations:
singular-name: account-cart-association
json-api-type: account-cart-relationship
json-api-format: "legacy"
no-wrapping: true
docs: "https://elasticpath.dev/docs/commerce-cloud/carts/account-cart-associations/account-cart-associations-overview"
delete-entity:
docs: "https://elasticpath.dev/docs/commerce-cloud/carts/account-cart-associations/delete-an-association"
url: "/v2/carts/{carts}/relationships/accounts"
create-entity:
docs: "https://elasticpath.dev/docs/commerce-cloud/carts/account-cart-associations/create-an-association"
url: "/v2/carts/{carts}/relationships/accounts"
content-type: application/json
attributes:
data[n].id:
type: RESOURCE_ID:account
data[n].type:
type: ENUM:account
autofill: VALUE:account
application-keys:
singular-name: "application-key"
json-api-type: "application_key"
Expand Down
3 changes: 0 additions & 3 deletions external/runbooks/runbook_rendering.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,10 @@ func RenderTemplates(templateName string, rawCmd string, stringVars map[string]*
return nil, fmt.Errorf("error processing variable %s, value %v is not an integer: %w", key, *val, err)
}
data[key] = parsedVal
data[strings.ReplaceAll(key, "-", "_")] = parsedVal
} else if variableDef.Type == "STRING" {
data[key] = val
data[strings.ReplaceAll(key, "-", "_")] = val
} else if strings.HasPrefix(variableDef.Type, "RESOURCE_ID:") {
data[key] = val
data[strings.ReplaceAll(key, "-", "_")] = val
} else {
return nil, fmt.Errorf("error processing variable %s, unknown type [%s] specified in template", key, variableDef.Type)
}
Expand Down
2 changes: 1 addition & 1 deletion external/runbooks/runbook_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"properties": {
"type": {
"type": "string",
"pattern": "^(STRING|URL|INT|ENUM:[a-z0-9A-Z._,-]+|FLOAT|BOOL|FILE|CURRENCY|SINGULAR_RESOURCE_TYPE|JSON_API_TYPE|RESOURCE_ID:[a-z0-9-]+|ANY)$"
"pattern": "^(STRING|URL|INT|ENUM:[a-z0-9A-Z._,-]+|FLOAT|BOOL|FILE|CURRENCY|SINGULAR_RESOURCE_TYPE|JSON_API_TYPE|RESOURCE_ID:[a-z0-9-]+)$"
},
"default": {
"type": ["integer", "string"],
Expand Down

0 comments on commit 2eb6572

Please sign in to comment.