Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_cloudfront_public_key: Add import support #17044

Merged
merged 2 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions aws/resource_aws_cloudfront_public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
50 changes: 46 additions & 4 deletions aws/resource_aws_cloudfront_public_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand All @@ -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) },
Expand All @@ -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",
},
Comment on lines +83 to +85
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this pull request, but we can support name_prefix import by adjusting the implementation to the naming package: https://github.com/hashicorp/terraform-provider-aws/blob/main/docs/contributing/contribution-checklists.md#adding-resource-name-generation-support 👍

},
},
})
}

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) },
Expand All @@ -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"),
),
},
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/cloudfront_public_key.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
```