From 95d13c463b64a3910f334a7e39345557bc2fcc7d Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Sun, 10 Jan 2021 19:59:35 +0900 Subject: [PATCH 1/8] Define aws_cloudfront_key_group resource --- aws/provider.go | 1 + aws/resource_aws_cloudfront_key_group.go | 146 +++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 aws/resource_aws_cloudfront_key_group.go diff --git a/aws/provider.go b/aws/provider.go index 1e171c878576..32f45214b3f5 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -483,6 +483,7 @@ func Provider() *schema.Provider { "aws_cloudformation_stack_set": resourceAwsCloudFormationStackSet(), "aws_cloudformation_stack_set_instance": resourceAwsCloudFormationStackSetInstance(), "aws_cloudfront_distribution": resourceAwsCloudFrontDistribution(), + "aws_cloudfront_key_group": resourceAwsCloudFrontKeyGroup(), "aws_cloudfront_origin_access_identity": resourceAwsCloudFrontOriginAccessIdentity(), "aws_cloudfront_public_key": resourceAwsCloudFrontPublicKey(), "aws_cloudtrail": resourceAwsCloudTrail(), diff --git a/aws/resource_aws_cloudfront_key_group.go b/aws/resource_aws_cloudfront_key_group.go new file mode 100644 index 000000000000..26f7e60a894c --- /dev/null +++ b/aws/resource_aws_cloudfront_key_group.go @@ -0,0 +1,146 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/cloudfront" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func resourceAwsCloudFrontKeyGroup() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsCloudFrontKeyGroupCreate, + Read: resourceAwsCloudFrontKeyGroupRead, + Update: resourceAwsCloudFrontKeyGroupUpdate, + Delete: resourceAwsCloudFrontKeyGroupDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "comment": { + Type: schema.TypeString, + Optional: true, + }, + "etag": { + Type: schema.TypeString, + Computed: true, + }, + "id": { + Type: schema.TypeString, + Computed: true, + }, + "items": { + Type: schema.TypeSet, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + Required: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + }, + }, + } +} + +func resourceAwsCloudFrontKeyGroupCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cloudfrontconn + + input := &cloudfront.CreateKeyGroupInput{ + KeyGroupConfig: expandCloudFrontKeyGroupConfig(d), + } + + log.Println("[DEBUG] Create CloudFront key group:", input) + + output, err := conn.CreateKeyGroup(input) + if err != nil { + return fmt.Errorf("error creating CloudFront key group: %s", err) + } + + d.SetId(aws.StringValue(output.KeyGroup.Id)) + return resourceAwsCloudFrontKeyGroupRead(d, meta) +} + +func resourceAwsCloudFrontKeyGroupRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cloudfrontconn + input := &cloudfront.GetKeyGroupInput{ + Id: aws.String(d.Id()), + } + + output, err := conn.GetKeyGroup(input) + if err != nil { + if isAWSErr(err, cloudfront.ErrCodeNoSuchResource, "") { + log.Printf("[WARN] No key group found: %s, removing from state", d.Id()) + d.SetId("") + return nil + } + return err + } + + if output == nil || output.KeyGroup == nil || output.KeyGroup.KeyGroupConfig == nil { + log.Printf("[WARN] No key group found: %s, removing from state", d.Id()) + d.SetId("") + return nil + } + + keyGroupConfig := output.KeyGroup.KeyGroupConfig + + d.Set("name", keyGroupConfig.Name) + d.Set("comment", keyGroupConfig.Comment) + d.Set("items", flattenStringSet(keyGroupConfig.Items)) + d.Set("etag", output.ETag) + + return nil +} + +func resourceAwsCloudFrontKeyGroupUpdate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cloudfrontconn + + input := &cloudfront.UpdateKeyGroupInput{ + Id: aws.String(d.Id()), + KeyGroupConfig: expandCloudFrontKeyGroupConfig(d), + IfMatch: aws.String(d.Get("etag").(string)), + } + + _, err := conn.UpdateKeyGroup(input) + if err != nil { + return fmt.Errorf("error updating CloudFront key group (%s): %s", d.Id(), err) + } + + return resourceAwsCloudFrontKeyGroupRead(d, meta) +} + +func resourceAwsCloudFrontKeyGroupDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cloudfrontconn + + input := &cloudfront.DeleteKeyGroupInput{ + Id: aws.String(d.Id()), + IfMatch: aws.String(d.Get("etag").(string)), + } + + _, err := conn.DeleteKeyGroup(input) + if err != nil { + if isAWSErr(err, cloudfront.ErrCodeNoSuchResource, "") { + return nil + } + return err + } + + return nil +} + +func expandCloudFrontKeyGroupConfig(d *schema.ResourceData) *cloudfront.KeyGroupConfig { + keyGroupConfig := &cloudfront.KeyGroupConfig{ + Items: expandStringSet(d.Get("items").(*schema.Set)), + Name: aws.String(d.Get("name").(string)), + } + + if v, ok := d.GetOk("comment"); ok { + keyGroupConfig.Comment = aws.String(v.(string)) + } + + return keyGroupConfig +} From d1e7ecb2794213d74a324a853582d43a39f937dd Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Sun, 10 Jan 2021 19:59:52 +0900 Subject: [PATCH 2/8] Add tests for aws_cloudfront_key_group resource --- aws/resource_aws_cloudfront_key_group_test.go | 282 ++++++++++++++++++ 1 file changed, 282 insertions(+) create mode 100644 aws/resource_aws_cloudfront_key_group_test.go diff --git a/aws/resource_aws_cloudfront_key_group_test.go b/aws/resource_aws_cloudfront_key_group_test.go new file mode 100644 index 000000000000..e1d0b95259c7 --- /dev/null +++ b/aws/resource_aws_cloudfront_key_group_test.go @@ -0,0 +1,282 @@ +package aws + +import ( + "fmt" + "log" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/cloudfront" + "github.com/hashicorp/go-multierror" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +func init() { + resource.AddTestSweepers("aws_cloudfront_key_group", &resource.Sweeper{ + Name: "aws_cloudfront_key_group", + F: testSweepCloudFrontKeyGroup, + }) +} + +func testSweepCloudFrontKeyGroup(region string) error { + client, err := sharedClientForRegion(region) + if err != nil { + return fmt.Errorf("Error getting client: %w", err) + } + conn := client.(*AWSClient).cloudfrontconn + var sweeperErrs *multierror.Error + + input := &cloudfront.ListKeyGroupsInput{} + + for { + output, err := conn.ListKeyGroups(input) + if err != nil { + if testSweepSkipSweepError(err) { + log.Printf("[WARN] Skipping CloudFront key group sweep for %s: %s", region, err) + return nil + } + sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error retrieving CloudFront key group: %w", err)) + return sweeperErrs.ErrorOrNil() + } + + if output == nil || output.KeyGroupList == nil || len(output.KeyGroupList.Items) == 0 { + log.Print("[DEBUG] No CloudFront key group to sweep") + return nil + } + + for _, item := range output.KeyGroupList.Items { + strId := aws.StringValue(item.KeyGroup.Id) + log.Printf("[INFO] CloudFront key group %s", strId) + _, err := conn.DeleteKeyGroup(&cloudfront.DeleteKeyGroupInput{ + Id: item.KeyGroup.Id, + }) + if err != nil { + sweeperErr := fmt.Errorf("error deleting CloudFront key group %s: %w", strId, err) + log.Printf("[ERROR] %s", sweeperErr) + sweeperErrs = multierror.Append(sweeperErrs, sweeperErr) + continue + } + } + + if output.KeyGroupList.NextMarker == nil { + break + } + input.Marker = output.KeyGroupList.NextMarker + } + + return sweeperErrs.ErrorOrNil() +} + +func TestAccAWSCloudFrontKeyGroup_basic(t *testing.T) { + rInt := acctest.RandInt() + resourceName := "aws_cloudfront_key_group.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(cloudfront.EndpointsID, t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCloudFrontKeyGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCloudFrontKeyGroupConfig(rInt), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudFrontKeyGroupExistence(resourceName), + resource.TestCheckResourceAttr("aws_cloudfront_key_group.test", "comment", "test key group"), + resource.TestCheckResourceAttrSet("aws_cloudfront_key_group.test", "etag"), + resource.TestCheckResourceAttrSet("aws_cloudfront_key_group.test", "id"), + resource.TestCheckResourceAttr("aws_cloudfront_key_group.test", "items.#", "1"), + resource.TestCheckResourceAttr("aws_cloudfront_key_group.test", "name", fmt.Sprintf("tf-acc-test-%d", rInt)), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAWSCloudFrontKeyGroup_disappears(t *testing.T) { + rInt := acctest.RandInt() + resourceName := "aws_cloudfront_key_group.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(cloudfront.EndpointsID, t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCloudFrontKeyGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCloudFrontKeyGroupConfig(rInt), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudFrontKeyGroupExistence(resourceName), + testAccCheckResourceDisappears(testAccProvider, resourceAwsCloudFrontKeyGroup(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func TestAccAWSCloudFrontKeyGroup_Comment(t *testing.T) { + rInt := acctest.RandInt() + resourceName := "aws_cloudfront_key_group.test" + + firstComment := "first comment" + secondComment := "second comment" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(cloudfront.EndpointsID, t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCloudFrontKeyGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCloudFrontKeyGroupConfigComment(rInt, firstComment), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudFrontKeyGroupExistence(resourceName), + resource.TestCheckResourceAttr("aws_cloudfront_key_group.test", "comment", firstComment), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSCloudFrontKeyGroupConfigComment(rInt, secondComment), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudFrontKeyGroupExistence(resourceName), + resource.TestCheckResourceAttr("aws_cloudfront_key_group.test", "comment", secondComment), + ), + }, + }, + }) +} + +func TestAccAWSCloudFrontKeyGroup_Items(t *testing.T) { + rInt := acctest.RandInt() + resourceName := "aws_cloudfront_key_group.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(cloudfront.EndpointsID, t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCloudFrontKeyGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCloudFrontKeyGroupConfig(rInt), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudFrontKeyGroupExistence(resourceName), + resource.TestCheckResourceAttr("aws_cloudfront_key_group.test", "items.#", "1"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSCloudFrontKeyGroupConfigItems(rInt), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudFrontKeyGroupExistence(resourceName), + resource.TestCheckResourceAttr("aws_cloudfront_key_group.test", "items.#", "2"), + ), + }, + }, + }) +} + +func testAccCheckCloudFrontKeyGroupExistence(r string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[r] + if !ok { + return fmt.Errorf("not found: %s", r) + } + if rs.Primary.ID == "" { + return fmt.Errorf("no Id is set") + } + + conn := testAccProvider.Meta().(*AWSClient).cloudfrontconn + + input := &cloudfront.GetKeyGroupInput{ + Id: aws.String(rs.Primary.ID), + } + + _, err := conn.GetKeyGroup(input) + if err != nil { + return fmt.Errorf("error retrieving CloudFront key group: %s", err) + } + return nil + } +} + +func testAccCheckCloudFrontKeyGroupDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).cloudfrontconn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_cloudfront_key_group" { + continue + } + + input := &cloudfront.GetKeyGroupInput{ + Id: aws.String(rs.Primary.ID), + } + + _, err := conn.GetKeyGroup(input) + if isAWSErr(err, cloudfront.ErrCodeNoSuchResource, "") { + continue + } + if err != nil { + return err + } + return fmt.Errorf("CloudFront key group (%s) was not deleted", rs.Primary.ID) + } + + return nil +} + +func testAccAWSCloudFrontKeyGroupConfigBase(rInt int) string { + return fmt.Sprintf(` +resource "aws_cloudfront_public_key" "test" { + comment = "test key" + encoded_key = file("test-fixtures/cloudfront-public-key.pem") + name = "tf-acc-test-%d" +} +`, rInt) +} + +func testAccAWSCloudFrontKeyGroupConfig(rInt int) string { + return testAccAWSCloudFrontKeyGroupConfigBase(rInt) + fmt.Sprintf(` +resource "aws_cloudfront_key_group" "test" { + comment = "test key group" + items = [aws_cloudfront_public_key.test.id] + name = "tf-acc-test-%d" +} +`, rInt) +} + +func testAccAWSCloudFrontKeyGroupConfigComment(rInt int, comment string) string { + return testAccAWSCloudFrontKeyGroupConfigBase(rInt) + fmt.Sprintf(` +resource "aws_cloudfront_key_group" "test" { + comment = %q + items = [aws_cloudfront_public_key.test.id] + name = "tf-acc-test-%d" +} +`, comment, rInt) +} + +func testAccAWSCloudFrontKeyGroupConfigItems(rInt int) string { + return testAccAWSCloudFrontKeyGroupConfigBase(rInt) + fmt.Sprintf(` +resource "aws_cloudfront_public_key" "test2" { + comment = "second test key" + encoded_key = file("test-fixtures/cloudfront-public-key.pem") + name = "tf-acc-test-second-%d" +} + +resource "aws_cloudfront_key_group" "test" { + comment = "test key group" + items = [aws_cloudfront_public_key.test.id, aws_cloudfront_public_key.test2.id] + name = "tf-acc-test-%d" +} +`, rInt, rInt) +} From aaa09e474541284b578dcda7ba7e7802d9972ad6 Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Sun, 10 Jan 2021 20:00:04 +0900 Subject: [PATCH 3/8] Add a doc for aws_cloudfront_key_group resource --- .../docs/r/cloudfront_key_group.html.markdown | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 website/docs/r/cloudfront_key_group.html.markdown diff --git a/website/docs/r/cloudfront_key_group.html.markdown b/website/docs/r/cloudfront_key_group.html.markdown new file mode 100644 index 000000000000..045fab5fe088 --- /dev/null +++ b/website/docs/r/cloudfront_key_group.html.markdown @@ -0,0 +1,42 @@ +--- +subcategory: "CloudFront" +layout: "aws" +page_title: "AWS: aws_cloudfront_key_group" +description: |- + Provides a CloudFront key group. +--- + +# Resource: aws_cloudfront_key_group + +## Example Usage + +The following example below creates a CloudFront key group. + +```hcl +resource "aws_cloudfront_public_key" "example" { + comment = "example public key" + encoded_key = file("public_key.pem") + name = "example-key" +} + +resource "aws_cloudfront_key_group" "example" { + comment = "example key group" + items = [aws_cloudfront_public_key.example.id] + name = "example-key-group" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `comment` - (Optional) A comment to describe the key group.. +* `items` - (Required) A list of the identifiers of the public keys in the key group. +* `name` - (Required) A name to identify the key group. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `etag` - The identifier for this version of the key group. +* `id` - The identifier for the key group. From a4a0633ac87d4d4114d56c141a8e78ecfcae3350 Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Sun, 4 Apr 2021 08:31:23 +0900 Subject: [PATCH 4/8] Remove the id attribute --- aws/resource_aws_cloudfront_key_group.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/aws/resource_aws_cloudfront_key_group.go b/aws/resource_aws_cloudfront_key_group.go index 26f7e60a894c..c42687108edd 100644 --- a/aws/resource_aws_cloudfront_key_group.go +++ b/aws/resource_aws_cloudfront_key_group.go @@ -28,10 +28,6 @@ func resourceAwsCloudFrontKeyGroup() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "id": { - Type: schema.TypeString, - Computed: true, - }, "items": { Type: schema.TypeSet, Elem: &schema.Schema{Type: schema.TypeString}, From 05ddc5890d64e0b63a4c4c72057fe95cb9c11dd5 Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Sun, 4 Apr 2021 08:51:14 +0900 Subject: [PATCH 5/8] Improve error handlings --- aws/resource_aws_cloudfront_key_group.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/aws/resource_aws_cloudfront_key_group.go b/aws/resource_aws_cloudfront_key_group.go index c42687108edd..fea7df8b68b2 100644 --- a/aws/resource_aws_cloudfront_key_group.go +++ b/aws/resource_aws_cloudfront_key_group.go @@ -49,11 +49,15 @@ func resourceAwsCloudFrontKeyGroupCreate(d *schema.ResourceData, meta interface{ KeyGroupConfig: expandCloudFrontKeyGroupConfig(d), } - log.Println("[DEBUG] Create CloudFront key group:", input) + log.Println("[DEBUG] Create CloudFront Key Group:", input) output, err := conn.CreateKeyGroup(input) if err != nil { - return fmt.Errorf("error creating CloudFront key group: %s", err) + return fmt.Errorf("error creating CloudFront Key Group: %w", err) + } + + if output == nil || output.KeyGroup == nil { + return fmt.Errorf("error creating CloudFront Key Group: empty response") } d.SetId(aws.StringValue(output.KeyGroup.Id)) @@ -68,18 +72,16 @@ func resourceAwsCloudFrontKeyGroupRead(d *schema.ResourceData, meta interface{}) output, err := conn.GetKeyGroup(input) if err != nil { - if isAWSErr(err, cloudfront.ErrCodeNoSuchResource, "") { + if !d.IsNewResource() && isAWSErr(err, cloudfront.ErrCodeNoSuchResource, "") { log.Printf("[WARN] No key group found: %s, removing from state", d.Id()) d.SetId("") return nil } - return err + return fmt.Errorf("error reading CloudFront Key Group (%s): %w", d.Id(), err) } if output == nil || output.KeyGroup == nil || output.KeyGroup.KeyGroupConfig == nil { - log.Printf("[WARN] No key group found: %s, removing from state", d.Id()) - d.SetId("") - return nil + return fmt.Errorf("error reading CloudFront Key Group: empty response") } keyGroupConfig := output.KeyGroup.KeyGroupConfig @@ -103,7 +105,7 @@ func resourceAwsCloudFrontKeyGroupUpdate(d *schema.ResourceData, meta interface{ _, err := conn.UpdateKeyGroup(input) if err != nil { - return fmt.Errorf("error updating CloudFront key group (%s): %s", d.Id(), err) + return fmt.Errorf("error updating CloudFront Key Group (%s): %w", d.Id(), err) } return resourceAwsCloudFrontKeyGroupRead(d, meta) @@ -122,7 +124,7 @@ func resourceAwsCloudFrontKeyGroupDelete(d *schema.ResourceData, meta interface{ if isAWSErr(err, cloudfront.ErrCodeNoSuchResource, "") { return nil } - return err + return fmt.Errorf("error deleting CloudFront Key Group (%s): %w", d.Id(), err) } return nil From 5b5517107ca45a2c69aa445f9ce1ec49d4c2c0ca Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Sun, 4 Apr 2021 09:10:02 +0900 Subject: [PATCH 6/8] Miscellaneous changes on the tests --- aws/resource_aws_cloudfront_key_group_test.go | 58 ++++++++++--------- ...s_cloudfront_origin_request_policy_test.go | 2 +- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/aws/resource_aws_cloudfront_key_group_test.go b/aws/resource_aws_cloudfront_key_group_test.go index e1d0b95259c7..0bcb9e8f728e 100644 --- a/aws/resource_aws_cloudfront_key_group_test.go +++ b/aws/resource_aws_cloudfront_key_group_test.go @@ -70,23 +70,24 @@ func testSweepCloudFrontKeyGroup(region string) error { } func TestAccAWSCloudFrontKeyGroup_basic(t *testing.T) { - rInt := acctest.RandInt() + rName := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_cloudfront_key_group.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(cloudfront.EndpointsID, t) }, + ErrorCheck: testAccErrorCheck(t, cloudfront.EndpointsID), Providers: testAccProviders, CheckDestroy: testAccCheckCloudFrontKeyGroupDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSCloudFrontKeyGroupConfig(rInt), + Config: testAccAWSCloudFrontKeyGroupConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckCloudFrontKeyGroupExistence(resourceName), resource.TestCheckResourceAttr("aws_cloudfront_key_group.test", "comment", "test key group"), resource.TestCheckResourceAttrSet("aws_cloudfront_key_group.test", "etag"), resource.TestCheckResourceAttrSet("aws_cloudfront_key_group.test", "id"), resource.TestCheckResourceAttr("aws_cloudfront_key_group.test", "items.#", "1"), - resource.TestCheckResourceAttr("aws_cloudfront_key_group.test", "name", fmt.Sprintf("tf-acc-test-%d", rInt)), + resource.TestCheckResourceAttr("aws_cloudfront_key_group.test", "name", rName), ), }, { @@ -99,16 +100,17 @@ func TestAccAWSCloudFrontKeyGroup_basic(t *testing.T) { } func TestAccAWSCloudFrontKeyGroup_disappears(t *testing.T) { - rInt := acctest.RandInt() + rName := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_cloudfront_key_group.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(cloudfront.EndpointsID, t) }, + ErrorCheck: testAccErrorCheck(t, cloudfront.EndpointsID), Providers: testAccProviders, CheckDestroy: testAccCheckCloudFrontKeyGroupDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSCloudFrontKeyGroupConfig(rInt), + Config: testAccAWSCloudFrontKeyGroupConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckCloudFrontKeyGroupExistence(resourceName), testAccCheckResourceDisappears(testAccProvider, resourceAwsCloudFrontKeyGroup(), resourceName), @@ -120,7 +122,7 @@ func TestAccAWSCloudFrontKeyGroup_disappears(t *testing.T) { } func TestAccAWSCloudFrontKeyGroup_Comment(t *testing.T) { - rInt := acctest.RandInt() + rName := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_cloudfront_key_group.test" firstComment := "first comment" @@ -128,11 +130,12 @@ func TestAccAWSCloudFrontKeyGroup_Comment(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(cloudfront.EndpointsID, t) }, + ErrorCheck: testAccErrorCheck(t, cloudfront.EndpointsID), Providers: testAccProviders, CheckDestroy: testAccCheckCloudFrontKeyGroupDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSCloudFrontKeyGroupConfigComment(rInt, firstComment), + Config: testAccAWSCloudFrontKeyGroupConfigComment(rName, firstComment), Check: resource.ComposeTestCheckFunc( testAccCheckCloudFrontKeyGroupExistence(resourceName), resource.TestCheckResourceAttr("aws_cloudfront_key_group.test", "comment", firstComment), @@ -144,7 +147,7 @@ func TestAccAWSCloudFrontKeyGroup_Comment(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccAWSCloudFrontKeyGroupConfigComment(rInt, secondComment), + Config: testAccAWSCloudFrontKeyGroupConfigComment(rName, secondComment), Check: resource.ComposeTestCheckFunc( testAccCheckCloudFrontKeyGroupExistence(resourceName), resource.TestCheckResourceAttr("aws_cloudfront_key_group.test", "comment", secondComment), @@ -155,16 +158,17 @@ func TestAccAWSCloudFrontKeyGroup_Comment(t *testing.T) { } func TestAccAWSCloudFrontKeyGroup_Items(t *testing.T) { - rInt := acctest.RandInt() + rName := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_cloudfront_key_group.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t); testAccPartitionHasServicePreCheck(cloudfront.EndpointsID, t) }, + ErrorCheck: testAccErrorCheck(t, cloudfront.EndpointsID), Providers: testAccProviders, CheckDestroy: testAccCheckCloudFrontKeyGroupDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSCloudFrontKeyGroupConfig(rInt), + Config: testAccAWSCloudFrontKeyGroupConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckCloudFrontKeyGroupExistence(resourceName), resource.TestCheckResourceAttr("aws_cloudfront_key_group.test", "items.#", "1"), @@ -176,7 +180,7 @@ func TestAccAWSCloudFrontKeyGroup_Items(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccAWSCloudFrontKeyGroupConfigItems(rInt), + Config: testAccAWSCloudFrontKeyGroupConfigItems(rName), Check: resource.ComposeTestCheckFunc( testAccCheckCloudFrontKeyGroupExistence(resourceName), resource.TestCheckResourceAttr("aws_cloudfront_key_group.test", "items.#", "2"), @@ -235,48 +239,48 @@ func testAccCheckCloudFrontKeyGroupDestroy(s *terraform.State) error { return nil } -func testAccAWSCloudFrontKeyGroupConfigBase(rInt int) string { +func testAccAWSCloudFrontKeyGroupConfigBase(rName string) string { return fmt.Sprintf(` resource "aws_cloudfront_public_key" "test" { comment = "test key" encoded_key = file("test-fixtures/cloudfront-public-key.pem") - name = "tf-acc-test-%d" + name = %q } -`, rInt) +`, rName) } -func testAccAWSCloudFrontKeyGroupConfig(rInt int) string { - return testAccAWSCloudFrontKeyGroupConfigBase(rInt) + fmt.Sprintf(` +func testAccAWSCloudFrontKeyGroupConfig(rName string) string { + return testAccAWSCloudFrontKeyGroupConfigBase(rName) + fmt.Sprintf(` resource "aws_cloudfront_key_group" "test" { comment = "test key group" items = [aws_cloudfront_public_key.test.id] - name = "tf-acc-test-%d" + name = %q } -`, rInt) +`, rName) } -func testAccAWSCloudFrontKeyGroupConfigComment(rInt int, comment string) string { - return testAccAWSCloudFrontKeyGroupConfigBase(rInt) + fmt.Sprintf(` +func testAccAWSCloudFrontKeyGroupConfigComment(rName string, comment string) string { + return testAccAWSCloudFrontKeyGroupConfigBase(rName) + fmt.Sprintf(` resource "aws_cloudfront_key_group" "test" { comment = %q items = [aws_cloudfront_public_key.test.id] - name = "tf-acc-test-%d" + name = %q } -`, comment, rInt) +`, comment, rName) } -func testAccAWSCloudFrontKeyGroupConfigItems(rInt int) string { - return testAccAWSCloudFrontKeyGroupConfigBase(rInt) + fmt.Sprintf(` +func testAccAWSCloudFrontKeyGroupConfigItems(rName string) string { + return testAccAWSCloudFrontKeyGroupConfigBase(rName) + fmt.Sprintf(` resource "aws_cloudfront_public_key" "test2" { comment = "second test key" encoded_key = file("test-fixtures/cloudfront-public-key.pem") - name = "tf-acc-test-second-%d" + name = "%[1]s-second" } resource "aws_cloudfront_key_group" "test" { comment = "test key group" items = [aws_cloudfront_public_key.test.id, aws_cloudfront_public_key.test2.id] - name = "tf-acc-test-%d" + name = %[1]q } -`, rInt, rInt) +`, rName) } diff --git a/aws/resource_aws_cloudfront_origin_request_policy_test.go b/aws/resource_aws_cloudfront_origin_request_policy_test.go index 52d22bfcd295..008ffe646527 100644 --- a/aws/resource_aws_cloudfront_origin_request_policy_test.go +++ b/aws/resource_aws_cloudfront_origin_request_policy_test.go @@ -118,7 +118,7 @@ func TestAccAWSCloudFrontOriginRequestPolicy_noneBehavior(t *testing.T) { func testAccAWSCloudFrontOriginRequestPolicyConfig(rInt int) string { return fmt.Sprintf(` resource "aws_cloudfront_origin_request_policy" "example" { - name = "test-policy%[1]d" + name = "test-policyz%[1]d" comment = "test comment" cookies_config { cookie_behavior = "whitelist" From 5cdd440d061254bd29ce2715de4b8699496abc29 Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Sun, 4 Apr 2021 09:11:30 +0900 Subject: [PATCH 7/8] Change the format from hcl to terraform --- website/docs/r/cloudfront_key_group.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/cloudfront_key_group.html.markdown b/website/docs/r/cloudfront_key_group.html.markdown index 045fab5fe088..84a8a2169022 100644 --- a/website/docs/r/cloudfront_key_group.html.markdown +++ b/website/docs/r/cloudfront_key_group.html.markdown @@ -12,7 +12,7 @@ description: |- The following example below creates a CloudFront key group. -```hcl +```terraform resource "aws_cloudfront_public_key" "example" { comment = "example public key" encoded_key = file("public_key.pem") From 92ae05572e5c012733502dd1dd9a7526f977b717 Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Sun, 4 Apr 2021 09:14:11 +0900 Subject: [PATCH 8/8] Add a changelog --- .changelog/17041.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/17041.txt diff --git a/.changelog/17041.txt b/.changelog/17041.txt new file mode 100644 index 000000000000..84097156d63c --- /dev/null +++ b/.changelog/17041.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +aws_cloudfront_key_group +```