-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
New resource: aws cloudwatch query definition #17899
New resource: aws cloudwatch query definition #17899
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Welcome @tlkamp 👋
It looks like this is your first Pull Request submission to the Terraform AWS Provider! If you haven’t already done so please make sure you have checked out our CONTRIBUTING guide and FAQ to make sure your contribution is adhering to best practice and has all the necessary elements in place for a successful approval.
Also take a look at our FAQ which details how we prioritize Pull Requests for inclusion.
Thanks again, and welcome to the community! 😃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, @tlkamp. I've made a number of updates to the PR, based on my comments included in the review.
Acceptance test results in Commercial partition
--- PASS: TestAccAWSCloudWatchQueryDefinition_disappears (44.26s)
--- PASS: TestAccAWSCloudWatchQueryDefinition_basic (56.71s)
--- PASS: TestAccAWSCloudWatchQueryDefinition_Rename (97.73s)
--- PASS: TestAccAWSCloudWatchQueryDefinition_LogGroups (100.81s)
Acceptance test results in GovCloud
--- PASS: TestAccAWSCloudWatchQueryDefinition_disappears (38.51s)
--- PASS: TestAccAWSCloudWatchQueryDefinition_basic (52.08s)
--- PASS: TestAccAWSCloudWatchQueryDefinition_Rename (75.55s)
--- PASS: TestAccAWSCloudWatchQueryDefinition_LogGroups (78.67s)
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Log group names have a particular format, so we can add ValidateFunc: validateLogGroupName
so that validation can be done at plan time
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"query": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To more closely match the API, we should call this query_string
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"log_groups": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To more closely match the API, we should call this log_group_names
if err := d.Set("query_definition_id", aws.StringValue(r.QueryDefinitionId)); err != nil { | ||
return diag.FromErr(err) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking for errors on Set()
is only necessary when setting TypeList
and TypeMap
values
if err := d.Set("query_definition_id", aws.StringValue(r.QueryDefinitionId)); err != nil { | |
return diag.FromErr(err) | |
} | |
d.Set("query_definition_id", aws.StringValue(r.QueryDefinitionId)) |
logGroups := d.Get("log_groups").([]interface{}) | ||
var lgs []*string | ||
|
||
for _, group := range logGroups { | ||
l := group.(string) | ||
lgs = append(lgs, aws.String(l)) | ||
} | ||
|
||
query := d.Get("query").(string) | ||
return &cloudwatchlogs.PutQueryDefinitionInput{ | ||
Name: aws.String(name), | ||
LogGroupNames: lgs, | ||
QueryString: aws.String(query), | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function expandStringList()
can handle the conversion from []interface{}
to []*string
logGroups := d.Get("log_groups").([]interface{}) | |
var lgs []*string | |
for _, group := range logGroups { | |
l := group.(string) | |
lgs = append(lgs, aws.String(l)) | |
} | |
query := d.Get("query").(string) | |
return &cloudwatchlogs.PutQueryDefinitionInput{ | |
Name: aws.String(name), | |
LogGroupNames: lgs, | |
QueryString: aws.String(query), | |
} | |
logGroups := d.Get("log_groups").([]interface{}) | |
query := d.Get("query").(string) | |
return &cloudwatchlogs.PutQueryDefinitionInput{ | |
Name: aws.String(name), | |
LogGroupNames: expandStringList(logGroups), | |
QueryString: aws.String(query), | |
} |
Config: testAccAWSCloudWatchQueryDefinitionConfig(queryName, ident), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSCloudWatchQueryDefinitionExists(resourceName, queryName), | ||
testAccCheckAWSCloudWatchQueryDefinitionDisappears(resourceName), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We now have a generic function testAccCheckResourceDisappears()
that can be used
testAccCheckAWSCloudWatchQueryDefinitionDisappears(resourceName), | |
testAccCheckResourceDisappears(testAccProvider, resourceAwsCloudWatchQueryDefinition(), resourceName), |
|
||
func testAccAWSCloudWatchQueryDefinitionConfig(name, ident string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_cloudwatch_query_definition" "%s" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typically, for simplicity, we name the resource "test"
in all cases
resource "aws_cloudwatch_query_definition" "%s" { | |
resource "aws_cloudwatch_query_definition" "test" { |
CheckDestroy: testAccCheckAWSCloudWatchQueryDefinitionDestroy(resourceName, queryName), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSCloudWatchQueryDefinitionConfig(queryName, ident), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For <resource>_basic
test cases, we typically don't include optional fields. In this case, we would have a testAccAWSCloudWatchQueryDefinitionConfig_Basic
function that doesn't define a value for log_groups
and the test would confirm that no log groups were set.
We would also want another test case that validates that log_groups
is set when there are values in the parameter.
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateVerifyIgnore: []string{"last_modified"}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be removed, since the field last_modified
is not defined on aws_cloudwatch_query_definition
Importer: &schema.ResourceImporter{ | ||
StateContext: resourceAwsCloudWatchQueryDefinitionImport, | ||
}, | ||
SchemaVersion: 1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SchemaVersion
should not be set for new resources
@gdavison thanks for the review! My apologies on missing your comments until recently. |
This has been released in version 3.35.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template for triage. Thanks! |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks! |
Community Note
Closes #13223
Output from acceptance testing: