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