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
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
56 changes: 56 additions & 0 deletions aws/data_source_aws_inspector_rules_packages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package aws

import (
"errors"
"fmt"
"log"
"sort"
"time"

"github.com/aws/aws-sdk-go/service/inspector"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsInspectorRulesPackages() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsInspectorRulesPackagesRead,

Schema: map[string]*schema.Schema{
"arns": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceAwsInspectorRulesPackagesRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).inspectorconn

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

var arns []string

input := &inspector.ListRulesPackagesInput{}

err := conn.ListRulesPackagesPages(input, func(page *inspector.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)

return nil
}
24 changes: 24 additions & 0 deletions aws/data_source_aws_inspector_rules_packages_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package aws

import (
"testing"

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

func TestAccAWSInspectorRulesPackages_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
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

},
},
})
}

const testAccCheckAWSInspectorRulesPackagesConfig = `
data "aws_inspector_rules_packages" "rules_packages" { }
`
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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?

"aws_s3_bucket": dataSourceAwsS3Bucket(),
"aws_s3_bucket_object": dataSourceAwsS3BucketObject(),
"aws_sns_topic": dataSourceAwsSnsTopic(),
Expand Down