Skip to content

Commit

Permalink
feat: write variables to ssm
Browse files Browse the repository at this point in the history
  • Loading branch information
adikari committed Sep 30, 2022
1 parent a0bbc42 commit dea5ba1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
6 changes: 5 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ func execute(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "failed to instantiate store")
}

store.WriteMany(Config.Configs)
err = store.PutMany(Config.Configs)

if err != nil {
return errors.Wrap(err, "failed to write param")
}

return nil
}
59 changes: 34 additions & 25 deletions store/ssmstore.go
Original file line number Diff line number Diff line change
@@ -1,63 +1,71 @@
package store

import (
"log"
"strconv"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
)

var _ Store = &SSMStore{}

var (
numberOfRetries = 10
throttleDelay = client.DefaultRetryerMinRetryDelay
)

type SSMStore struct {
svc ssmiface.SSMAPI
}

func NewSSMStore() (*SSMStore, error) {
ssmSession := session.Must(session.NewSession())

svc := ssm.New(ssmSession)
retryer := client.DefaultRetryer{
NumMaxRetries: numberOfRetries,
MinThrottleDelay: throttleDelay,
}

svc := ssm.New(ssmSession, &aws.Config{
Retryer: retryer,
})

return &SSMStore{
svc: svc,
}, nil
}

func (s *SSMStore) WriteMany(input []ConfigInput) error {
log.Printf("%v", input)
return nil
}

func (s *SSMStore) Write(input ConfigInput) error {
version := 1
current, err := s.Read(input.Key)
func (s *SSMStore) PutMany(input []ConfigInput) error {
for _, config := range input {
err := s.Put(config)

if err != nil && err != ConfigNotFoundError {
return err
if err != nil {
return err
}
}

if err == nil {
version = current.Metadata.Version + 1
}
return nil
}

func (s *SSMStore) Put(input ConfigInput) error {
configType := "String"

if input.Secret == true {
configType = "SecureString"
}

putParameterInput := &ssm.PutParameterInput{
Name: aws.String(input.Key),
Type: aws.String(configType),
Value: aws.String(input.Value),
Overwrite: aws.Bool(true),
Description: aws.String(strconv.Itoa(version)),
Name: aws.String(input.Key),
Type: aws.String(configType),
Value: aws.String(input.Value),
Overwrite: aws.Bool(true),
}

_, err = s.svc.PutParameter(putParameterInput)
_, err := s.svc.PutParameter(putParameterInput)

if err != nil {
return err
Expand All @@ -67,7 +75,8 @@ func (s *SSMStore) Write(input ConfigInput) error {
}

func (s *SSMStore) Delete(key string) error {
_, err := s.Read(key)
_, err := s.Get(key)

if err != nil {
return err
}
Expand All @@ -84,15 +93,15 @@ func (s *SSMStore) Delete(key string) error {
return nil
}

func (s *SSMStore) ReadMany(keys []string) ([]Config, error) {
func (s *SSMStore) GetMany(keys []string) ([]Config, error) {
return nil, nil
}

func (s *SSMStore) ReadAll() ([]Config, error) {
func (s *SSMStore) GetAll() ([]Config, error) {
return nil, nil
}

func (s *SSMStore) Read(key string) (Config, error) {
func (s *SSMStore) Get(key string) (Config, error) {
getParametersInput := &ssm.GetParametersInput{
Names: []*string{aws.String(key)},
WithDecryption: aws.Bool(true),
Expand Down
10 changes: 5 additions & 5 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ var (
)

type Store interface {
Write(input ConfigInput) error
WriteMany(input []ConfigInput) error
Read(key string) (Config, error)
ReadMany(keys []string) ([]Config, error)
ReadAll() ([]Config, error)
Put(input ConfigInput) error
PutMany(input []ConfigInput) error
Get(key string) (Config, error)
GetMany(keys []string) ([]Config, error)
GetAll() ([]Config, error)
Delete(key string) error
}

0 comments on commit dea5ba1

Please sign in to comment.