Skip to content

Commit bb0c048

Browse files
committed
Added possibility to specify few tags for filter
1 parent 7c9c928 commit bb0c048

4 files changed

Lines changed: 65 additions & 10 deletions

File tree

scripts/sources/parameters/cmd/aws_ssm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestDescribeParameters(t *testing.T) {
8989

9090
api := &SSMDescribeParametersImpl{}
9191

92-
flags := Flags{ProductTag: "test"}
92+
flags := Flags{FilterTags: []FilterTag{{Name: "Product", Value: "test"}}}
9393

9494
// Test DescribeParameters
9595
parameterKeys := FetchKeysOfParameters(

scripts/sources/parameters/cmd/data.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ type Parameter struct {
77
Export string
88
}
99

10+
// Tags for filter defined by user
11+
type FilterTag struct {
12+
Name string
13+
Value string
14+
}
15+
1016
// Contains command-line flags defined by user
1117
type Flags struct {
1218
Export bool
1319
MaxResults int
1420
Outfile string
15-
ProductTag string
21+
FilterTags []FilterTag
1622
Update bool
1723
}

scripts/sources/parameters/cmd/parameters.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"log"
88
"os"
99
"os/signal"
10+
"strings"
1011

1112
"github.com/aws/aws-sdk-go-v2/service/ssm"
1213
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
@@ -51,12 +52,13 @@ func FetchKeysOfParameters(
5152
var parameters []string
5253

5354
// Set parameter filters
54-
filterKey := "tag:Product"
55-
parameterFilters := []types.ParameterStringFilter{
56-
{
55+
parameterFilters := []types.ParameterStringFilter{}
56+
for _, ft := range flags.FilterTags {
57+
filterKey := fmt.Sprintf("tag:%s", ft.Name)
58+
parameterFilters = append(parameterFilters, types.ParameterStringFilter{
5759
Key: &filterKey,
58-
Values: []string{flags.ProductTag},
59-
},
60+
Values: []string{ft.Value},
61+
})
6062
}
6163
describeInput := &ssm.DescribeParametersInput{
6264
MaxResults: int32(flags.MaxResults),
@@ -107,6 +109,26 @@ func GenerateChunks(flatSlice []string, chunkSize int) [][]string {
107109
return chunks
108110
}
109111

112+
// ParseFilterTags convert string from user input to key value structure
113+
func ParseFilterTags(filterTagsStr string) []FilterTag {
114+
var filterTags []FilterTag
115+
116+
filterTagsSlice := strings.Split(filterTagsStr, ",")
117+
for _, t := range filterTagsSlice {
118+
tagNameValue := strings.Split(t, ":")
119+
if len(tagNameValue) != 2 || len(tagNameValue[0]) == 0 || len(tagNameValue[1]) == 0 {
120+
log.Printf("Unable to parse tag name and value: %s", t)
121+
continue
122+
}
123+
filterTags = append(filterTags, FilterTag{
124+
Name: tagNameValue[0],
125+
Value: tagNameValue[1],
126+
})
127+
}
128+
129+
return filterTags
130+
}
131+
110132
// WriteToFile generate or update existing file and
111133
// flash to it environment variables
112134
func WriteToFile(parameters []Parameter, outfile string, update bool, export bool) {
@@ -150,17 +172,22 @@ func HandleSignals(cancel context.CancelFunc) {
150172

151173
func Extract() {
152174
var flags Flags
175+
var filterTagsStr string
153176
flag.BoolVar(&flags.Export, "export", false, "Add prefix 'export' to each parameter")
154177
flag.IntVar(&flags.MaxResults, "max", 3, "The maximum number of items to return for call to AWS")
155178
flag.StringVar(&flags.Outfile, "outfile", "", "Output file where parameters will be saved")
156-
flag.StringVar(&flags.ProductTag, "product", "", "Product tag")
179+
flag.StringVar(&filterTagsStr, "tags", "", "Product tags for filter separated by comma in format 'tagName1:tagValue1,tagName2:tagValue2'")
157180
flag.BoolVar(&flags.Update, "update", false, "Update existing file if exists (by default the file will be overwritten)")
158181
flag.Parse()
159182

160-
if flags.ProductTag == "" {
161-
log.Fatalln("Please specify the tag of product")
183+
if filterTagsStr == "" {
184+
log.Fatalln("Please specify the tags for filter, at least Product tag")
162185
}
163186

187+
// Convert string of tags for filter to key:value structure
188+
filterTags := ParseFilterTags(filterTagsStr)
189+
flags.FilterTags = filterTags
190+
164191
ctx, cancel := context.WithCancel(context.Background())
165192
go HandleSignals(cancel)
166193

scripts/sources/parameters/cmd/parameters_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,25 @@ func TestGenerateChunks(t *testing.T) {
2626
}
2727
}
2828
}
29+
30+
func TestFilterTags(t *testing.T) {
31+
var emptyFilterTags []FilterTag
32+
var cases = []struct {
33+
filterTagsStr string
34+
expected []FilterTag
35+
}{
36+
{"Product", emptyFilterTags},
37+
{"Product:", emptyFilterTags},
38+
{":test", emptyFilterTags},
39+
{":", emptyFilterTags},
40+
{"Product:test", []FilterTag{{Name: "Product", Value: "test"}}},
41+
{"Product:test,Node:true", []FilterTag{{Name: "Product", Value: "test"}, {Name: "Node", Value: "true"}}},
42+
}
43+
for _, c := range cases {
44+
filterTags := ParseFilterTags(c.filterTagsStr)
45+
if !reflect.DeepEqual(filterTags, c.expected) {
46+
t.Logf("Value should be %s, but got %s", c.expected, filterTags)
47+
t.Fatal()
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)