-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathssm.go
76 lines (62 loc) · 1.51 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
package main
import (
"context"
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ssm"
log "github.com/sirupsen/logrus"
)
type SSMClient struct {
client *ssm.Client
}
func NewSSMClient(region string, profile string) (*SSMClient, error) {
var cfg aws.Config
var err error
ctx := context.TODO()
if profile != "" {
cfg, err = config.LoadDefaultConfig(ctx,
config.WithSharedConfigProfile(profile),
)
} else {
cfg, err = config.LoadDefaultConfig(ctx)
}
if err != nil {
return nil, err
}
if region != "" {
cfg.Region = region
}
client := ssm.NewFromConfig(cfg)
return &SSMClient{client}, nil
}
func (c *SSMClient) GetParametersByPath(path string) (map[string]string, error) {
if strings.HasSuffix(path, "/") != true {
path = fmt.Sprintf("%s/", path)
}
var nextToken *string
parameters := make(map[string]string)
for {
params := &ssm.GetParametersByPathInput{
Path: aws.String(path),
Recursive: aws.Bool(true),
WithDecryption: aws.Bool(true),
MaxResults: aws.Int32(10),
NextToken: nextToken,
}
response, err := c.client.GetParametersByPath(context.TODO(), params)
if err != nil {
log.Errorf("Error Getting Parameters from SSM: %s", err)
return nil, err
}
for _, p := range response.Parameters {
parameters[strings.TrimPrefix(*p.Name, path)] = *p.Value
}
if response.NextToken == nil {
break
}
nextToken = response.NextToken
}
return parameters, nil
}