Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for a global prefix #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
var valueFiles valueFilesList
var targetDir string
var profile string
var prefix string
var verbose bool
var dryRun bool

Expand Down Expand Up @@ -46,6 +47,7 @@ func main() {
f.BoolVarP(&dryRun, "dry-run", "d", false, "doesn't replace the file content")
f.StringVarP(&targetDir, "target-dir", "o", "", "dir to output content")
f.StringVarP(&profile, "profile", "p", "", "aws profile to fetch the ssm parameters")
f.StringVarP(&prefix, "prefix", "", "", "prefix to apply by default to parameters")

cmd.MarkFlagRequired("values")

Expand All @@ -56,7 +58,7 @@ func main() {
}

func run(cmd *cobra.Command, args []string) error {
funcMap := hssm.GetFuncMap(profile)
funcMap := hssm.GetFuncMap(profile, prefix)
for _, filePath := range valueFiles {
content, err := hssm.ExecuteTemplate(filePath, funcMap, verbose)
if err != nil {
Expand Down
18 changes: 11 additions & 7 deletions internal/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func ExecuteTemplate(sourceFilePath string, funcMap template.FuncMap, verbose bo
}

// GetFuncMap builds the relevant function map to helm_ssm
func GetFuncMap(profile string) template.FuncMap {
func GetFuncMap(profile string, prefix string) template.FuncMap {
// Clone the func map because we are adding context-specific functions.
var funcMap template.FuncMap = map[string]interface{}{}
for k, v := range sprig.GenericFuncMap() {
Expand All @@ -58,7 +58,7 @@ func GetFuncMap(profile string) template.FuncMap {

awsSession := newAWSSession(profile)
funcMap["ssm"] = func(ssmPath string, options ...string) (string, error) {
optStr, err := resolveSSMParameter(awsSession, ssmPath, options)
optStr, err := resolveSSMParameter(awsSession, prefix, ssmPath, options)
str := ""
if optStr != nil {
str = *optStr
Expand All @@ -68,7 +68,7 @@ func GetFuncMap(profile string) template.FuncMap {
return funcMap
}

func resolveSSMParameter(session *session.Session, ssmPath string, options []string) (*string, error) {
func resolveSSMParameter(session *session.Session, defaultPrefix string, ssmPath string, options []string) (*string, error) {
opts, err := handleOptions(options)
if err != nil {
return nil, err
Expand All @@ -86,7 +86,14 @@ func resolveSSMParameter(session *session.Session, ssmPath string, options []str
svc = ssm.New(session)
}

return GetSSMParameter(svc, opts["prefix"]+ssmPath, defaultValue, true)
var ssmFullPath string
if optPrefix, exists := opts["prefix"]; exists {
ssmFullPath = optPrefix + ssmPath
} else {
ssmFullPath = defaultPrefix + ssmPath
}

return GetSSMParameter(svc, ssmFullPath, defaultValue, true)
}

func handleOptions(options []string) (map[string]string, error) {
Expand All @@ -106,9 +113,6 @@ func handleOptions(options []string) (map[string]string, error) {
if _, exists := opts["required"]; !exists {
opts["required"] = "true"
}
if _, exists := opts["prefix"]; !exists {
opts["prefix"] = ""
}
return opts, nil
}

Expand Down
4 changes: 2 additions & 2 deletions internal/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestFailExecuteTemplate(t *testing.T) {

func TestSsmFunctionExistsInFuncMap(t *testing.T) {
t.Logf("\"ssm\" function should exist in function map.")
funcMap := GetFuncMap("")
funcMap := GetFuncMap("", "")
keys := make([]string, len(funcMap))
for k := range funcMap {
keys = append(keys, k)
Expand All @@ -75,7 +75,7 @@ func TestSsmFunctionExistsInFuncMap(t *testing.T) {

func TestSprigFunctionsExistInFuncMap(t *testing.T) {
t.Logf("\"quote\" function (from sprig) should exist in function map.")
funcMap := GetFuncMap("")
funcMap := GetFuncMap("", "")
keys := make([]string, len(funcMap))
for k := range funcMap {
keys = append(keys, k)
Expand Down