Skip to content

Commit

Permalink
WIP #404 - Uh wut
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-r-west committed Apr 14, 2024
1 parent df6897f commit 30c0562
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func Execute() {
os.Exit(1)
} else {

os.Exit(0)
//os.Exit(0)
}
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/runbooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ func initRunbookShowCommands() *cobra.Command {

for _, line := range rawCmdLines {
if len(strings.Trim(line, " \n")) > 0 {
//if i <= 10 {
println(line)
//}
}

}
Expand Down
22 changes: 11 additions & 11 deletions docs/runbook-development.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,17 @@ 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 -}}
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:
Expand Down
2 changes: 1 addition & 1 deletion external/json/to_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,6 @@ func formatValue(v string) string {
} else if match, _ := regexp.MatchString("^\\[\\]$", v); match {
return v
} else {
return fmt.Sprintf("\"%s\"", v)
return fmt.Sprintf("\"%s\"", strings.ReplaceAll(v, `"`, `\"`))
}
}
3 changes: 2 additions & 1 deletion external/resources/yaml/resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ account-authentication-settings:
auto_create_account_for_account_members:
type: BOOL
account_member_self_management:
type: BOOL
type: ENUM:disabled,update_only

account-management-authentication-tokens:
singular-name: "account-management-authentication-token"
json-api-type: "account_management_authentication_token"
Expand Down
9 changes: 9 additions & 0 deletions external/runbooks/account-management.epcc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ actions:
# Initialize alias for Authentication Realm
- epcc get account-authentication-settings
- epcc create password-profile related_authentication_realm_for_account_authentication_settings_last_read=entity name "Username and Password Authentication"
enable-self-signup-and-management:
description:
short: "Enable password authentication"
commands:
# Initialize alias for Authentication Realm
- epcc get account-authentication-settings
- |
epcc create password-profile related_authentication_realm_for_account_authentication_settings_last_read=entity name "Username and Password Authentication"
epcc update account-authentication-setting enable_self_signup true auto_create_account_for_account_members true account_member_self_management "update_only"
create-deep-hierarchy:
description:
short: "Create a hierarchy"
Expand Down
47 changes: 46 additions & 1 deletion external/runbooks/runbook_rendering.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"fmt"
"github.com/Masterminds/sprig/v3"
"math"
"math/rand"
"strconv"
"strings"
"text/template"
Expand All @@ -20,7 +22,15 @@ func CreateMapForRunbookArgumentPointers(runbookAction *RunbookAction) map[strin
}

func RenderTemplates(templateName string, rawCmd string, stringVars map[string]*string, variableDefinitions map[string]Variable) ([]string, error) {
tpl, err := template.New(templateName).Funcs(sprig.FuncMap()).Parse(rawCmd)
tpl, err := template.New(templateName).Funcs(sprig.FuncMap()).Funcs(
map[string]any{
"pow": func(a, b int) int { return int(math.Pow(float64(a), float64(b))) },
"pseudoRandAlphaNum": randAlphaNum,
"pseudoRandAlpha": randAlpha,
"pseudoRandNumeric": randNumeric,
"pseudoRandString": randString,
"pseudoRandInt": randInt,
}).Parse(rawCmd)

if err != nil {
// Handle this case better
Expand All @@ -40,10 +50,13 @@ 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 All @@ -63,3 +76,35 @@ func RenderTemplates(templateName string, rawCmd string, stringVars map[string]*
rawCmdLines := strings.Split(renderedTpl.String(), "\n")
return rawCmdLines, nil
}

// randString is the internal function that generates a random string.
// It takes the length of the string and a string of allowed characters as parameters.
func randString(letters string, n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}

// randAlphaNum generates a string consisting of characters in the range 0-9, a-z, and A-Z.
func randAlphaNum(n int) string {
const letters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
return randString(letters, n)
}

// randAlpha generates a string consisting of characters in the range a-z and A-Z.
func randAlpha(n int) string {
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
return randString(letters, n)
}

// randNumeric generates a string consisting of characters in the range 0-9.
func randNumeric(n int) string {
const digits = "0123456789"
return randString(digits, n)
}

func randInt(min, max int) int {
return rand.Intn(max-min) + min
}

0 comments on commit 30c0562

Please sign in to comment.