-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathtemplate.go
145 lines (129 loc) · 3.72 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package hssm
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"text/template"
"github.com/Masterminds/sprig"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
)
// WriteFileD dumps a given content on the file with path `targetDir/fileName`.
func WriteFileD(fileName string, targetDir string, content string) error {
targetFilePath := targetDir + "/" + fileName
_ = os.Mkdir(targetDir, os.ModePerm)
return WriteFile(targetFilePath, content)
}
// WriteFile dumps a given content on the file with path `targetFilePath`.
func WriteFile(targetFilePath string, content string) error {
return ioutil.WriteFile(targetFilePath, []byte(content), 0777)
}
// ExecuteTemplate loads a template file, executes is against a given function map and writes the output
func ExecuteTemplate(sourceFilePath string, funcMap template.FuncMap, verbose bool) (string, error) {
fileContent, err := ioutil.ReadFile(sourceFilePath)
if err != nil {
return "", err
}
t := template.New("ssmtpl").Funcs(funcMap)
if _, err := t.Parse(string(fileContent)); err != nil {
return "", err
}
var buf bytes.Buffer
vals := map[string]interface{}{}
if err := t.Execute(&buf, vals); err != nil {
return "", err
}
if verbose {
fmt.Println(string(buf.Bytes()))
}
return buf.String(), nil
}
// GetFuncMap builds the relevant function map to helm_ssm
func GetFuncMap(profile string, prefix string, clean bool, tagCleaned string) template.FuncMap {
cleanFunc := func(...interface{}) (string, error) {
return tagCleaned, nil
}
// Clone the func map because we are adding context-specific functions.
var funcMap template.FuncMap = map[string]interface{}{}
for k, v := range sprig.GenericFuncMap() {
if clean {
funcMap[k] = cleanFunc
} else {
funcMap[k] = v
}
}
awsSession := newAWSSession(profile)
if clean {
funcMap["ssm"] = cleanFunc
} else {
funcMap["ssm"] = func(ssmPath string, options ...string) (string, error) {
var hasPrefix = false
for _, s := range options {
if strings.HasPrefix(s, "prefix") {
hasPrefix = true
}
}
if !hasPrefix {
options = append(options, fmt.Sprintf("prefix=%s", prefix))
}
optStr, err := resolveSSMParameter(awsSession, ssmPath, options)
str := ""
if optStr != nil {
str = *optStr
}
return str, err
}
}
return funcMap
}
func resolveSSMParameter(session *session.Session, ssmPath string, options []string) (*string, error) {
opts, err := handleOptions(options)
if err != nil {
return nil, err
}
var defaultValue *string
if optDefaultValue, exists := opts["default"]; exists {
defaultValue = &optDefaultValue
}
var svc ssmiface.SSMAPI
if region, exists := opts["region"]; exists {
svc = ssm.New(session, aws.NewConfig().WithRegion(region))
} else {
svc = ssm.New(session)
}
return GetSSMParameter(svc, opts["prefix"]+ssmPath, defaultValue, true)
}
func handleOptions(options []string) (map[string]string, error) {
validOptions := []string{
"required",
"prefix",
"region",
}
opts := map[string]string{}
for _, o := range options {
split := strings.Split(o, "=")
if len(split) != 2 {
return nil, fmt.Errorf("Invalid option: %s. Valid options: %s", o, validOptions)
}
opts[split[0]] = split[1]
}
if _, exists := opts["required"]; !exists {
opts["required"] = "true"
}
if _, exists := opts["prefix"]; !exists {
opts["prefix"] = ""
}
return opts, nil
}
func newAWSSession(profile string) *session.Session {
// Specify profile for config and region for requests
session := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Profile: profile,
}))
return session
}