Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/cloudwatch event target adding validation #15669

Merged
merged 4 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions aws/resource_aws_cloudwatch_event_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ func resourceAwsCloudWatchEventTarget() *schema.Resource {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ValidateFunc: validation.All(
MapMaxItems(10),
MapKeysDoNotMatch(regexp.MustCompile(`^AWS.*$`), "input_path must not start with \"AWS\""),
),
},
"input_template": {
Type: schema.TypeString,
Expand Down
46 changes: 39 additions & 7 deletions aws/resource_aws_cloudwatch_event_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package aws
import (
"fmt"
"log"
"regexp"
"strings"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -359,7 +361,11 @@ func TestAccAWSCloudWatchEventTarget_input_transformer(t *testing.T) {
CheckDestroy: testAccCheckAWSCloudWatchEventTargetDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudWatchEventTargetConfigInputTransformer(rName),
Config: testAccAWSCloudWatchEventTargetConfigInputTransformer(rName, 11),
ExpectError: regexp.MustCompile(`.*expected number of items in.* to be lesser than or equal to.*`),
},
{
Config: testAccAWSCloudWatchEventTargetConfigInputTransformer(rName, 10),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchEventTargetExists("aws_cloudwatch_event_target.test", &target),
),
Expand Down Expand Up @@ -1062,7 +1068,35 @@ resource "aws_sqs_queue" "sqs_queue" {
`, rName)
}

func testAccAWSCloudWatchEventTargetConfigInputTransformer(rName string) string {
func testAccAWSCloudWatchEventTargetConfigInputTransformer(rName string, inputPathCount int) string {
sampleInputPaths := [...]string{
"account",
"count",
"eventFirstSeen",
"eventLastSeen",
"Finding_ID",
"Finding_Type",
"instanceId",
"port",
"region",
"severity",
"time",
}
var inputPaths strings.Builder
var inputTemplates strings.Builder

if len(sampleInputPaths) < inputPathCount {
inputPathCount = len(sampleInputPaths)
}

for i := 0; i < inputPathCount; i++ {
fmt.Fprintf(&inputPaths, `
%s = "$.%s"`, sampleInputPaths[i], sampleInputPaths[i])

fmt.Fprintf(&inputTemplates, `
"%s": <%s>,`, sampleInputPaths[i], sampleInputPaths[i])
}

return fmt.Sprintf(`
resource "aws_iam_role" "iam_for_lambda" {
name = "tf_acc_input_transformer"
Expand Down Expand Up @@ -1106,20 +1140,18 @@ resource "aws_cloudwatch_event_target" "test" {

input_transformer {
input_paths = {
time = "$.time"
%s
}

input_template = <<EOF
{
"detail-type": "Scheduled Event",
"source": "aws.events",
"time": <time>,
"region": "eu-west-1",
"source": "aws.events",%s
"detail": {}
}
EOF

}
}
`, rName)
`, rName, inputPaths.String(), inputTemplates.String())
}
34 changes: 34 additions & 0 deletions aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2466,3 +2466,37 @@ func validateNestedExactlyOneOf(m map[string]interface{}, valid []string) error
}
return nil
}

func MapMaxItems(max int) schema.SchemaValidateFunc {
return func(i interface{}, k string) (warnings []string, errors []error) {
m, ok := i.(map[string]interface{})
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be map", k))
return warnings, errors
}

if len(m) > max {
errors = append(errors, fmt.Errorf("expected number of items in %s to be lesser than or equal to %d, got %d", k, max, len(m)))
}

return warnings, errors
}
}

func MapKeysDoNotMatch(r *regexp.Regexp, message string) schema.SchemaValidateFunc {
return func(i interface{}, k string) (warnings []string, errors []error) {
m, ok := i.(map[string]interface{})
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be map", k))
return warnings, errors
}

for key := range m {
if ok := r.MatchString(key); ok {
errors = append(errors, fmt.Errorf("%s: %s: %s", k, message, key))
}
}

return warnings, errors
}
}
4 changes: 4 additions & 0 deletions website/docs/r/cloudwatch_event_target.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ For more information, see [Task Networking](https://docs.aws.amazon.com/AmazonEC
`input_transformer` support the following:

* `input_paths` - (Optional) Key value pairs specified in the form of JSONPath (for example, time = $.time)
* You can have as many as 10 key-value pairs.
* You must use JSON dot notation, not bracket notation.
* The keys can't start with "AWS".

* `input_template` - (Required) Structure containing the template body.

## Import
Expand Down