-
Notifications
You must be signed in to change notification settings - Fork 10
/
parameter.go
97 lines (82 loc) · 2.38 KB
/
parameter.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
package awsssm
import (
"encoding/json"
"io"
"strings"
"github.com/mitchellh/mapstructure"
)
// Parameter holds a Systems Manager parameter from AWS Parameter Store
type Parameter struct {
Value *string
}
// GetValue return the actual Value of the parameter
func (p *Parameter) GetValue() string {
if p.Value == nil {
return ""
}
return *p.Value
}
// NewParameters creates a Parameters
func NewParameters(basePath string, parameters map[string]*Parameter) *Parameters {
return &Parameters{
basePath: basePath,
parameters: parameters,
}
}
// Parameters holds the output and all AWS Parameter Store that have the same base path
type Parameters struct {
readIndex int64
bytesJSON []byte
basePath string
parameters map[string]*Parameter
}
// Read implements the io.Reader interface for the key/value pair
func (p *Parameters) Read(des []byte) (n int, err error) {
if p.bytesJSON == nil {
p.bytesJSON, err = json.Marshal(p.getKeyValueMap())
if err != nil {
return 0, err
}
}
if p.readIndex >= int64(len(p.bytesJSON)) {
p.readIndex = 0
return 0, io.EOF
}
n = copy(des, p.bytesJSON[p.readIndex:])
p.readIndex += int64(n)
return n, nil
}
// GetValueByName returns the value based on the name
// so the AWS Parameter Store parameter name is base path + name
func (p *Parameters) GetValueByName(name string) string {
parameter, ok := p.parameters[p.basePath+name]
if !ok {
return ""
}
return parameter.GetValue()
}
// GetValueByFullPath returns the value based on the full path
func (p *Parameters) GetValueByFullPath(name string) string {
parameter, ok := p.parameters[name]
if !ok {
return ""
}
return parameter.GetValue()
}
// Decode decodes the parameters into the given struct
// We are using this package to decode the values to the struct https://github.com/mitchellh/mapstructure
// For more details how you can use this check the parameter_test.go file
func (p *Parameters) Decode(output interface{}) error {
return mapstructure.Decode(p.getKeyValueMap(), output)
}
func (p *Parameters) getKeyValueMap() map[string]string {
keyValue := make(map[string]string, len(p.parameters))
for k, v := range p.parameters {
keyValue[strings.Replace(k, p.basePath, "", 1)] = v.GetValue()
}
return keyValue
}
// GetAllValues returns a map with all the keys and values in the store.
func (p *Parameters) GetAllValues() map[string]string {
return p.getKeyValueMap()
}