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