Skip to content

Commit 993358b

Browse files
author
Josh Newman
committed
ref(general): upgrade to use aws-sdk v2
1 parent e5de09d commit 993358b

File tree

7 files changed

+83
-66
lines changed

7 files changed

+83
-66
lines changed

cmd/common.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55
"strings"
66
"time"
77

8-
"github.com/aws/aws-sdk-go/service/ssm"
8+
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
99
"github.com/jedib0t/go-pretty/table"
1010
)
1111

12+
// formatDate formats a time.Time instance.
1213
func formatDate(dt *time.Time) string {
1314
if useLocalTime {
1415
return dt.Local().Format("2006-01-02 03:04:05 PM")
@@ -28,26 +29,30 @@ func insertColumn(row table.Row, index int, item interface{}) table.Row {
2829
return row
2930
}
3031

31-
func getStringChunks(items []*string, chunkSize int) (chunks [][]*string) {
32+
// getStringChunks returns chunks for a slice of strings.
33+
func getStringChunks(items []string, chunkSize int) (chunks [][]string) {
3234
for chunkSize < len(items) {
3335
items, chunks = items[chunkSize:], append(chunks, items[0:chunkSize:chunkSize])
3436
}
3537

3638
return append(chunks, items)
3739
}
3840

39-
func sortParams(params []*ssm.Parameter) {
41+
// sortParams sorts parameters by name.
42+
func sortParams(params []types.Parameter) {
4043
sort.Slice(params, func(i, j int) bool {
4144
return strings.ToLower(*params[i].Name) < strings.ToLower(*params[j].Name)
4245
})
4346
}
4447

48+
// sortDiffRows sorts diff rows by key.
4549
func sortDiffRows(rows []*diffRow) {
4650
sort.Slice(rows, func(i, j int) bool {
4751
return strings.ToLower(rows[i].Key) < strings.ToLower(rows[j].Key)
4852
})
4953
}
5054

55+
// stripSlash strips the first slash.
5156
func stripSlash(str string) string {
5257
return strings.TrimRight(strings.TrimLeft(str, "/"), "/")
5358
}

cmd/diff.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"fmt"
66
"strings"
77

8-
"github.com/aws/aws-sdk-go/aws"
9-
"github.com/aws/aws-sdk-go/service/ssm"
8+
"github.com/aws/aws-sdk-go-v2/service/ssm"
9+
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
1010
"github.com/jedib0t/go-pretty/table"
1111
"github.com/jedib0t/go-pretty/text"
1212
"github.com/spf13/cobra"
@@ -44,16 +44,16 @@ func runDiffCmd(cmd *cobra.Command, args []string) {
4444
fmt.Println(text.FgBlue.Sprintf("Getting diff between \"%s\" and \"%s\"...", path1, path2))
4545

4646
options := &getParamsOptions{
47-
Client: ssm.New(session),
47+
Client: ssm.NewFromConfig(awsConfig),
4848
Path: &path1,
49-
Recursive: aws.Bool(true),
50-
Decrypt: &decrypt,
49+
Recursive: true,
50+
Decrypt: decrypt,
5151
}
5252

53-
params1 := getParams(options, []*ssm.Parameter{}, nil)
53+
params1 := getParams(options, []types.Parameter{}, nil)
5454

5555
options.Path = &path2
56-
params2 := getParams(options, []*ssm.Parameter{}, nil)
56+
params2 := getParams(options, []types.Parameter{}, nil)
5757

5858
tw := table.NewWriter()
5959
tw.Style().Format.Header = text.FormatLower

cmd/ls.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package cmd
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"strings"
78

8-
ssm "github.com/aws/aws-sdk-go/service/ssm"
9+
"github.com/aws/aws-sdk-go-v2/service/ssm"
10+
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
911
"github.com/jedib0t/go-pretty/table"
1012
"github.com/jedib0t/go-pretty/text"
1113
"github.com/spf13/cobra"
@@ -36,13 +38,13 @@ func runLsCmd(cmd *cobra.Command, args []string) {
3638
fmt.Println(text.FgBlue.Sprintf("Listing parameters for \"%s\"", path))
3739

3840
options := &getParamsOptions{
39-
Client: ssm.New(session),
41+
Client: ssm.NewFromConfig(awsConfig),
4042
Path: &path,
41-
Recursive: &recursive,
42-
Decrypt: &decrypt,
43+
Recursive: recursive,
44+
Decrypt: decrypt,
4345
}
4446

45-
params := getParams(options, []*ssm.Parameter{}, nil)
47+
params := getParams(options, []types.Parameter{}, nil)
4648

4749
sortParams(params)
4850

@@ -68,7 +70,7 @@ func runLsCmd(cmd *cobra.Command, args []string) {
6870

6971
row := table.Row{
7072
rest,
71-
*param.Type,
73+
param.Type,
7274
formatDate(param.LastModifiedDate),
7375
}
7476

@@ -97,13 +99,13 @@ func runLsCmd(cmd *cobra.Command, args []string) {
9799
}
98100

99101
type getParamsOptions struct {
100-
Client *ssm.SSM
102+
Client *ssm.Client
101103
Path *string
102-
Recursive *bool
103-
Decrypt *bool
104+
Recursive bool
105+
Decrypt bool
104106
}
105107

106-
func getParams(options *getParamsOptions, params []*ssm.Parameter, nextToken *string) []*ssm.Parameter {
108+
func getParams(options *getParamsOptions, params []types.Parameter, nextToken *string) []types.Parameter {
107109
cfg := &ssm.GetParametersByPathInput{
108110
Path: options.Path,
109111
Recursive: options.Recursive,
@@ -114,7 +116,7 @@ func getParams(options *getParamsOptions, params []*ssm.Parameter, nextToken *st
114116
cfg.NextToken = nextToken
115117
}
116118

117-
out, err := options.Client.GetParametersByPath(cfg)
119+
out, err := options.Client.GetParametersByPath(context.TODO(), cfg)
118120
if err != nil {
119121
panic(err)
120122
}

cmd/migrate.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package cmd
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"os"
78
"strings"
89

9-
"github.com/aws/aws-sdk-go/aws"
10-
"github.com/aws/aws-sdk-go/service/ssm"
10+
"github.com/aws/aws-sdk-go-v2/service/ssm"
11+
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
1112
"github.com/jedib0t/go-pretty/text"
1213
"github.com/spf13/cobra"
1314
)
@@ -46,24 +47,24 @@ func runMigrateCmd(cmd *cobra.Command, args []string) {
4647
regionTo = regionFrom
4748
}
4849

49-
clientFrom := ssm.New(session, &aws.Config{
50-
Region: &regionFrom,
50+
clientFrom := ssm.New(ssm.Options{
51+
Region: regionFrom,
5152
})
5253

53-
clientTo := ssm.New(session, &aws.Config{
54-
Region: &regionTo,
54+
clientTo := ssm.New(ssm.Options{
55+
Region: regionTo,
5556
})
5657

5758
fmt.Println(text.FgBlue.Sprintf("Migrating %s \"%s\" ==> %s \"%s\"", regionFrom, pathFrom, regionTo, pathTo))
5859

5960
options := &getParamsOptions{
6061
Client: clientFrom,
6162
Path: &pathFrom,
62-
Recursive: aws.Bool(true),
63-
Decrypt: aws.Bool(true),
63+
Recursive: true,
64+
Decrypt: true,
6465
}
6566

66-
params := getParams(options, []*ssm.Parameter{}, nil)
67+
params := getParams(options, []types.Parameter{}, nil)
6768

6869
fmt.Println(text.FgBlue.Sprintf("Found %d parameters to migrate...", len(params)))
6970

@@ -81,10 +82,10 @@ func runMigrateCmd(cmd *cobra.Command, args []string) {
8182
Name: &name,
8283
Type: param.Type,
8384
Value: param.Value,
84-
Overwrite: &overwrite,
85+
Overwrite: overwrite,
8586
}
8687

87-
if _, err := clientTo.PutParameter(input); err != nil {
88+
if _, err := clientTo.PutParameter(context.TODO(), input); err != nil {
8889
if strings.HasPrefix(err.Error(), "ParameterAlreadyExists") {
8990
fmt.Println(text.FgYellow.Sprintf("%s already exists... To overwrite, add the --overwrite flag.", name))
9091
continue

cmd/put.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package cmd
22

33
import (
44
"bufio"
5+
"context"
56
"errors"
67
"fmt"
78
"os"
89
"strings"
910

10-
ssm "github.com/aws/aws-sdk-go/service/ssm"
11+
"github.com/aws/aws-sdk-go-v2/service/ssm"
12+
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
1113
"github.com/jedib0t/go-pretty/text"
1214
"github.com/spf13/cobra"
1315
)
@@ -37,13 +39,13 @@ var putCmd = &cobra.Command{
3739
},
3840
}
3941

40-
func runPutCmdContext(cmd *cobra.Command, args []string, context string) {
42+
func runPutCmdContext(cmd *cobra.Command, args []string, ctx string) {
4143
overwrite, _ := cmd.Flags().GetBool("overwrite")
4244
valueType, _ := cmd.Flags().GetString("type")
4345

44-
client := ssm.New(session)
46+
client := ssm.NewFromConfig(awsConfig)
4547

46-
ctxMessage := fmt.Sprintf("context = /%s", context)
48+
ctxMessage := fmt.Sprintf("context = /%s", ctx)
4749
kvMessage := fmt.Sprintf("enter key/value pairs to put in the format of \"key value\".")
4850

4951
fmt.Printf("%v\n", text.FgYellow.Sprint(ctxMessage))
@@ -75,13 +77,13 @@ func runPutCmdContext(cmd *cobra.Command, args []string, context string) {
7577
continue
7678
}
7779

78-
path := fmt.Sprintf("/%s/%s", context, param)
80+
path := fmt.Sprintf("/%s/%s", ctx, param)
7981

80-
_, err := client.PutParameter(&ssm.PutParameterInput{
82+
_, err := client.PutParameter(context.TODO(), &ssm.PutParameterInput{
8183
Name: &path,
8284
Value: &value,
83-
Type: &valueType,
84-
Overwrite: &overwrite,
85+
Type: types.ParameterType(valueType),
86+
Overwrite: overwrite,
8587
})
8688

8789
if err != nil {
@@ -100,13 +102,13 @@ func runPutCmd(cmd *cobra.Command, args []string) {
100102
overwrite, _ := cmd.Flags().GetBool("overwrite")
101103
valueType, _ := cmd.Flags().GetString("type")
102104

103-
client := ssm.New(session)
105+
client := ssm.NewFromConfig(awsConfig)
104106

105-
_, err := client.PutParameter(&ssm.PutParameterInput{
107+
_, err := client.PutParameter(context.TODO(), &ssm.PutParameterInput{
106108
Name: &path,
107109
Value: &value,
108-
Type: &valueType,
109-
Overwrite: &overwrite,
110+
Type: types.ParameterType(valueType),
111+
Overwrite: overwrite,
110112
})
111113

112114
if err != nil {

cmd/rm.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package cmd
22

33
import (
44
"bufio"
5+
"context"
56
"errors"
67
"fmt"
78
"os"
89

9-
"github.com/aws/aws-sdk-go/aws"
10-
ssm "github.com/aws/aws-sdk-go/service/ssm"
10+
"github.com/aws/aws-sdk-go-v2/service/ssm"
11+
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
1112
"github.com/jedib0t/go-pretty/text"
1213
"github.com/spf13/cobra"
1314
)
@@ -29,19 +30,19 @@ func runRmCmd(cmd *cobra.Command, args []string) {
2930
path := args[0]
3031
recursive, _ := cmd.Flags().GetBool("recursive")
3132

32-
client := ssm.New(session)
33+
client := ssm.NewFromConfig(awsConfig)
3334

34-
var names []*string
35+
var names []string
3536

3637
if recursive {
3738
opts := &getParamsOptions{
3839
Client: client,
3940
Path: &path,
40-
Recursive: aws.Bool(true),
41-
Decrypt: aws.Bool(false),
41+
Recursive: true,
42+
Decrypt: false,
4243
}
4344

44-
params := getParams(opts, []*ssm.Parameter{}, nil)
45+
params := getParams(opts, []types.Parameter{}, nil)
4546

4647
if len(params) == 0 {
4748
fmt.Println("No parameters to delete at the specified path.")
@@ -53,7 +54,7 @@ func runRmCmd(cmd *cobra.Command, args []string) {
5354
fmt.Println(text.FgYellow.Sprint("The following parameters will be removed..."))
5455

5556
for _, param := range params {
56-
names = append(names, param.Name)
57+
names = append(names, *param.Name)
5758

5859
fmt.Println(*param.Name)
5960
}
@@ -71,7 +72,7 @@ func runRmCmd(cmd *cobra.Command, args []string) {
7172
for _, arg := range args {
7273
// declare in loop for pointer to not be repeated
7374
str := arg
74-
names = append(names, &str)
75+
names = append(names, str)
7576
}
7677
}
7778

@@ -80,7 +81,7 @@ func runRmCmd(cmd *cobra.Command, args []string) {
8081
num := 0
8182

8283
for _, chunk := range chunks {
83-
res, err := client.DeleteParameters(&ssm.DeleteParametersInput{
84+
res, err := client.DeleteParameters(context.TODO(), &ssm.DeleteParametersInput{
8485
Names: chunk,
8586
})
8687

0 commit comments

Comments
 (0)