-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
102 lines (90 loc) · 2.48 KB
/
utils.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
package app
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/sirupsen/logrus"
)
func ddbItem(sess *session.Session, primaryID, secondaryID string) (*map[string]*dynamodb.AttributeValue, error) {
ddbSvc := dynamodb.New(sess)
logrus.WithFields(logrus.Fields{"primaryID": primaryID, "secondaryID": secondaryID}).Debug("DynamoDB GetItem")
result, err := ddbSvc.GetItem(&dynamodb.GetItemInput{
TableName: aws.String("apppack"),
Key: map[string]*dynamodb.AttributeValue{
"primary_id": {
S: aws.String(primaryID),
},
"secondary_id": {
S: aws.String(secondaryID),
},
},
})
if err != nil {
return nil, err
}
if result.Item == nil {
return nil, fmt.Errorf("could not find DDB item %s %s", primaryID, secondaryID)
}
return &result.Item, nil
}
func SsmParameters(sess *session.Session, path string) ([]*ssm.Parameter, error) {
ssmSvc := ssm.New(sess)
var parameters []*ssm.Parameter
input := ssm.GetParametersByPathInput{
Path: &path,
WithDecryption: aws.Bool(true),
}
err := ssmSvc.GetParametersByPathPages(&input, func(resp *ssm.GetParametersByPathOutput, lastPage bool) bool {
logrus.WithFields(logrus.Fields{"path": *input.Path}).Debug("loading parameter by path page")
for _, parameter := range resp.Parameters {
if parameter == nil {
continue
}
parameters = append(parameters, parameter)
}
return !lastPage
})
if err != nil {
return nil, err
}
return parameters, nil
}
func S3FromURL(sess *session.Session, logURL string) (*strings.Builder, error) {
s3Svc := s3.New(sess)
parts := strings.Split(strings.TrimPrefix(logURL, "s3://"), "/")
bucket := parts[0]
object := strings.Join(parts[1:], "/")
logrus.WithFields(logrus.Fields{"bucket": bucket, "key": object}).Debug("fetching object from S3")
out, err := s3Svc.GetObject(&s3.GetObjectInput{
Bucket: &bucket,
Key: &object,
})
if err != nil {
return nil, err
}
buf := new(strings.Builder)
_, err = io.Copy(buf, out.Body)
if err != nil {
return nil, err
}
return buf, nil
}
var JSONIndent = " "
func toJSON(v interface{}) (*bytes.Buffer, error) {
j, err := json.Marshal(v)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer([]byte{})
if err = json.Indent(buf, j, "", JSONIndent); err != nil {
return nil, err
}
return buf, nil
}