Skip to content

Commit eab69ea

Browse files
authored
fix filters marshal to include regex strings (#17)
1 parent eabf2f5 commit eab69ea

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

cmd/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package main
1515

1616
import (
17-
"encoding/json"
1817
"fmt"
1918
"log"
2019
"os"
@@ -220,12 +219,12 @@ Full docs can be found at github.com/aws/amazon-` + binName
220219
fmt.Printf("An error occurred while transforming the aggregate filters")
221220
os.Exit(1)
222221
}
223-
filtersJSON, err := json.MarshalIndent(filters, "", " ")
222+
filtersJSON, err := filters.MarshalIndent("", " ")
224223
if err != nil {
225224
fmt.Printf("An error occurred when printing filters due to --verbose being specified: %v", err)
226225
os.Exit(1)
227226
}
228-
transformedFiltersJSON, err := json.MarshalIndent(transformedFilters, "", " ")
227+
transformedFiltersJSON, err := transformedFilters.MarshalIndent("", " ")
229228
if err != nil {
230229
fmt.Printf("An error occurred when printing aggregate filters due to --verbose being specified: %v", err)
231230
os.Exit(1)

pkg/selector/types.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package selector
1515

1616
import (
17+
"encoding/json"
1718
"regexp"
1819

1920
"github.com/aws/aws-sdk-go/service/ec2"
@@ -52,6 +53,28 @@ type filterPair struct {
5253
instanceSpec interface{}
5354
}
5455

56+
func getRegexpString(r *regexp.Regexp) *string {
57+
if r == nil {
58+
return nil
59+
}
60+
rStr := r.String()
61+
return &rStr
62+
}
63+
64+
// MarshalIndent is used to return a pretty-print json representation of a Filters struct
65+
func (f *Filters) MarshalIndent(prefix, indent string) ([]byte, error) {
66+
type Alias Filters
67+
return json.MarshalIndent(&struct {
68+
AllowList *string
69+
DenyList *string
70+
*Alias
71+
}{
72+
AllowList: getRegexpString(f.AllowList),
73+
DenyList: getRegexpString(f.DenyList),
74+
Alias: (*Alias)(f),
75+
}, prefix, indent)
76+
}
77+
5578
// Filters is used to group instance type resource attributes for filtering
5679
type Filters struct {
5780
// AvailabilityZones is the AWS Availability Zones where instances will be provisioned.

pkg/selector/types_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package selector_test
15+
16+
import (
17+
"regexp"
18+
"strings"
19+
"testing"
20+
21+
"github.com/aws/amazon-ec2-instance-selector/pkg/selector"
22+
h "github.com/aws/amazon-ec2-instance-selector/pkg/test"
23+
)
24+
25+
// Tests
26+
27+
func TestMarshalIndent(t *testing.T) {
28+
cpuArch := "x86_64"
29+
allowRegex := "^abc$"
30+
denyRegex := "^zyx$"
31+
32+
filters := selector.Filters{
33+
AllowList: regexp.MustCompile(allowRegex),
34+
DenyList: regexp.MustCompile(denyRegex),
35+
CPUArchitecture: &cpuArch,
36+
}
37+
out, err := filters.MarshalIndent("", " ")
38+
outStr := string(out)
39+
h.Ok(t, err)
40+
h.Assert(t, strings.Contains(outStr, "AllowList") && strings.Contains(outStr, allowRegex), "Does not include AllowList regex string")
41+
h.Assert(t, strings.Contains(outStr, "DenyList") && strings.Contains(outStr, denyRegex), "Does not include DenyList regex string")
42+
43+
}
44+
45+
func TestMarshalIndent_nil(t *testing.T) {
46+
denyRegex := "^zyx$"
47+
48+
filters := selector.Filters{
49+
AllowList: nil,
50+
DenyList: regexp.MustCompile(denyRegex),
51+
}
52+
out, err := filters.MarshalIndent("", " ")
53+
outStr := string(out)
54+
h.Ok(t, err)
55+
h.Assert(t, strings.Contains(outStr, "AllowList") && strings.Contains(outStr, "null"), "Does not include AllowList null entry")
56+
h.Assert(t, strings.Contains(outStr, "DenyList") && strings.Contains(outStr, denyRegex), "Does not include DenyList regex string")
57+
58+
}

0 commit comments

Comments
 (0)