Skip to content

Commit c5d434f

Browse files
committed
Started reorganization to integration aws_ssm with checkenv
1 parent bb0c048 commit c5d434f

7 files changed

Lines changed: 535 additions & 0 deletions

File tree

aws_ssm/aws_ssm.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Based on: https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/gov2/ssm/GetParameter/GetParameterv2.go
3+
*/
4+
package aws_ssm
5+
6+
import (
7+
"context"
8+
"log"
9+
10+
"github.com/aws/aws-sdk-go-v2/config"
11+
"github.com/aws/aws-sdk-go-v2/service/ssm"
12+
)
13+
14+
// SSMGetParametersAPI and SSMDescribeParametersAPI defines the interface
15+
// for the GetParameters and DescribeParameters function.
16+
// We use this interface to test the function using a mocked service
17+
type SSMGetParametersAPI interface {
18+
GetParameters(
19+
ctx context.Context,
20+
params *ssm.GetParametersInput,
21+
optFns ...func(*ssm.Options),
22+
) (*ssm.GetParametersOutput, error)
23+
}
24+
25+
type SSMDescribeParametersAPI interface {
26+
DescribeParameters(
27+
ctx context.Context,
28+
params *ssm.DescribeParametersInput,
29+
optFns ...func(*ssm.Options),
30+
) (*ssm.DescribeParametersOutput, error)
31+
}
32+
33+
type AWSSystemsManagerParameterStore interface {
34+
GetParameters(
35+
ctx context.Context,
36+
params *ssm.GetParametersInput,
37+
optFns ...func(*ssm.Options),
38+
) (*ssm.GetParametersOutput, error)
39+
40+
DescribeParameters(
41+
ctx context.Context,
42+
params *ssm.DescribeParametersInput,
43+
optFns ...func(*ssm.Options),
44+
) (*ssm.DescribeParametersOutput, error)
45+
}
46+
47+
// ExecGetParameters and ExecDescribeParameters retrieves an AWS Systems Manager string parameter
48+
// Inputs:
49+
// c: is the context of the method call, which includes the AWS Region
50+
// api: is the interface that defines the method call
51+
// input: defines the input arguments to the service call
52+
// Output:
53+
// If success, a GetParametersOutput object containing the result of the service call and nil
54+
// Otherwise, nil and an error from the call to GetParameters
55+
func ExecGetParameters(c context.Context, api SSMGetParametersAPI, input *ssm.GetParametersInput) (*ssm.GetParametersOutput, error) {
56+
return api.GetParameters(c, input)
57+
}
58+
59+
func ExecDescribeParameters(c context.Context, api SSMDescribeParametersAPI, input *ssm.DescribeParametersInput) (*ssm.DescribeParametersOutput, error) {
60+
return api.DescribeParameters(c, input)
61+
}
62+
63+
// Load the Shared AWS Configuration (~/.aws/config)
64+
func InitAWSClient(ctx context.Context) *ssm.Client {
65+
cfg, err := config.LoadDefaultConfig(ctx)
66+
if err != nil {
67+
log.Fatalln("Failed loading AWS Configuration", err)
68+
}
69+
client := ssm.NewFromConfig(cfg)
70+
71+
return client
72+
}

aws_ssm/aws_ssm_test.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package aws_ssm
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"io/ioutil"
8+
"testing"
9+
10+
"github.com/aws/aws-sdk-go-v2/aws"
11+
"github.com/aws/aws-sdk-go-v2/service/ssm"
12+
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
13+
)
14+
15+
// Fill fake output data
16+
type SSMGetParametersImpl struct{}
17+
type SSMDescribeParametersImpl struct{}
18+
19+
func (dt SSMGetParametersImpl) GetParameters(
20+
ctx context.Context,
21+
params *ssm.GetParametersInput,
22+
optFns ...func(*ssm.Options),
23+
) (*ssm.GetParametersOutput, error) {
24+
parameters := []types.Parameter{}
25+
for _, d := range globalData {
26+
parameters = append(parameters, types.Parameter{
27+
Name: aws.String(d.Name),
28+
Value: aws.String(d.Value),
29+
})
30+
}
31+
output := &ssm.GetParametersOutput{
32+
Parameters: parameters,
33+
}
34+
35+
return output, nil
36+
}
37+
func (dt SSMDescribeParametersImpl) DescribeParameters(
38+
ctx context.Context,
39+
params *ssm.DescribeParametersInput,
40+
optFns ...func(*ssm.Options),
41+
) (*ssm.DescribeParametersOutput, error) {
42+
43+
// TODO(kompotkot): How to test filters?
44+
parameters := []types.ParameterMetadata{
45+
{Name: aws.String("/test/dev/t1")},
46+
{Name: aws.String("/test/dev/t2")},
47+
}
48+
output := &ssm.DescribeParametersOutput{
49+
Parameters: parameters,
50+
}
51+
52+
return output, nil
53+
}
54+
55+
type DataTags struct {
56+
Product string `json:"Product"`
57+
}
58+
59+
type Data struct {
60+
Name string `json:"Name"`
61+
Value string `json:"Value"`
62+
Tags []DataTags `json:"Tags"`
63+
}
64+
65+
var globalData []Data
66+
67+
var globalParameterKeys []string
68+
69+
func populateData(t *testing.T) error {
70+
content, err := ioutil.ReadFile("../data.json")
71+
if err != nil {
72+
return err
73+
}
74+
75+
contentStr := string(content)
76+
err = json.Unmarshal([]byte(contentStr), &globalData)
77+
if err != nil {
78+
return nil
79+
}
80+
81+
return nil
82+
}
83+
84+
func TestDescribeParameters(t *testing.T) {
85+
err := populateData(t)
86+
if err != nil {
87+
t.Fatal("Failed to populate data")
88+
}
89+
90+
api := &SSMDescribeParametersImpl{}
91+
92+
flags := Flags{FilterTags: []FilterTag{{Name: "Product", Value: "test"}}}
93+
94+
// Test DescribeParameters
95+
parameterKeys := FetchKeysOfParameters(
96+
context.Background(),
97+
*api,
98+
flags,
99+
)
100+
if len(parameterKeys) != 2 {
101+
// TODO(kompotkot): Extract length of parameters from data.json
102+
t.Logf("Length of parameter keys should be 2, but got %d", len(parameterKeys))
103+
t.Fail()
104+
}
105+
106+
for _, p := range parameterKeys {
107+
globalParameterKeys = append(globalParameterKeys, p)
108+
}
109+
}
110+
111+
func TestGetParameters(t *testing.T) {
112+
parameterKeyChunks := GenerateChunks(globalParameterKeys, 10)
113+
114+
api := &SSMGetParametersImpl{}
115+
116+
flags := Flags{Export: false}
117+
118+
parameters := FetchParameters(
119+
context.Background(),
120+
*api,
121+
parameterKeyChunks,
122+
flags,
123+
)
124+
if len(parameters) != 2 {
125+
// TODO(kompotkot): Extract length of parameters from data.json
126+
t.Logf("Length of parameters should be 2, but got %d", len(parameters))
127+
t.Fail()
128+
}
129+
fmt.Println(parameters)
130+
}

aws_ssm/data.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package aws_ssm
2+
3+
// Parameter structure for storing final result from AWS SSM
4+
type Parameter struct {
5+
Name string
6+
Value string
7+
Export string
8+
}
9+
10+
// Tags for filter defined by user
11+
type FilterTag struct {
12+
Name string
13+
Value string
14+
}
15+
16+
// Contains command-line flags defined by user
17+
type Flags struct {
18+
Export bool
19+
MaxResults int
20+
Outfile string
21+
FilterTags []FilterTag
22+
Update bool
23+
}

0 commit comments

Comments
 (0)