This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
parameters.go
107 lines (94 loc) · 3.09 KB
/
parameters.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
package ssm
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
log "github.com/sirupsen/logrus"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ssm"
)
// SSM implements the SSM API interface.
type SSM struct {
client ssm.Client
}
// NewSSMClient Returns an SSM client
func NewSSMClient() (*SSM, error) {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil{
return nil, err
}
ssmClient := &SSM{*ssm.NewFromConfig(cfg)}
return ssmClient, nil
}
// Param struct
type Param struct {
// Get Param fields
Name string
WithDecryption bool
ssmClient *SSM
// update/insert param fields
Overwrite bool // set to true when updating param, dont set when inserting param
DataType string // can be text or aws:ec2:image
Type string //e.g SecureString
Value string // value of ssm parameter
}
// Param function creates the struct for querying the ssm parameter store
// name param is used to set the ssm key
// decryption param determine whither Return decrypted or encrypted values for secure string. is ignored for String and StringList
// overwrite param determine whither to overwrite existing value or not. used only with update value.
// dataType param specify data type. valid values are: text | aws:ec2:image
// paramType param specify data type. valid values are: String | StringList | SecureString
// value param specify the actual value for parameter
func (s *SSM) Param(name string, decryption bool, overwrite bool, dataType string, paramType string, value string) *Param {
return &Param{
Name: name,
WithDecryption: decryption,
ssmClient: s,
Overwrite: overwrite,
DataType: dataType,
Type: paramType,
Value: value,
}
}
// SetValue Creates new SSM parameter
func (p *Param) SetValue() (string, error) {
ssmClient := p.ssmClient.client
_, err := ssmClient.PutParameter(context.TODO() ,&ssm.PutParameterInput{
Name: &p.Name,
DataType: &p.DataType,
Type: types.ParameterType(p.Type),
Value: &p.Value,
})
if err != nil {
log.Warnf("error creating ssm parameter [%+v] : %+v\n", p.Name, err)
return "", err
}
return fmt.Sprintf("successfully created ssm param: %+v", p.Name), nil
}
// UpdateValue Updates SSM parameter value
func (p *Param) UpdateValue() (string, error) {
_, err := p.ssmClient.client.PutParameter(context.TODO(), &ssm.PutParameterInput{
Overwrite: p.Overwrite,
Name: &p.Name,
DataType: &p.DataType,
Value: &p.Value,
})
if err != nil {
log.Warnf("error updating ssm parameter [%+v] : %+v\n", p.Name, err)
return "", err
}
return fmt.Sprintf("successfully updated ssm param: %+v", p.Name), nil
}
// GetValue Returns SSM parameter value
func (p *Param) GetValue() (string, error) {
ssmClient := p.ssmClient.client
parameter, err := ssmClient.GetParameter(context.TODO(), &ssm.GetParameterInput{
Name: &p.Name,
WithDecryption: p.WithDecryption,
})
if err != nil {
log.Warnf("error getting ssm parameter [%+v] : %+v\n", p.Name, err)
return "", err
}
return *parameter.Parameter.Value, nil
}