github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_inspector_assessment_template_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/inspector"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSInspectorTemplate_basic(t *testing.T) {
    16  	rInt := acctest.RandInt()
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckAWSInspectorTemplateDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccAWSInspectorTemplateAssessment(rInt),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckAWSInspectorTemplateExists("aws_inspector_assessment_template.foo"),
    27  				),
    28  			},
    29  			resource.TestStep{
    30  				Config: testAccCheckAWSInspectorTemplatetModified(rInt),
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccCheckAWSInspectorTargetExists("aws_inspector_assessment_template.foo"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func testAccCheckAWSInspectorTemplateDestroy(s *terraform.State) error {
    40  	conn := testAccProvider.Meta().(*AWSClient).inspectorconn
    41  
    42  	for _, rs := range s.RootModule().Resources {
    43  		if rs.Type != "aws_inspector_assessment_template" {
    44  			continue
    45  		}
    46  
    47  		resp, err := conn.DescribeAssessmentTemplates(&inspector.DescribeAssessmentTemplatesInput{
    48  			AssessmentTemplateArns: []*string{
    49  				aws.String(rs.Primary.ID),
    50  			},
    51  		})
    52  
    53  		if err != nil {
    54  			if inspectorerr, ok := err.(awserr.Error); ok && inspectorerr.Code() == "InvalidInputException" {
    55  				return nil
    56  			} else {
    57  				return fmt.Errorf("Error finding Inspector Assessment Template: %s", err)
    58  			}
    59  		}
    60  
    61  		if len(resp.AssessmentTemplates) > 0 {
    62  			return fmt.Errorf("Found Template, expected none: %s", resp)
    63  		}
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  func testAccCheckAWSInspectorTemplateExists(name string) resource.TestCheckFunc {
    70  	return func(s *terraform.State) error {
    71  		_, ok := s.RootModule().Resources[name]
    72  		if !ok {
    73  			return fmt.Errorf("Not found: %s", name)
    74  		}
    75  
    76  		return nil
    77  	}
    78  }
    79  
    80  func testAccAWSInspectorTemplateAssessment(rInt int) string {
    81  	return fmt.Sprintf(`
    82  resource "aws_inspector_resource_group" "foo" {
    83  	tags {
    84  	  Name  = "tf-acc-test-%d"
    85    }
    86  }
    87  
    88  resource "aws_inspector_assessment_target" "foo" {
    89  	name = "tf-acc-test-basic-%d"
    90  	resource_group_arn =  "${aws_inspector_resource_group.foo.arn}"
    91  }
    92  
    93  resource "aws_inspector_assessment_template" "foo" {
    94    name = "tf-acc-test-basic-tpl-%d"
    95    target_arn    = "${aws_inspector_assessment_target.foo.arn}"
    96    duration      = 3600
    97  
    98    rules_package_arns = [
    99  	  "arn:aws:inspector:us-west-2:758058086616:rulespackage/0-9hgA516p",
   100  	  "arn:aws:inspector:us-west-2:758058086616:rulespackage/0-H5hpSawc",
   101  	  "arn:aws:inspector:us-west-2:758058086616:rulespackage/0-JJOtZiqQ",
   102  	  "arn:aws:inspector:us-west-2:758058086616:rulespackage/0-vg5GGHSD",
   103    ]
   104  }`, rInt, rInt, rInt)
   105  }
   106  
   107  func testAccCheckAWSInspectorTemplatetModified(rInt int) string {
   108  	return fmt.Sprintf(`
   109  resource "aws_inspector_resource_group" "foo" {
   110  	tags {
   111  	  Name  = "tf-acc-test-%d"
   112    }
   113  }
   114  
   115  resource "aws_inspector_assessment_target" "foo" {
   116  	name = "tf-acc-test-basic-%d"
   117  	resource_group_arn =  "${aws_inspector_resource_group.foo.arn}"
   118  }
   119  
   120  resource "aws_inspector_assessment_template" "foo" {
   121    name = "tf-acc-test-basic-tpl-%d"
   122    target_arn    = "${aws_inspector_assessment_target.foo.arn}"
   123    duration      = 3600
   124  
   125    rules_package_arns = [
   126  	  "arn:aws:inspector:us-west-2:758058086616:rulespackage/0-9hgA516p",
   127  	  "arn:aws:inspector:us-west-2:758058086616:rulespackage/0-H5hpSawc",
   128  	  "arn:aws:inspector:us-west-2:758058086616:rulespackage/0-JJOtZiqQ",
   129  	  "arn:aws:inspector:us-west-2:758058086616:rulespackage/0-vg5GGHSD",
   130    ]
   131  }`, rInt, rInt, rInt)
   132  }