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’ll 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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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(
MapLenBetween(0, 10),
MapKeyNotMatch(regexp.MustCompile(`^AWS.*$`), "input_path must not start with \"AWS\""),
gdavison marked this conversation as resolved.
Show resolved Hide resolved
),
},
"input_template": {
Type: schema.TypeString,
Expand Down
19 changes: 18 additions & 1 deletion aws/resource_aws_cloudwatch_event_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1107,14 +1107,31 @@ resource "aws_cloudwatch_event_target" "test" {
input_transformer {
input_paths = {
time = "$.time"
severity : "$.detail.severity",
Finding_ID : "$.detail.id",
instanceId : "$.detail.resource.instanceDetails.instanceId",
port : "$.detail.service.action.networkConnectionAction.localPortDetails.port",
eventFirstSeen : "$.detail.service.eventFirstSeen",
eventLastSeen : "$.detail.service.eventLastSeen",
count : "$.detail.service.count",
Finding_Type : "$.detail.type",
region : "$.region",
}
philnichol marked this conversation as resolved.
Show resolved Hide resolved

input_template = <<EOF
{
"detail-type": "Scheduled Event",
"source": "aws.events",
"time": <time>,
"region": "eu-west-1",
"severity": <severity>,
"Finding_ID": <Finding_ID>,
"instanceId": <instanceId>,
"port": <port>,
"eventFirstSeen": <eventFirstSeen>,
"eventLastSeen": <eventLastSeen>,
"count": <count>,
"Finding_Type": <Finding_Type>,
"region": <region>,
"detail": {}
}
EOF
Expand Down
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 MapLenBetween(min, 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) < min || len(m) > max {
errors = append(errors, fmt.Errorf("expected length of %s to be in the range (%d - %d), got %d", k, min, max, len(m)))
}

return warnings, errors
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, I would just check the maximum size, since there isn't really a lower limit. You could call it MapMaxLen() or match it with the MaxItems field for TypeList and TypeSet and call it MapMaxItems().

This would be a great addition to the Terraform Plugin SDK


func MapKeyNotMatch(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
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A picky naming thing: many of the validation functions read a bit more smoothly, like StringDoesNotMatch(), StringDoesNotContainAny(), or StringIsNotWhiteSpace(). Maybe a name like MapKeyDoesNotMatch().

This would also be a great addition to the Terraform Plugin SDK

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, have renamed to MapKeysDoNotMatch.
Will look into raising a proposal in the SDK plugin

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