-
Notifications
You must be signed in to change notification settings - Fork 0
/
narcissus.go
103 lines (93 loc) · 2.84 KB
/
narcissus.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
// Package narcissus updates a struct with fields that have been tagged with `ssm:"Parameter"` according to the corresponding value in SSM Parameter Store using reflection.
package narcissus
import (
"fmt"
"reflect"
"strconv"
"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"
)
// Wrapper wraps the SSM Client so that it can be mocked. Also allows for client reuse.
type Wrapper struct {
Client ssmiface.SSMAPI
}
func (wrapper *Wrapper) getSSMParameter(name *string) (*string, error) {
withDecryption := true
input := ssm.GetParameterInput{
Name: name,
WithDecryption: &withDecryption,
}
value, err := wrapper.Client.GetParameter(&input)
if err != nil {
return nil, err
}
return value.Parameter.Value, nil
}
func (wrapper *Wrapper) handleSSMUpdate(field *reflect.Value, fieldType *reflect.StructField, ssmPath *string, ssm *string) error {
parameterName := fmt.Sprintf("%s%s", *ssmPath, *ssm)
updatedValue, err := wrapper.getSSMParameter(¶meterName)
if err != nil {
return err
}
switch field.Kind() {
case reflect.String:
field.SetString(*updatedValue)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
ssmInt, err := strconv.ParseInt(*updatedValue, 0, 64)
if err != nil {
return err
}
field.SetInt(ssmInt)
case reflect.Float32, reflect.Float64:
ssmFloat, err := strconv.ParseFloat(*updatedValue, 64)
if err != nil {
return err
}
field.SetFloat(ssmFloat)
case reflect.Bool:
ssmBool, err := strconv.ParseBool(*updatedValue)
if err != nil {
return err
}
field.SetBool(ssmBool)
default:
return fmt.Errorf("Field %s is of a type that cannot be set", fieldType.Name)
}
return nil
}
func getSSMClient() *ssm.SSM {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
client := ssm.New(sess)
return client
}
// UpdateBySSM updates a struct by fields tagged `ssm` using a given wrapped SSM client.
func (wrapper *Wrapper) UpdateBySSM(generic interface{}, ssmPath *string) error {
valueOfGeneric := reflect.ValueOf(generic).Elem()
typeOfGeneric := valueOfGeneric.Type()
for i := 0; i < valueOfGeneric.NumField(); i++ {
field := valueOfGeneric.Field(i)
fieldType := typeOfGeneric.Field(i)
if field.Kind() == reflect.Struct {
err := wrapper.UpdateBySSM(field.Addr().Interface(), ssmPath)
if err != nil {
return err
}
} else if ssm, ok := fieldType.Tag.Lookup("ssm"); ok {
err := wrapper.handleSSMUpdate(&field, &fieldType, ssmPath, &ssm)
if err != nil {
return err
}
}
}
return nil
}
// UpdateBySSM updates a struct by fields tagged `ssm`.
func UpdateBySSM(generic interface{}, ssmPath *string) error {
wrapper := &Wrapper{
Client: getSSMClient(),
}
return wrapper.UpdateBySSM(generic, ssmPath)
}