Skip to content

Commit

Permalink
feat: add ability to clear orphans (#6)
Browse files Browse the repository at this point in the history
* add prefix to config

* get orphans

* feat: remove orphans from ssm
  • Loading branch information
adikari authored Nov 21, 2022
1 parent da56ac6 commit d7878e9
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 17 deletions.
45 changes: 41 additions & 4 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ func deploy(cmd *cobra.Command, args []string) error {
return errors.New("value for prompt must be \"all\" or \"missing\"")
}

if removeOrphans {
log.Panic("remove orphans flag is not implemented")
}

if err != nil {
return errors.Wrap(err, "failed to load config")
}
Expand Down Expand Up @@ -144,11 +140,52 @@ func deploy(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "failed to write params")
}

if removeOrphans {
orphans, err := doRemoveOrphans(st, config.Prefix, config.All)
if err != nil {
log.Print("failed to remove orphans")
}

fmt.Printf("%d orphans removed.\n", len(orphans))
}

fmt.Printf("%d new configs deployed. service = %s, stage = %s\n", len(configsToDeploy), config.Service, stage)

return nil
}

func doRemoveOrphans(st store.Store, prefix string, all []store.ConfigInput) ([]store.ConfigInput, error) {
var orphans []store.ConfigInput
params, err := st.GetByPath(prefix)

if err != nil {
return nil, err
}

for _, param := range params {
exists := false

for _, config := range all {
if config.Name == *param.Name {
exists = true
break
}
}

if !exists {
orphans = append(orphans, store.ConfigInput{Name: *param.Name})
}
}

err = st.DeleteMany(orphans)

if err != nil {
return nil, err
}

return orphans, nil
}

func promptConfig(config store.ConfigInput) store.ConfigInput {
validate := func(input string) error {
if len(input) < 1 {
Expand Down
16 changes: 11 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type rawConfig struct {
type Config struct {
Provider string
Service string
Prefix string
All []store.ConfigInput
Configs []store.ConfigInput
Secrets []store.ConfigInput
Expand Down Expand Up @@ -58,6 +59,7 @@ func Load(param LoadConfigInput) (*Config, error) {
c := Config{}
c.Service = rc.Service
c.Provider = rc.Provider
c.Prefix = getPrefix(param.Stage, c.Service)

if c.Provider == "" {
c.Provider = store.SsmProvider
Expand All @@ -78,7 +80,7 @@ func Load(param LoadConfigInput) (*Config, error) {

for key, value := range rc.Config["defaults"] {
c.Configs = append(c.Configs, store.ConfigInput{
Name: formatPath(param.Stage, c.Service, key),
Name: formatPath(c.Prefix, key),
Value: value,
Secret: false,
})
Expand All @@ -94,7 +96,7 @@ func Load(param LoadConfigInput) (*Config, error) {

for key, value := range rc.Config[param.Stage] {
c.Configs = append(c.Configs, store.ConfigInput{
Name: formatPath(param.Stage, c.Service, key),
Name: formatPath(c.Prefix, key),
Value: value,
Secret: false,
})
Expand All @@ -104,7 +106,7 @@ func Load(param LoadConfigInput) (*Config, error) {

for key, value := range rc.Secret["defaults"] {
c.Secrets = append(c.Secrets, store.ConfigInput{
Name: formatPath(param.Stage, c.Service, key),
Name: formatPath(c.Prefix, key),
Description: value,
Secret: true,
})
Expand All @@ -127,8 +129,12 @@ func formatSharedPath(stage string, key string) string {
return fmt.Sprintf("/%s/shared/%s", stage, key)
}

func formatPath(stage string, service string, key string) string {
return fmt.Sprintf("/%s/%s/%s", stage, service, key)
func formatPath(prefix string, key string) string {
return fmt.Sprintf("%s%s", prefix, key)
}

func getPrefix(stage string, service string) string {
return fmt.Sprintf("/%s/%s/", stage, service)
}

func validateConfig(rc rawConfig) error {
Expand Down
4 changes: 0 additions & 4 deletions example/safebox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ config:
DB_NAME: "database name updated"
API_ENDPOINT: "{{.internalDomainName}}"
NEW: "endpoint-{{.stage}}"
NEW2: "endpoint updated"
NEW3: "endpoint updated"
NEW4: "endpoint updated"
NEW5: "endpoint updated"
NEW6: "endpoint updated"
NEW7: "endpoint updated"

prod:
DB_NAME: "production db name"
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ require (
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/exp v0.0.0-20221114191408-850992195362 // indirect
golang.org/x/sys v0.1.0 // indirect
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJ
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
golang.org/x/exp v0.0.0-20221114191408-850992195362 h1:NoHlPRbyl1VFI6FjwHtPQCN7wAMXI6cKcqrmXhOOfBQ=
golang.org/x/exp v0.0.0-20221114191408-850992195362/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
53 changes: 50 additions & 3 deletions store/ssmstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,29 @@ func (s *SSMStore) Delete(config ConfigInput) error {
return nil
}

func (s *SSMStore) DeleteMany(configs []ConfigInput) error {
if len(configs) <= 0 {
return nil
}

for _, config := range configs {
err := s.Delete(config)

if err != nil {
return err
}
}

return nil
}

func (s *SSMStore) GetMany(configs []ConfigInput) ([]Config, error) {
if len(configs) <= 0 {
return []Config{}, nil
}

get := func(c []ConfigInput) ([]Config, error) {
var res []Config
var result []Config

getParametersInput := &ssm.GetParametersInput{
Names: getNames(c),
Expand All @@ -114,10 +130,10 @@ func (s *SSMStore) GetMany(configs []ConfigInput) ([]Config, error) {
}

for _, param := range resp.Parameters {
res = append(res, parameterToConfig(param))
result = append(result, parameterToConfig(param))
}

return res, nil
return result, nil
}

var params []Config
Expand All @@ -144,6 +160,37 @@ func (s *SSMStore) Get(config ConfigInput) (Config, error) {
return configs[0], nil
}

func (s *SSMStore) GetByPath(path string) ([]Config, error) {
var result []Config

input := &ssm.GetParametersByPathInput{
Path: aws.String(path),
WithDecryption: aws.Bool(true),
}

var recursiveGet func()
recursiveGet = func() {
resp, err := s.svc.GetParametersByPath(input)

if err != nil {
return
}

for _, param := range resp.Parameters {
result = append(result, parameterToConfig(param))
}

if resp.NextToken != nil {
input.NextToken = resp.NextToken
recursiveGet()
}
}

recursiveGet()

return result, nil
}

func basePath(key string) string {
pathParts := strings.Split(key, "/")
if len(pathParts) == 1 {
Expand Down
2 changes: 2 additions & 0 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ type Store interface {
PutMany(input []ConfigInput) error
Get(config ConfigInput) (Config, error)
GetMany(configs []ConfigInput) ([]Config, error)
GetByPath(path string) ([]Config, error)
Delete(config ConfigInput) error
DeleteMany(configs []ConfigInput) error
}

func GetStore(provider string) (Store, error) {
Expand Down

0 comments on commit d7878e9

Please sign in to comment.