This repository has been archived by the owner on Sep 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssm.go
138 lines (106 loc) · 2.5 KB
/
ssm.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
package main
import (
"reflect"
"strings"
"text/template"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
)
type SSMParameter struct {
Name string
Value string
Version int64
}
func (p SSMParameter) String() string {
return p.Value
}
func SSMFromParameter(p *ssm.Parameter) SSMParameter {
return SSMParameter{
Name: *p.Name,
Value: *p.Value,
Version: *p.Version,
}
}
var _ssmSvc *ssm.SSM = nil
func getSvc() *ssm.SSM {
if _ssmSvc == nil {
sess := session.Must(session.NewSession())
_ssmSvc = ssm.New(sess)
}
return _ssmSvc
}
func ssmGet(name string, params ParamValue) (*SSMParameter, error) {
var err error
input := ssm.GetParameterInput{Name: &name}
if b, ok := params["decrypt"]; ok {
input.SetWithDecryption(b.Bool())
}
if err = input.Validate(); err != nil {
return nil, err
}
p, err := getSvc().GetParameter(&input)
if err != nil {
return nil, err
}
ret := SSMFromParameter(p.Parameter)
return &ret, nil
}
func ssmGetPath(path string, params ParamValue) ([]SSMParameter, error) {
var err error
var trim bool = false
input := ssm.GetParametersByPathInput{Path: &path}
if b, ok := params["decrypt"]; ok {
input.SetWithDecryption(b.Bool())
}
if b, ok := params["recurse"]; ok {
input.SetRecursive(b.Bool())
}
if i, ok := params["maxresults"]; ok {
input.SetMaxResults(i.Int())
}
if b, ok := params["trim"]; ok {
trim = b.Bool()
}
// TODO: array of string filters
if err = input.Validate(); err != nil {
return nil, err
}
var output []SSMParameter
err = getSvc().GetParametersByPathPages(&input,
func(page *ssm.GetParametersByPathOutput, lastPage bool) bool {
for _, p := range page.Parameters {
ssp := SSMFromParameter(p)
if trim {
ssp.Name = strings.TrimPrefix(*p.Name, path)
}
output = append(output, ssp)
}
return true
})
return output, err
}
func getSSMFuncMap() template.FuncMap {
return template.FuncMap{
"ssmGet": func(path string, args ...string) (*SSMParameter, error) {
tmpl, err := ParamParse(ParamDecl{
"decrypt": reflect.Bool,
}, args)
if err != nil {
return nil, err
}
return ssmGet(path, tmpl)
},
"ssmGetPath": func(path string, args ...string) ([]SSMParameter, error) {
tmpl, err := ParamParse(ParamDecl{
"decrypt": reflect.Bool,
"maxresults": reflect.Int,
"recursive": reflect.Bool,
"trim": reflect.Bool,
}, args)
if err != nil {
return nil, err
}
return ssmGetPath(path, tmpl)
},
}
}