-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3175 from MattiasGees/feature/rules-packages
Add rules packages datasource for AWS Inspector
- Loading branch information
Showing
5 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" { } | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |