diff --git a/aws/resource_aws_cloudfront_public_key.go b/aws/resource_aws_cloudfront_public_key.go index 1d30170ff6d..671911905f5 100644 --- a/aws/resource_aws_cloudfront_public_key.go +++ b/aws/resource_aws_cloudfront_public_key.go @@ -16,6 +16,9 @@ func resourceAwsCloudFrontPublicKey() *schema.Resource { Read: resourceAwsCloudFrontPublicKeyRead, Update: resourceAwsCloudFrontPublicKeyUpdate, Delete: resourceAwsCloudFrontPublicKeyDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, Schema: map[string]*schema.Schema{ "caller_reference": { diff --git a/aws/resource_aws_cloudfront_public_key_test.go b/aws/resource_aws_cloudfront_public_key_test.go index 2305e0122f0..3b6a7c4304b 100644 --- a/aws/resource_aws_cloudfront_public_key_test.go +++ b/aws/resource_aws_cloudfront_public_key_test.go @@ -14,6 +14,7 @@ import ( func TestAccAWSCloudFrontPublicKey_basic(t *testing.T) { rInt := acctest.RandInt() + resourceName := "aws_cloudfront_public_key.example" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(cloudfront.EndpointsID, t) }, @@ -23,18 +24,45 @@ func TestAccAWSCloudFrontPublicKey_basic(t *testing.T) { { Config: testAccAWSCloudFrontPublicKeyConfig(rInt), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudFrontPublicKeyExistence("aws_cloudfront_public_key.example"), + testAccCheckCloudFrontPublicKeyExistence(resourceName), resource.TestCheckResourceAttr("aws_cloudfront_public_key.example", "comment", "test key"), resource.TestMatchResourceAttr("aws_cloudfront_public_key.example", "caller_reference", regexp.MustCompile(fmt.Sprintf("^%s", resource.UniqueIdPrefix))), resource.TestCheckResourceAttr("aws_cloudfront_public_key.example", "name", fmt.Sprintf("tf-acc-test-%d", rInt)), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAWSCloudFrontPublicKey_disappears(t *testing.T) { + rInt := acctest.RandInt() + resourceName := "aws_cloudfront_public_key.example" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(cloudfront.EndpointsID, t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCloudFrontPublicKeyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCloudFrontPublicKeyConfig(rInt), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudFrontPublicKeyExistence(resourceName), + testAccCheckResourceDisappears(testAccProvider, resourceAwsCloudFrontPublicKey(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, }, }) } func TestAccAWSCloudFrontPublicKey_namePrefix(t *testing.T) { startsWithPrefix := regexp.MustCompile("^tf-acc-test-") + resourceName := "aws_cloudfront_public_key.example" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(cloudfront.EndpointsID, t) }, @@ -44,16 +72,25 @@ func TestAccAWSCloudFrontPublicKey_namePrefix(t *testing.T) { { Config: testAccAWSCloudFrontPublicKeyConfig_namePrefix(), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudFrontPublicKeyExistence("aws_cloudfront_public_key.example"), + testAccCheckCloudFrontPublicKeyExistence(resourceName), resource.TestMatchResourceAttr("aws_cloudfront_public_key.example", "name", startsWithPrefix), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{ + "name_prefix", + }, + }, }, }) } func TestAccAWSCloudFrontPublicKey_update(t *testing.T) { rInt := acctest.RandInt() + resourceName := "aws_cloudfront_public_key.example" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(cloudfront.EndpointsID, t) }, @@ -63,14 +100,19 @@ func TestAccAWSCloudFrontPublicKey_update(t *testing.T) { { Config: testAccAWSCloudFrontPublicKeyConfig(rInt), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudFrontPublicKeyExistence("aws_cloudfront_public_key.example"), + testAccCheckCloudFrontPublicKeyExistence(resourceName), resource.TestCheckResourceAttr("aws_cloudfront_public_key.example", "comment", "test key"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, { Config: testAccAWSCloudFrontPublicKeyConfigUpdate(rInt), Check: resource.ComposeTestCheckFunc( - testAccCheckCloudFrontPublicKeyExistence("aws_cloudfront_public_key.example"), + testAccCheckCloudFrontPublicKeyExistence(resourceName), resource.TestCheckResourceAttr("aws_cloudfront_public_key.example", "comment", "test key1"), ), }, diff --git a/website/docs/r/cloudfront_public_key.html.markdown b/website/docs/r/cloudfront_public_key.html.markdown index bedf8901211..bf46b179bbe 100644 --- a/website/docs/r/cloudfront_public_key.html.markdown +++ b/website/docs/r/cloudfront_public_key.html.markdown @@ -36,3 +36,11 @@ In addition to all arguments above, the following attributes are exported: * `caller_reference` - Internal value used by CloudFront to allow future updates to the public key configuration. * `etag` - The current version of the public key. For example: `E2QWRUHAPOMQZL`. * `id` - The identifier for the public key. For example: `K3D5EWEUDCCXON`. + +## Import + +CloudFront Public Key can be imported using the `id`, e.g. + +``` +$ terraform import aws_cloudfront_public_key.example K3D5EWEUDCCXON +```