Skip to content

Commit

Permalink
Merge remote-tracking branch 'ryandeivert/ryandeivert-add-flow-log-s3…
Browse files Browse the repository at this point in the history
…-dest' into coveo-master
  • Loading branch information
Julien Duchesne committed Oct 1, 2018
2 parents e337493 + 7d831d7 commit 73820a0
Show file tree
Hide file tree
Showing 7 changed files with 981 additions and 143 deletions.
18 changes: 18 additions & 0 deletions aws/ec2_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package aws

import (
"regexp"
)

var (
ec2ResourceIdRegexp = regexp.MustCompile(`\A([a-z]+)-([a-z0-9]+)\z`)
)

// parseEc2ResourceId parses an EC2 resource ID into prefix and unique part.
func parseEc2ResourceId(s string) (string, string) {
matches := ec2ResourceIdRegexp.FindStringSubmatch(s)
if matches == nil {
return "", ""
}
return matches[1], matches[2]
}
45 changes: 45 additions & 0 deletions aws/ec2_helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package aws

import (
"testing"
)

func TestParseEc2ResourceId(t *testing.T) {
type TestCase struct {
ResourceId string
ExpectedPrefix string
ExpectedUnique string
}
testCases := []TestCase{
{
ResourceId: "",
ExpectedPrefix: "",
ExpectedUnique: "",
},
{
ResourceId: "vpc12345678",
ExpectedPrefix: "",
ExpectedUnique: "",
},
{
ResourceId: "vpc-12345678",
ExpectedPrefix: "vpc",
ExpectedUnique: "12345678",
},
{
ResourceId: "i-12345678abcdef00",
ExpectedPrefix: "i",
ExpectedUnique: "12345678abcdef00",
},
}

for i, testCase := range testCases {
resultPrefix, resultUnique := parseEc2ResourceId(testCase.ResourceId)
if resultPrefix != testCase.ExpectedPrefix || resultUnique != testCase.ExpectedUnique {
t.Errorf(
"test case %d: got (%s, %s), but want (%s, %s)",
i, resultPrefix, resultUnique, testCase.ExpectedPrefix, testCase.ExpectedUnique,
)
}
}
}
Loading

0 comments on commit 73820a0

Please sign in to comment.