forked from stelligent/mu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
param.go
122 lines (91 loc) · 2.45 KB
/
param.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
package aws
import (
"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"
"github.com/stelligent/mu/common"
)
type paramManager struct {
ssmAPI ssmiface.SSMAPI
dryrun bool
}
func newParamManager(sess *session.Session, dryrun bool) (common.ParamManager, error) {
log.Debug("Connecting to SSM service")
ssmAPI := ssm.New(sess)
return ¶mManager{
dryrun: dryrun,
ssmAPI: ssmAPI,
}, nil
}
// SetParam set the value of a parameter
func (paramMgr *paramManager) SetParam(name string, value string, kmsKey string) error {
ssmAPI := paramMgr.ssmAPI
if paramMgr.dryrun {
return nil
}
log.Debug("Setting param '%s' to '%s'", name, value)
input := &ssm.PutParameterInput{
Name: aws.String(name),
Value: aws.String(value),
Type: aws.String(ssm.ParameterTypeSecureString),
KeyId: aws.String(kmsKey),
Overwrite: aws.Bool(true),
}
_, err := ssmAPI.PutParameter(input)
if err != nil {
return err
}
return nil
}
// DeleteParam set the value of a parameter
func (paramMgr *paramManager) DeleteParam(name string) error {
ssmAPI := paramMgr.ssmAPI
if paramMgr.dryrun {
return nil
}
log.Debug("Deleting param '%s'", name)
input := &ssm.DeleteParameterInput{
Name: aws.String(name),
}
_, err := ssmAPI.DeleteParameter(input)
return err
}
// GetParam get the value of a parameter
func (paramMgr *paramManager) GetParam(name string) (string, error) {
ssmAPI := paramMgr.ssmAPI
log.Debug("Getting param '%s'", name)
input := &ssm.GetParametersInput{
Names: []*string{aws.String(name)},
WithDecryption: aws.Bool(true),
}
output, err := ssmAPI.GetParameters(input)
if err != nil {
return "", err
}
if len(output.Parameters) != 1 {
return "", nil
}
return aws.StringValue(output.Parameters[0].Value), nil
}
// ParamVersion checks if the parameter is set in SSM Parameter Store and return version
func (paramMgr *paramManager) ParamVersion(name string) (int64, error) {
ssmAPI := paramMgr.ssmAPI
log.Debug("checking param exists '%s'", name)
input := &ssm.DescribeParametersInput{
Filters: []*ssm.ParametersFilter{
{
Key: aws.String("Name"),
Values: []*string{aws.String(name)},
},
},
}
output, err := ssmAPI.DescribeParameters(input)
if err != nil {
return 0, err
}
if len(output.Parameters) != 1 {
return 0, nil
}
return *output.Parameters[0].Version, nil
}