github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/builtin/providers/aws/resource_aws_cloudfront_origin_access_identity_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "regexp" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/service/cloudfront" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSCloudFrontOriginAccessIdentity_basic(t *testing.T) { 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckCloudFrontOriginAccessIdentityDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccAWSCloudFrontOriginAccessIdentityConfig, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckCloudFrontOriginAccessIdentityExistence("aws_cloudfront_origin_access_identity.origin_access_identity"), 24 resource.TestCheckResourceAttr("aws_cloudfront_origin_access_identity.origin_access_identity", "comment", "some comment"), 25 resource.TestMatchResourceAttr("aws_cloudfront_origin_access_identity.origin_access_identity", 26 "caller_reference", 27 regexp.MustCompile("^20[0-9]{2}.*")), 28 resource.TestMatchResourceAttr("aws_cloudfront_origin_access_identity.origin_access_identity", 29 "s3_canonical_user_id", 30 regexp.MustCompile("^[a-z0-9]+")), 31 resource.TestMatchResourceAttr("aws_cloudfront_origin_access_identity.origin_access_identity", 32 "cloudfront_access_identity_path", 33 regexp.MustCompile("^origin-access-identity/cloudfront/[A-Z0-9]+")), 34 resource.TestMatchResourceAttr("aws_cloudfront_origin_access_identity.origin_access_identity", 35 "iam_arn", 36 regexp.MustCompile("^arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity [A-Z0-9]+")), 37 ), 38 }, 39 }, 40 }) 41 } 42 43 func TestAccAWSCloudFrontOriginAccessIdentity_noComment(t *testing.T) { 44 resource.Test(t, resource.TestCase{ 45 PreCheck: func() { testAccPreCheck(t) }, 46 Providers: testAccProviders, 47 CheckDestroy: testAccCheckCloudFrontOriginAccessIdentityDestroy, 48 Steps: []resource.TestStep{ 49 resource.TestStep{ 50 Config: testAccAWSCloudFrontOriginAccessIdentityNoCommentConfig, 51 Check: resource.ComposeTestCheckFunc( 52 testAccCheckCloudFrontOriginAccessIdentityExistence("aws_cloudfront_origin_access_identity.origin_access_identity"), 53 resource.TestMatchResourceAttr("aws_cloudfront_origin_access_identity.origin_access_identity", 54 "caller_reference", 55 regexp.MustCompile("^20[0-9]{2}.*")), 56 resource.TestMatchResourceAttr("aws_cloudfront_origin_access_identity.origin_access_identity", 57 "s3_canonical_user_id", 58 regexp.MustCompile("^[a-z0-9]+")), 59 resource.TestMatchResourceAttr("aws_cloudfront_origin_access_identity.origin_access_identity", 60 "cloudfront_access_identity_path", 61 regexp.MustCompile("^origin-access-identity/cloudfront/[A-Z0-9]+")), 62 resource.TestMatchResourceAttr("aws_cloudfront_origin_access_identity.origin_access_identity", 63 "iam_arn", 64 regexp.MustCompile("^arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity [A-Z0-9]+")), 65 ), 66 }, 67 }, 68 }) 69 } 70 71 func testAccCheckCloudFrontOriginAccessIdentityDestroy(s *terraform.State) error { 72 conn := testAccProvider.Meta().(*AWSClient).cloudfrontconn 73 74 for _, rs := range s.RootModule().Resources { 75 if rs.Type != "aws_cloudfront_origin_access_identity" { 76 continue 77 } 78 79 params := &cloudfront.GetCloudFrontOriginAccessIdentityInput{ 80 Id: aws.String(rs.Primary.ID), 81 } 82 83 _, err := conn.GetCloudFrontOriginAccessIdentity(params) 84 if err == nil { 85 return fmt.Errorf("CloudFront origin access identity was not deleted") 86 } 87 } 88 89 return nil 90 } 91 92 func testAccCheckCloudFrontOriginAccessIdentityExistence(r string) resource.TestCheckFunc { 93 return func(s *terraform.State) error { 94 rs, ok := s.RootModule().Resources[r] 95 if !ok { 96 return fmt.Errorf("Not found: %s", r) 97 } 98 if rs.Primary.ID == "" { 99 return fmt.Errorf("No Id is set") 100 } 101 102 conn := testAccProvider.Meta().(*AWSClient).cloudfrontconn 103 104 params := &cloudfront.GetCloudFrontOriginAccessIdentityInput{ 105 Id: aws.String(rs.Primary.ID), 106 } 107 108 _, err := conn.GetCloudFrontOriginAccessIdentity(params) 109 if err != nil { 110 return fmt.Errorf("Error retrieving CloudFront distribution: %s", err) 111 } 112 return nil 113 } 114 } 115 116 const testAccAWSCloudFrontOriginAccessIdentityConfig = ` 117 resource "aws_cloudfront_origin_access_identity" "origin_access_identity" { 118 comment = "some comment" 119 } 120 ` 121 122 const testAccAWSCloudFrontOriginAccessIdentityNoCommentConfig = ` 123 resource "aws_cloudfront_origin_access_identity" "origin_access_identity" { 124 } 125 `