github.com/andrewrynhard/terraform@v0.9.5-0.20170502003928-8d286b83eae4/builtin/providers/aws/resource_aws_emr_security_configuration_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/service/emr"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSEmrSecurityConfiguration_basic(t *testing.T) {
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckEmrSecurityConfigurationDestroy,
    18  		Steps: []resource.TestStep{
    19  			{
    20  				Config: testAccEmrSecurityConfigurationConfig,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckEmrSecurityConfigurationExists("aws_emr_security_configuration.foo"),
    23  				),
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  func testAccCheckEmrSecurityConfigurationDestroy(s *terraform.State) error {
    30  	conn := testAccProvider.Meta().(*AWSClient).emrconn
    31  	for _, rs := range s.RootModule().Resources {
    32  		if rs.Type != "aws_emr_security_configuration" {
    33  			continue
    34  		}
    35  
    36  		// Try to find the Security Configuration
    37  		resp, err := conn.DescribeSecurityConfiguration(&emr.DescribeSecurityConfigurationInput{
    38  			Name: aws.String(rs.Primary.ID),
    39  		})
    40  		if err == nil {
    41  			if resp.Name != nil && *resp.Name == rs.Primary.ID {
    42  				// assume this means the resource still exists
    43  				return fmt.Errorf("Error: EMR Security Configuration still exists: %s", *resp.Name)
    44  			}
    45  			return nil
    46  		}
    47  
    48  		// Verify the error is what we want
    49  		if err != nil {
    50  			if isAWSErr(err, "InvalidRequestException", "does not exist") {
    51  				return nil
    52  			}
    53  			return err
    54  		}
    55  	}
    56  
    57  	return nil
    58  }
    59  
    60  func testAccCheckEmrSecurityConfigurationExists(n string) resource.TestCheckFunc {
    61  	return func(s *terraform.State) error {
    62  		rs, ok := s.RootModule().Resources[n]
    63  		if !ok {
    64  			return fmt.Errorf("Not found: %s", n)
    65  		}
    66  
    67  		if rs.Primary.ID == "" {
    68  			return fmt.Errorf("No EMR Security Configuration ID is set")
    69  		}
    70  
    71  		conn := testAccProvider.Meta().(*AWSClient).emrconn
    72  		resp, err := conn.DescribeSecurityConfiguration(&emr.DescribeSecurityConfigurationInput{
    73  			Name: aws.String(rs.Primary.ID),
    74  		})
    75  		if err != nil {
    76  			return err
    77  		}
    78  
    79  		if resp.Name == nil {
    80  			return fmt.Errorf("EMR Security Configuration had nil name which shouldn't happen")
    81  		}
    82  
    83  		if *resp.Name != rs.Primary.ID {
    84  			return fmt.Errorf("EMR Security Configuration name mismatch, got (%s), expected (%s)", *resp.Name, rs.Primary.ID)
    85  		}
    86  
    87  		return nil
    88  	}
    89  }
    90  
    91  const testAccEmrSecurityConfigurationConfig = `
    92  resource "aws_emr_security_configuration" "foo" {
    93  	configuration = <<EOF
    94  {
    95    "EncryptionConfiguration": {
    96      "AtRestEncryptionConfiguration": {
    97        "S3EncryptionConfiguration": {
    98          "EncryptionMode": "SSE-S3"
    99        },
   100        "LocalDiskEncryptionConfiguration": {
   101          "EncryptionKeyProviderType": "AwsKms",
   102          "AwsKmsKey": "arn:aws:kms:us-west-2:187416307283:alias/tf_emr_test_key"
   103        }
   104      },
   105      "EnableInTransitEncryption": false,
   106      "EnableAtRestEncryption": true
   107    }
   108  }
   109  EOF
   110  }
   111  `