Skip to content

Commit

Permalink
Merge pull request #3175 from MattiasGees/feature/rules-packages
Browse files Browse the repository at this point in the history
Add rules packages datasource for AWS Inspector
  • Loading branch information
bflad authored Feb 12, 2018
2 parents 8dbd36e + f762880 commit fb9ae06
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
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.#"),
},
},
})
}

const testAccCheckAWSInspectorRulesPackagesConfig = `
data "aws_inspector_rules_packages" "test" { }
`
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ func Provider() terraform.ResourceProvider {
"aws_iam_server_certificate": dataSourceAwsIAMServerCertificate(),
"aws_iam_user": dataSourceAwsIAMUser(),
"aws_internet_gateway": dataSourceAwsInternetGateway(),
"aws_inspector_rules_packages": dataSourceAwsInspectorRulesPackages(),
"aws_instance": dataSourceAwsInstance(),
"aws_instances": dataSourceAwsInstances(),
"aws_ip_ranges": dataSourceAwsIPRanges(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@
<li<%= sidebar_current("docs-aws-datasource-iam-user") %>>
<a href="/docs/providers/aws/d/iam_user.html">aws_iam_user</a>
</li>
<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>
<li<%= sidebar_current("docs-aws-datasource-instance") %>>
<a href="/docs/providers/aws/d/instance.html">aws_instance</a>
</li>
Expand Down
46 changes: 46 additions & 0 deletions website/docs/d/inspector_rules_packages.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
layout: "aws"
page_title: "AWS: aws_inspector_rules_packages"
sidebar_current: "docs-aws-datasource-inspector-rules-packages"
description: |-
Provides a list of AWS Inspector Rules packages which can be used by AWS Inspector.
---

# Data Source: aws_inspector_rules_packages

The AWS Inspector Rules Packages data source allows access to the list of AWS
Inspector Rules Packages which can be used by AWS Inspector within the region
configured in the provider.

## Example Usage

```hcl
# Declare the data source
data "aws_inspector_rules_packages" "rules" {}
# e.g. Use in aws_inspector_assessment_template
resource "aws_inspector_resource_group" "group" {
tags {
test = "test"
}
}
resource "aws_inspector_assessment_target" "assessment" {
name = "test"
resource_group_arn = "${aws_inspector_resource_group.group.arn}"
}
resource "aws_inspector_assessment_template" "assessment" {
name = "Test"
target_arn = "${aws_inspector_assessment_target.assessment.arn}"
duration = "60"
rules_package_arns = "${data.aws_inspector_rules_packages.rules.arns}"
}
```

## Attributes Reference

The following attributes are exported:

* `arns` - A list of the AWS Inspector Rules Packages arns available in the AWS region.

0 comments on commit fb9ae06

Please sign in to comment.