Skip to content

Commit

Permalink
Refactor get parameters by path call to not have 10 params limit
Browse files Browse the repository at this point in the history
  • Loading branch information
geototti21 committed Aug 14, 2020
1 parent 3e971bd commit 3cd4bdb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
22 changes: 12 additions & 10 deletions parameter_store_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
)

type ssmClient interface {
GetParametersByPath(input *ssm.GetParametersByPathInput) (*ssm.GetParametersByPathOutput, error)
GetParametersByPathPages(input *ssm.GetParametersByPathInput, fn func(*ssm.GetParametersByPathOutput, bool) bool) error
GetParameter(input *ssm.GetParameterInput) (*ssm.GetParameterOutput, error)
}

Expand All @@ -34,20 +34,22 @@ func (ps *ParameterStore) GetAllParametersByPath(path string, decrypt bool) (*Pa
var input = &ssm.GetParametersByPathInput{}
input.SetWithDecryption(decrypt)
input.SetPath(path)
input.SetMaxResults(10)
return ps.getParameters(input)
}

func (ps *ParameterStore) getParameters(input *ssm.GetParametersByPathInput) (*Parameters, error) {
result, err := ps.ssm.GetParametersByPath(input)
if err != nil {
return nil, err
}
parameters := NewParameters(*input.Path, make(map[string]*Parameter, len(result.Parameters)))
for _, v := range result.Parameters {
if v.Name == nil {
continue
parameters := NewParameters(*input.Path, make(map[string]*Parameter))
if err := ps.ssm.GetParametersByPathPages(input, func(result *ssm.GetParametersByPathOutput, b bool) bool {
for _, v := range result.Parameters {
if v.Name == nil {
continue
}
parameters.parameters[*v.Name] = &Parameter{Value: v.Value}
}
parameters.parameters[*v.Name] = &Parameter{Value: v.Value}
return !b
}); err != nil {
return nil, err
}
return parameters, nil
}
Expand Down
48 changes: 40 additions & 8 deletions parameter_store_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,35 @@ var param2 = new(ssm.Parameter).
SetValue("rds.something.aws.com").
SetARN("arn:aws:ssm:us-east-2:aws-account-id:/my-service/dev/DB_HOST")

var param3 = new(ssm.Parameter).
SetName("/my-service/dev/DB_USERNAME").
SetValue("username").
SetARN("arn:aws:ssm:us-east-2:aws-account-id:/my-service/dev/DB_USERNAME")

var errSSM = errors.New("ssm request error")

type stubGetParametersByPathOutput struct {
Output ssm.GetParametersByPathOutput
MoreParamsLeft bool
}

type stubSSMClient struct {
GetParametersByPathOutput *ssm.GetParametersByPathOutput
GetParametersByPathError error
GetParameterOutput *ssm.GetParameterOutput
GetParameterError error
GetParametersByPathOutput []stubGetParametersByPathOutput
GetParametersByPathError error
GetParameterOutput *ssm.GetParameterOutput
GetParameterError error
}

func (s stubSSMClient) GetParametersByPath(input *ssm.GetParametersByPathInput) (*ssm.GetParametersByPathOutput, error) {
return s.GetParametersByPathOutput, s.GetParametersByPathError
func (s stubSSMClient) GetParametersByPathPages(input *ssm.GetParametersByPathInput, fn func(*ssm.GetParametersByPathOutput, bool) bool) error {
if s.GetParametersByPathError == nil {
for _, output := range s.GetParametersByPathOutput {
done := fn(&output.Output, output.MoreParamsLeft)
if done {
return nil
}
}
}
return s.GetParametersByPathError
}

func (s stubSSMClient) GetParameter(input *ssm.GetParameterInput) (*ssm.GetParameterOutput, error) {
Expand All @@ -46,8 +64,21 @@ func TestClient_GetParametersByPath(t *testing.T) {
{
name: "Success",
ssmClient: &stubSSMClient{
GetParametersByPathOutput: &ssm.GetParametersByPathOutput{
Parameters: getParameters(),
GetParametersByPathOutput: []stubGetParametersByPathOutput{
{
MoreParamsLeft: true,
Output: ssm.GetParametersByPathOutput{
Parameters: getParameters(),
},
},
{
MoreParamsLeft: false,
Output: ssm.GetParametersByPathOutput{
Parameters: []*ssm.Parameter{
param3,
},
},
},
},
},
path: "/my-service/dev/",
Expand All @@ -56,6 +87,7 @@ func TestClient_GetParametersByPath(t *testing.T) {
parameters: map[string]*Parameter{
"/my-service/dev/DB_PASSWORD": {Value: param1.Value},
"/my-service/dev/DB_HOST": {Value: param2.Value},
"/my-service/dev/DB_USERNAME": {Value: param3.Value},
},
},
},
Expand Down

0 comments on commit 3cd4bdb

Please sign in to comment.