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

New resource: aws cloudwatch query definition #17899

Merged

Conversation

tlkamp
Copy link
Contributor

@tlkamp tlkamp commented Mar 3, 2021

Community Note

  • Please vote on this pull request by adding a 👍 reaction to the original pull request comment to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for pull request followers and do not help prioritize the request

Closes #13223

Output from acceptance testing:

$ make testacc TESTARGS='-run=TestAccAWSCloudWatchQueryDefinition'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccAWSCloudWatchQueryDefinition -timeout 120m
=== RUN   TestAccAWSCloudWatchQueryDefinition_basic
=== PAUSE TestAccAWSCloudWatchQueryDefinition_basic
=== RUN   TestAccAWSCloudWatchQueryDefinition_disappears
=== PAUSE TestAccAWSCloudWatchQueryDefinition_disappears
=== RUN   TestAccAWSCloudWatchQueryDefinition_update
=== PAUSE TestAccAWSCloudWatchQueryDefinition_update
=== CONT  TestAccAWSCloudWatchQueryDefinition_basic
=== CONT  TestAccAWSCloudWatchQueryDefinition_update
=== CONT  TestAccAWSCloudWatchQueryDefinition_disappears
--- PASS: TestAccAWSCloudWatchQueryDefinition_disappears (22.34s)
--- PASS: TestAccAWSCloudWatchQueryDefinition_basic (27.95s)
--- PASS: TestAccAWSCloudWatchQueryDefinition_update (44.00s)
PASS
ok      github.com/terraform-providers/terraform-provider-aws/aws       46.095s

Traci Kamp added 19 commits February 25, 2021 16:50
@tlkamp tlkamp requested a review from a team as a code owner March 3, 2021 15:39
@ghost ghost added size/XL Managed by automation to categorize the size of a PR. documentation Introduces or discusses updates to documentation. provider Pertains to the provider itself, rather than any interaction with AWS. service/cloudwatchlogs tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure. labels Mar 3, 2021
@github-actions github-actions bot added the needs-triage Waiting for first response or review from a maintainer. label Mar 3, 2021
Copy link

@github-actions github-actions bot left a 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! 😃

@gdavison gdavison self-assigned this Mar 29, 2021
Copy link
Contributor

@gdavison gdavison left a 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,
Copy link
Contributor

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": {
Copy link
Contributor

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": {
Copy link
Contributor

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

Comment on lines +58 to +60
if err := d.Set("query_definition_id", aws.StringValue(r.QueryDefinitionId)); err != nil {
return diag.FromErr(err)
}
Copy link
Contributor

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

Suggested change
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))

Comment on lines +66 to +79
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),
}
Copy link
Contributor

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

Suggested change
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),
Copy link
Contributor

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

Suggested change
testAccCheckAWSCloudWatchQueryDefinitionDisappears(resourceName),
testAccCheckResourceDisappears(testAccProvider, resourceAwsCloudWatchQueryDefinition(), resourceName),


func testAccAWSCloudWatchQueryDefinitionConfig(name, ident string) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_query_definition" "%s" {
Copy link
Contributor

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

Suggested change
resource "aws_cloudwatch_query_definition" "%s" {
resource "aws_cloudwatch_query_definition" "test" {

CheckDestroy: testAccCheckAWSCloudWatchQueryDefinitionDestroy(resourceName, queryName),
Steps: []resource.TestStep{
{
Config: testAccAWSCloudWatchQueryDefinitionConfig(queryName, ident),
Copy link
Contributor

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"},
Copy link
Contributor

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,
Copy link
Contributor

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 added a commit that referenced this pull request Mar 30, 2021
@gdavison gdavison merged commit d330df0 into hashicorp:main Mar 30, 2021
@github-actions github-actions bot added this to the v3.35.0 milestone Mar 30, 2021
@tlkamp
Copy link
Contributor Author

tlkamp commented Mar 31, 2021

@gdavison thanks for the review! My apologies on missing your comments until recently.

@ghost
Copy link

ghost commented Apr 1, 2021

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!

@ghost
Copy link

ghost commented Apr 30, 2021

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!

@ghost ghost locked as resolved and limited conversation to collaborators Apr 30, 2021
@breathingdust breathingdust removed the needs-triage Waiting for first response or review from a maintainer. label Sep 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Introduces or discusses updates to documentation. provider Pertains to the provider itself, rather than any interaction with AWS. size/XL Managed by automation to categorize the size of a PR. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New resource: aws_cloudwatch_query_definition
3 participants