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

Add rules packages datasource for AWS Inspector #3175

Merged
merged 6 commits into from
Feb 12, 2018

Conversation

MattiasGees
Copy link
Contributor

@MattiasGees MattiasGees commented Jan 29, 2018

This add the rules packages datasource to retrieve all the available AWS inspector rules packages for your region.

This is my first time creating a full data source for Terraform. All feedback is welcome on both code and test coverage.

I am also not sure about the name of the datasource. At this moment it is rules_packages but maybe inspector_rules_packages is a better name?

This add the rules packages datasource to retrieve all the available AWS inspector rules packages for your region
@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Jan 29, 2018
@radeksimko radeksimko added service/inspector Issues and PRs that pertain to the inspector service. new-data-source Introduces a new data source. labels Jan 29, 2018
@bflad
Copy link
Contributor

bflad commented Feb 5, 2018

Since AWS is very segregated between services and the APIs, we would definitely recommend calling the data source something with the service name in it and would require that change on review so its not confused with many of the other services that offer "rules" like EC2 security groups. So in this case dataSourceAwsInspectorRulePackages and aws_inspector_rule_packages would be appropriate.

I'll provide a full review shortly.

Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

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

@MattiasGees thanks for your contribution! Check out the notes below and let me know if you have any questions.

This will be pretty straightforward to enhance in the future if we want to by name, provider, etc. filtering with DescribeRulesPackages on the ARNs.

"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsRulesPackages() *schema.Resource {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please change to dataSourceAwsInspectorRulesPackages() and update the Read function accordingly as well. 👍

log.Printf("[DEBUG] Reading Rules Packages.")
d.SetId(time.Now().UTC().String())

var results int64 = 300
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably use the SDK paginator instead of limiting the results or potentially missing some due to not checking NextToken.

var arns []string

input := &inspector.ListRulesPackagesInput{}

err := conn.ListRulesPackagesPages(input, func(page *ListRulesPackagesOutput, lastPage bool) bool {
  for _, arn := range page.RulesPackageArns {
    arns = append(arns, *arn)
  }
  return !lastPage
})
if err != nil {
  return fmt.Errorf("Error fetching Rules Packages: %s", err)
}

if len(arns) == 0 {
  return errors.New("No rules packages found.")
}

sort.Strings(arns) 
d.Set("arns", arns)

aws/provider.go Outdated
@@ -214,6 +214,7 @@ func Provider() terraform.ResourceProvider {
"aws_region": dataSourceAwsRegion(),
"aws_route_table": dataSourceAwsRouteTable(),
"aws_route53_zone": dataSourceAwsRoute53Zone(),
"aws_rules_packages": dataSourceAwsRulesPackages(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Please update to aws_inspector_rules_packages 👍

return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Can't find Tules Packages resource: %s", n)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: typo

Config: testAccCheckAwsRulesPackagesConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsRulesPackagesMeta("data.aws_rules_packages.aws_rules_packages"),
),
Copy link
Contributor

@bflad bflad Feb 5, 2018

Choose a reason for hiding this comment

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

We can check attributes here with some of the resource helpers instead of more complicated acceptance testing logic in another function: e.g. resource.TestCheckResourceAttr("data.aws_inspector_rules_packages.test", "arns.#", "4"), or if indeterminate resource.TestCheckResourceAttrSet("data.aws_inspector_rules_packages.test", "arns.#")

return fmt.Errorf("Rules Packages resource ID not set.")
}

actual, err := testAccCheckAwsRulesPackagesBuildAvailable(rs.Primary.Attributes)
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need this function or its call, see above.

return err
}

expected := actual
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we don't need to acceptance test sort.Strings().

@bflad bflad added the waiting-response Maintainers are waiting on response from community or contributor. label Feb 6, 2018
This changes the name of the function to aws_inspector_rules_packages.
Also a change to the pagination was made + simplified the tests
@ghost ghost added size/M Managed by automation to categorize the size of a PR. and removed size/L Managed by automation to categorize the size of a PR. labels Feb 12, 2018
@MattiasGees
Copy link
Contributor Author

@bflad I made the requested changes.

@bflad bflad self-requested a review February 12, 2018 11:09
@bflad bflad removed the waiting-response Maintainers are waiting on response from community or contributor. label Feb 12, 2018
@bflad bflad added this to the v1.10.0 milestone Feb 12, 2018
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

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

You're pretty close! 😄 Let us know if you don't have time to address the last items.

We definitely need to address the test failure and add documentation:

  • Add data source documentation in website/docs/d/inspector_rules_packages.html.markdown
  • Add link in data sources sidebar section of website/aws.erb e.g.
                        <li<%= sidebar_current("docs-aws-datasource-inspector-rules-packages") %>>
                          <a href="/docs/providers/aws/d/inspector_rules_packages.html">aws_inspector_rules_packages</a>
                        </li>

Steps: []resource.TestStep{
{
Config: testAccCheckAWSInspectorRulesPackagesConfig,
Check: resource.TestCheckResourceAttrSet("data.aws_inspector_rules_packages.test", "arns.#"),
Copy link
Contributor

Choose a reason for hiding this comment

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

The .test here needs to either be updated to .rules_packages to match the name in testAccCheckAWSInspectorRulesPackagesConfig or the data source to be named test in the configuration

make testacc TEST=./aws TESTARGS='-run=TestAccAWSInspectorRulesPackages_basic'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSInspectorRulesPackages_basic -timeout 120m
=== RUN   TestAccAWSInspectorRulesPackages_basic
--- FAIL: TestAccAWSInspectorRulesPackages_basic (7.84s)
	testing.go:513: Step 0 error: Check failed: Not found: data.aws_inspector_rules_packages.test in [root]
FAIL
exit status 1
FAIL	github.com/terraform-providers/terraform-provider-aws/aws	7.888s
make: *** [testacc] Error 1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I renamed it to test

aws/provider.go Outdated
@@ -214,6 +214,7 @@ func Provider() terraform.ResourceProvider {
"aws_region": dataSourceAwsRegion(),
"aws_route_table": dataSourceAwsRouteTable(),
"aws_route53_zone": dataSourceAwsRoute53Zone(),
"aws_inspector_rules_packages": dataSourceAwsInspectorRulesPackages(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor nitpick: Can you lexicographically sort this please?

@bflad bflad added the waiting-response Maintainers are waiting on response from community or contributor. label Feb 12, 2018
@ghost ghost added size/L Managed by automation to categorize the size of a PR. and removed size/M Managed by automation to categorize the size of a PR. labels Feb 12, 2018
@bflad bflad removed the waiting-response Maintainers are waiting on response from community or contributor. label Feb 12, 2018
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

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

This looks great! Thanks for the contribution! 🚀

make testacc TEST=./aws TESTARGS='-run=TestAccAWSInspectorRulesPackages_basic'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSInspectorRulesPackages_basic -timeout 120m
=== RUN   TestAccAWSInspectorRulesPackages_basic
--- PASS: TestAccAWSInspectorRulesPackages_basic (9.91s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	9.971s

@bflad bflad merged commit fb9ae06 into hashicorp:master Feb 12, 2018
bflad added a commit that referenced this pull request Feb 12, 2018
@MattiasGees MattiasGees deleted the feature/rules-packages branch February 13, 2018 07:31
@bflad
Copy link
Contributor

bflad commented Feb 27, 2018

This has been released in version 1.10.0 of the AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

@ghost
Copy link

ghost commented Apr 7, 2020

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 and limited conversation to collaborators Apr 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
new-data-source Introduces a new data source. service/inspector Issues and PRs that pertain to the inspector service. size/L Managed by automation to categorize the size of a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants