From b9aeafbbbabb1292dae484aa7db19981a2a53b69 Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Tue, 28 Aug 2018 20:50:51 +0400 Subject: [PATCH 01/14] add schema for cloudfront_pubkey --- aws/resource_aws_cloudfront_public_key.go | 58 +++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 aws/resource_aws_cloudfront_public_key.go diff --git a/aws/resource_aws_cloudfront_public_key.go b/aws/resource_aws_cloudfront_public_key.go new file mode 100644 index 000000000000..c0951ecb0295 --- /dev/null +++ b/aws/resource_aws_cloudfront_public_key.go @@ -0,0 +1,58 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/cloudfront" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceAwsCloudFrontPublicKey() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsCloudFrontPublicKeyCreate, + Read: resourceAwsCloudFrontPublicKeyRead, + Update: resourceAwsCloudFrontPublicKeyUpdate, + Delete: resourceAwsCloudFrontPublicKeyDelete, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + ConflictsWith: []string{"name_prefix"}, + }, + "name_prefix": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + ConflictsWith: []string{"name"}, + }, + "encoded_key": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "comment": { + Type: schema.TypeString, + Optional: true, + }, + "etag": { + Type: schema.TypeString, + Computed: true, + }, + "location": { + Type: schema.TypeString, + Computed: true, + }, + "id": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} From e4a216e28224a127fa912cb4c8328862bbb7533c Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Wed, 29 Aug 2018 19:17:33 +0400 Subject: [PATCH 02/14] add validate func to verify key name --- aws/resource_aws_cloudfront_public_key.go | 2 ++ aws/validators.go | 27 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/aws/resource_aws_cloudfront_public_key.go b/aws/resource_aws_cloudfront_public_key.go index c0951ecb0295..4eea2c3534c0 100644 --- a/aws/resource_aws_cloudfront_public_key.go +++ b/aws/resource_aws_cloudfront_public_key.go @@ -24,6 +24,7 @@ func resourceAwsCloudFrontPublicKey() *schema.Resource { Computed: true, ForceNew: true, ConflictsWith: []string{"name_prefix"}, + ValidateFunc: validateCloudFrontPublicKeyName, }, "name_prefix": { Type: schema.TypeString, @@ -31,6 +32,7 @@ func resourceAwsCloudFrontPublicKey() *schema.Resource { Computed: true, ForceNew: true, ConflictsWith: []string{"name"}, + ValidateFunc: validateCloudFrontPublicKeyNamePrefix, }, "encoded_key": { Type: schema.TypeString, diff --git a/aws/validators.go b/aws/validators.go index e834c9239beb..c65ffd161eaf 100644 --- a/aws/validators.go +++ b/aws/validators.go @@ -1974,3 +1974,30 @@ func validateNeptuneEventSubscriptionNamePrefix(v interface{}, k string) (ws []s } return } + +func validateCloudFrontPublicKeyName(v interface{}, k string) (ws []string, errors []error) { + value := v.(string) + if !regexp.MustCompile(`^[0-9A-Za-z_-]+$`).MatchString(value) { + errors = append(errors, fmt.Errorf( + "only alphanumeric characters, underscores and hyphens allowed in %q", k)) + } + if len(value) > 128 { + errors = append(errors, fmt.Errorf( + "%q cannot be greater than 128 characters", k)) + } + return +} + +func validateCloudFrontPublicKeyNamePrefix(v interface{}, k string) (ws []string, errors []error) { + value := v.(string) + if !regexp.MustCompile(`^[0-9A-Za-z_-]+$`).MatchString(value) { + errors = append(errors, fmt.Errorf( + "only alphanumeric characters, underscores and hyphens allowed in %q", k)) + } + prefixMaxLength := 128 - resource.UniqueIDSuffixLength + if len(value) > prefixMaxLength { + errors = append(errors, fmt.Errorf( + "%q cannot be greater than %d characters", k, prefixMaxLength)) + } + return +} From 0975ac0327099ce2a4416aad3338cd00a64e9725 Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Wed, 29 Aug 2018 20:03:31 +0400 Subject: [PATCH 03/14] add create func for cloudfront_pubkey --- aws/resource_aws_cloudfront_public_key.go | 44 ++++++++++++++++++++--- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/aws/resource_aws_cloudfront_public_key.go b/aws/resource_aws_cloudfront_public_key.go index 4eea2c3534c0..265ac579c7f7 100644 --- a/aws/resource_aws_cloudfront_public_key.go +++ b/aws/resource_aws_cloudfront_public_key.go @@ -47,10 +47,6 @@ func resourceAwsCloudFrontPublicKey() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "location": { - Type: schema.TypeString, - Computed: true, - }, "id": { Type: schema.TypeString, Computed: true, @@ -58,3 +54,43 @@ func resourceAwsCloudFrontPublicKey() *schema.Resource { }, } } + +func resourceAwsCloudFrontPublicKeyCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cloudfrontconn + + if v, ok := d.GetOk("name"); ok { + d.Set("name", v.(string)) + } else if v, ok := d.GetOk("name_prefix"); ok { + d.Set("name", resource.PrefixedUniqueId(v.(string))) + } else { + d.Set("name", resource.PrefixedUniqueId("tf-")) + } + + request := &cloudfront.CreatePublicKeyInput{ + PublicKeyConfig: expandPublicKeyConfig(d), + } + + log.Println("[DEBUG] Create CloudFront PublicKey:", request) + + output, err := conn.CreatePublicKey(request) + if err != nil { + return fmt.Errorf("error creating CloudFront PublicKey: %s", err) + } + + d.SetId(aws.StringValue(output.PublicKey.Id)) + return resourceAwsCloudFrontPublicKeyRead(d, meta) +} + +func expandPublicKeyConfig(d *schema.ResourceData) *cloudfront.PublicKeyConfig { + publicKeyConfig := &cloudfront.PublicKeyConfig{ + CallerReference: aws.String(resource.UniqueId()), + EncodedKey: aws.String(d.Get("encoded_key").(string)), + Name: aws.String(d.Get("name").(string)), + } + + if v, ok := d.GetOk("comment"); ok { + publicKeyConfig.Comment = aws.String(v.(string)) + } + + return publicKeyConfig +} From 7d299ec4fcf15065c6f0bad5a0e5a1eb8c91b23f Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Wed, 29 Aug 2018 20:32:08 +0400 Subject: [PATCH 04/14] add read func for cloudfront_pubkey --- aws/resource_aws_cloudfront_public_key.go | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/aws/resource_aws_cloudfront_public_key.go b/aws/resource_aws_cloudfront_public_key.go index 265ac579c7f7..3305b4a81541 100644 --- a/aws/resource_aws_cloudfront_public_key.go +++ b/aws/resource_aws_cloudfront_public_key.go @@ -81,6 +81,36 @@ func resourceAwsCloudFrontPublicKeyCreate(d *schema.ResourceData, meta interface return resourceAwsCloudFrontPublicKeyRead(d, meta) } +func resourceAwsCloudFrontPublicKeyRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cloudfrontconn + request := &cloudfront.GetPublicKeyInput{ + Id: aws.String(d.Id()), + } + + output, err := conn.GetPublicKey(request) + if err != nil { + if isAWSErr(err, cloudfront.ErrCodeNoSuchPublicKey, "") { + log.Printf("[WARN] No PublicKey found: %s, removing from state", d.Id()) + d.SetId("") + return nil + } + return err + } + + var publicKeyConfig *cloudfront.PublicKeyConfig + publicKeyConfig = output.PublicKey.PublicKeyConfig + + d.Set("encoded_key", publicKeyConfig.EncodedKey) + d.Set("name", publicKeyConfig.Name) + if publicKeyConfig.Comment != nil { + d.Set("comment", publicKeyConfig.Comment) + } + + d.Set("etag", output.ETag) + + return nil +} + func expandPublicKeyConfig(d *schema.ResourceData) *cloudfront.PublicKeyConfig { publicKeyConfig := &cloudfront.PublicKeyConfig{ CallerReference: aws.String(resource.UniqueId()), From bc0af850218294eb4456b8fc668cc62c8502ceab Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Wed, 29 Aug 2018 20:47:30 +0400 Subject: [PATCH 05/14] update cloudfront_pubkey resource in provider --- aws/provider.go | 1 + 1 file changed, 1 insertion(+) diff --git a/aws/provider.go b/aws/provider.go index bf80ad573833..b985dff5825b 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -325,6 +325,7 @@ func Provider() terraform.ResourceProvider { "aws_cloudformation_stack": resourceAwsCloudFormationStack(), "aws_cloudfront_distribution": resourceAwsCloudFrontDistribution(), "aws_cloudfront_origin_access_identity": resourceAwsCloudFrontOriginAccessIdentity(), + "aws_cloudfront_public_key": resourceAwsCloudFrontPublicKey(), "aws_cloudtrail": resourceAwsCloudTrail(), "aws_cloudwatch_event_permission": resourceAwsCloudWatchEventPermission(), "aws_cloudwatch_event_rule": resourceAwsCloudWatchEventRule(), From 8c2d1db56c8ebf62dbca3b034547f0970df6fa1a Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Thu, 30 Aug 2018 18:44:30 +0400 Subject: [PATCH 06/14] add update func for cloudfront_pubkey --- aws/resource_aws_cloudfront_public_key.go | 34 +++++++++++++++++------ 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/aws/resource_aws_cloudfront_public_key.go b/aws/resource_aws_cloudfront_public_key.go index 3305b4a81541..4323cd546b27 100644 --- a/aws/resource_aws_cloudfront_public_key.go +++ b/aws/resource_aws_cloudfront_public_key.go @@ -19,25 +19,25 @@ func resourceAwsCloudFrontPublicKey() *schema.Resource { Schema: map[string]*schema.Schema{ "name": { - Type: schema.TypeString, - Optional: true, - Computed: true, - ForceNew: true, + Type: schema.TypeString, + Optional: true, + Computed: true, + //ForceNew: true, ConflictsWith: []string{"name_prefix"}, ValidateFunc: validateCloudFrontPublicKeyName, }, "name_prefix": { - Type: schema.TypeString, - Optional: true, - Computed: true, - ForceNew: true, + Type: schema.TypeString, + Optional: true, + Computed: true, + //ForceNew: true, ConflictsWith: []string{"name"}, ValidateFunc: validateCloudFrontPublicKeyNamePrefix, }, "encoded_key": { Type: schema.TypeString, Required: true, - ForceNew: true, + //ForceNew: true, }, "comment": { Type: schema.TypeString, @@ -111,6 +111,22 @@ func resourceAwsCloudFrontPublicKeyRead(d *schema.ResourceData, meta interface{} return nil } +func resourceAwsCloudFrontPublicKeyUpdate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cloudfrontconn + request := &cloudfront.UpdatePublicKeyInput{ + Id: aws.String(d.Id()), + PublicKeyConfig: expandPublicKeyConfig(d), + IfMatch: aws.String(d.Get("etag").(string)), + } + + _, err := conn.UpdatePublicKey(request) + if err != nil { + return fmt.Errorf("error updating CloudFront PublicKey (%s): %s", d.Id(), err) + } + + return resourceAwsCloudFrontPublicKeyRead(d, meta) +} + func expandPublicKeyConfig(d *schema.ResourceData) *cloudfront.PublicKeyConfig { publicKeyConfig := &cloudfront.PublicKeyConfig{ CallerReference: aws.String(resource.UniqueId()), From 32ca92190fd79eb77e63d78e879c2f72cefdd2a2 Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Thu, 30 Aug 2018 18:52:00 +0400 Subject: [PATCH 07/14] add delete func --- aws/resource_aws_cloudfront_public_key.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/aws/resource_aws_cloudfront_public_key.go b/aws/resource_aws_cloudfront_public_key.go index 4323cd546b27..8059b4590a33 100644 --- a/aws/resource_aws_cloudfront_public_key.go +++ b/aws/resource_aws_cloudfront_public_key.go @@ -113,6 +113,7 @@ func resourceAwsCloudFrontPublicKeyRead(d *schema.ResourceData, meta interface{} func resourceAwsCloudFrontPublicKeyUpdate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).cloudfrontconn + request := &cloudfront.UpdatePublicKeyInput{ Id: aws.String(d.Id()), PublicKeyConfig: expandPublicKeyConfig(d), @@ -127,6 +128,27 @@ func resourceAwsCloudFrontPublicKeyUpdate(d *schema.ResourceData, meta interface return resourceAwsCloudFrontPublicKeyRead(d, meta) } +func resourceAwsCloudFrontPublicKeyDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cloudfrontconn + + request := &cloudfront.DeletePublicKeyInput{ + Id: aws.String(d.Id()), + IfMatch: aws.String(d.Get("etag").(string)), + } + + _, err := conn.DeletePublicKey(request) + if err != nil { + if isAWSErr(err, cloudfront.ErrCodeNoSuchPublicKey, "") { + log.Printf("[WARN] No PublicKey found: %s, removing from state", d.Id()) + d.SetId("") + return nil + } + return err + } + + return nil +} + func expandPublicKeyConfig(d *schema.ResourceData) *cloudfront.PublicKeyConfig { publicKeyConfig := &cloudfront.PublicKeyConfig{ CallerReference: aws.String(resource.UniqueId()), From 745daa882a3995da0fc5f01d946076eb81e2cf37 Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Thu, 30 Aug 2018 19:03:15 +0400 Subject: [PATCH 08/14] fix the id issue --- aws/resource_aws_cloudfront_public_key.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/aws/resource_aws_cloudfront_public_key.go b/aws/resource_aws_cloudfront_public_key.go index 8059b4590a33..82b5e0813996 100644 --- a/aws/resource_aws_cloudfront_public_key.go +++ b/aws/resource_aws_cloudfront_public_key.go @@ -47,10 +47,6 @@ func resourceAwsCloudFrontPublicKey() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "id": { - Type: schema.TypeString, - Computed: true, - }, }, } } From 489865243405952a0f66e61c74e93bc127d519a9 Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Thu, 30 Aug 2018 19:25:34 +0400 Subject: [PATCH 09/14] fix the issues in update func --- aws/resource_aws_cloudfront_public_key.go | 39 ++++++++++++++++------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/aws/resource_aws_cloudfront_public_key.go b/aws/resource_aws_cloudfront_public_key.go index 82b5e0813996..4c6f6592982b 100644 --- a/aws/resource_aws_cloudfront_public_key.go +++ b/aws/resource_aws_cloudfront_public_key.go @@ -3,6 +3,7 @@ package aws import ( "fmt" "log" + "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/cloudfront" @@ -19,25 +20,25 @@ func resourceAwsCloudFrontPublicKey() *schema.Resource { Schema: map[string]*schema.Schema{ "name": { - Type: schema.TypeString, - Optional: true, - Computed: true, - //ForceNew: true, + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, ConflictsWith: []string{"name_prefix"}, ValidateFunc: validateCloudFrontPublicKeyName, }, "name_prefix": { - Type: schema.TypeString, - Optional: true, - Computed: true, - //ForceNew: true, + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, ConflictsWith: []string{"name"}, ValidateFunc: validateCloudFrontPublicKeyNamePrefix, }, "encoded_key": { Type: schema.TypeString, Required: true, - //ForceNew: true, + ForceNew: true, }, "comment": { Type: schema.TypeString, @@ -47,6 +48,10 @@ func resourceAwsCloudFrontPublicKey() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "caller_reference": { + Type: schema.TypeString, + Computed: true, + }, }, } } @@ -98,10 +103,15 @@ func resourceAwsCloudFrontPublicKeyRead(d *schema.ResourceData, meta interface{} d.Set("encoded_key", publicKeyConfig.EncodedKey) d.Set("name", publicKeyConfig.Name) + if publicKeyConfig.Comment != nil { d.Set("comment", publicKeyConfig.Comment) } + if publicKeyConfig.CallerReference != nil { + d.Set("caller_reference", publicKeyConfig.CallerReference) + } + d.Set("etag", output.ETag) return nil @@ -147,14 +157,19 @@ func resourceAwsCloudFrontPublicKeyDelete(d *schema.ResourceData, meta interface func expandPublicKeyConfig(d *schema.ResourceData) *cloudfront.PublicKeyConfig { publicKeyConfig := &cloudfront.PublicKeyConfig{ - CallerReference: aws.String(resource.UniqueId()), - EncodedKey: aws.String(d.Get("encoded_key").(string)), - Name: aws.String(d.Get("name").(string)), + EncodedKey: aws.String(d.Get("encoded_key").(string)), + Name: aws.String(d.Get("name").(string)), } if v, ok := d.GetOk("comment"); ok { publicKeyConfig.Comment = aws.String(v.(string)) } + if v, ok := d.GetOk("caller_reference"); ok { + publicKeyConfig.CallerReference = aws.String(v.(string)) + } else { + publicKeyConfig.CallerReference = aws.String(time.Now().Format(time.RFC3339Nano)) + } + return publicKeyConfig } From 9f50045ef15de3e3ba16db41654435986250db57 Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Thu, 30 Aug 2018 19:47:45 +0400 Subject: [PATCH 10/14] add unit tests for cloudfront_pubkey name validate funcs --- aws/validators_test.go | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/aws/validators_test.go b/aws/validators_test.go index 5427d1c6a2f0..0bb36a23d15f 100644 --- a/aws/validators_test.go +++ b/aws/validators_test.go @@ -2809,3 +2809,59 @@ func TestValidateNeptuneParamGroupNamePrefix(t *testing.T) { } } } + +func TestValidateCloudFrontPublicKeyName(t *testing.T) { + cases := []struct { + Value string + ErrCount int + }{ + { + Value: "testing123!", + ErrCount: 1, + }, + { + Value: "testing 123", + ErrCount: 1, + }, + { + Value: randomString(129), + ErrCount: 1, + }, + } + + for _, tc := range cases { + _, errors := validateCloudFrontPublicKeyName(tc.Value, "aws_cloudfront_public_key") + + if len(errors) != tc.ErrCount { + t.Fatalf("Expected the CloudFront PublicKey Name to trigger a validation error for %q", tc.Value) + } + } +} + +func TestValidateCloudFrontPublicKeyNamePrefix(t *testing.T) { + cases := []struct { + Value string + ErrCount int + }{ + { + Value: "testing123!", + ErrCount: 1, + }, + { + Value: "testing 123", + ErrCount: 1, + }, + { + Value: randomString(128), + ErrCount: 1, + }, + } + + for _, tc := range cases { + _, errors := validateCloudFrontPublicKeyNamePrefix(tc.Value, "aws_cloudfront_public_key") + + if len(errors) != tc.ErrCount { + t.Fatalf("Expected the CloudFront PublicKey Name to trigger a validation error for %q", tc.Value) + } + } +} From b83a500057ce261071d45cca2d3c46b763bcf405 Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Thu, 30 Aug 2018 21:53:43 +0400 Subject: [PATCH 11/14] add acceptance test for cloudfront_pubkey --- ...resource_aws_cloudfront_public_key_test.go | 156 ++++++++++++++++++ aws/test-fixtures/cloudfront-public-key.pem | 9 + 2 files changed, 165 insertions(+) create mode 100644 aws/resource_aws_cloudfront_public_key_test.go create mode 100644 aws/test-fixtures/cloudfront-public-key.pem diff --git a/aws/resource_aws_cloudfront_public_key_test.go b/aws/resource_aws_cloudfront_public_key_test.go new file mode 100644 index 000000000000..f3bf1787d29e --- /dev/null +++ b/aws/resource_aws_cloudfront_public_key_test.go @@ -0,0 +1,156 @@ +package aws + +import ( + "fmt" + "regexp" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/cloudfront" + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAWSCloudFrontPublicKey_basic(t *testing.T) { + rInt := acctest.RandInt() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCloudFrontPublicKeyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCloudFrontPublicKeyConfig(rInt), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudFrontPublicKeyExistence("aws_cloudfront_public_key.example"), + resource.TestCheckResourceAttr("aws_cloudfront_public_key.example", "comment", "test key"), + resource.TestMatchResourceAttr("aws_cloudfront_public_key.example", + "caller_reference", + regexp.MustCompile("^20[0-9]{2}.*")), + resource.TestCheckResourceAttr("aws_cloudfront_public_key.example", "name", fmt.Sprintf("tf-acc-test-%d", rInt)), + ), + }, + }, + }) +} + +func TestAccAWSCloudFrontPublicKey_namePrefix(t *testing.T) { + startsWithPrefix := regexp.MustCompile("^tf-acc-test-") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCloudFrontPublicKeyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCloudFrontPublicKeyConfig_namePrefix(), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudFrontPublicKeyExistence("aws_cloudfront_public_key.example"), + resource.TestMatchResourceAttr("aws_cloudfront_public_key.example", "name", startsWithPrefix), + ), + }, + }, + }) +} + +func TestAccAWSCloudFrontPublicKey_update(t *testing.T) { + rInt := acctest.RandInt() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCloudFrontPublicKeyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCloudFrontPublicKeyConfig(rInt), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudFrontPublicKeyExistence("aws_cloudfront_public_key.example"), + resource.TestCheckResourceAttr("aws_cloudfront_public_key.example", "comment", "test key"), + ), + }, + { + Config: testAccAWSCloudFrontPublicKeyConfigUpdate(rInt), + Check: resource.ComposeTestCheckFunc( + testAccCheckCloudFrontPublicKeyExistence("aws_cloudfront_public_key.example"), + resource.TestCheckResourceAttr("aws_cloudfront_public_key.example", "comment", "test key1"), + ), + }, + }, + }) +} + +func testAccCheckCloudFrontPublicKeyExistence(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 + + params := &cloudfront.GetPublicKeyInput{ + Id: aws.String(rs.Primary.ID), + } + + _, err := conn.GetPublicKey(params) + if err != nil { + return fmt.Errorf("Error retrieving CloudFront PublicKey: %s", err) + } + return nil + } +} + +func testAccCheckCloudFrontPublicKeyDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).cloudfrontconn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_cloudfront_public_key" { + continue + } + + params := &cloudfront.GetPublicKeyInput{ + Id: aws.String(rs.Primary.ID), + } + + _, err := conn.GetPublicKey(params) + if err == nil { + return fmt.Errorf("CloudFront PublicKey was not deleted") + } + } + + return nil +} + +func testAccAWSCloudFrontPublicKeyConfig(rInt int) string { + return fmt.Sprintf(` + resource "aws_cloudfront_public_key" "example" { + comment = "test key" + encoded_key = "${file("test-fixtures/cloudfront-public-key.pem")}" + name = "tf-acc-test-%d" +} +`, rInt) +} + +func testAccAWSCloudFrontPublicKeyConfig_namePrefix() string { + return fmt.Sprintf(` + resource "aws_cloudfront_public_key" "example" { + comment = "test key" + encoded_key = "${file("test-fixtures/cloudfront-public-key.pem")}" + name_prefix = "tf-acc-test-" +} +`) +} + +func testAccAWSCloudFrontPublicKeyConfigUpdate(rInt int) string { + return fmt.Sprintf(` + resource "aws_cloudfront_public_key" "example" { + comment = "test key1" + encoded_key = "${file("test-fixtures/cloudfront-public-key.pem")}" + name = "tf-acc-test-%d" +} +`, rInt) +} diff --git a/aws/test-fixtures/cloudfront-public-key.pem b/aws/test-fixtures/cloudfront-public-key.pem new file mode 100644 index 000000000000..d25ae696d392 --- /dev/null +++ b/aws/test-fixtures/cloudfront-public-key.pem @@ -0,0 +1,9 @@ +-----BEGIN PUBLIC KEY----- +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtZCjGTEV/ttumSJBnsc2 +SUzPY/wJjfNchT2mjWivg/S7HuwKp1tDHizxrXTVuZLdDKceVcSclS7otzwfmGxM +Gjk2/CM2hEMThT86q76TrbH6hvGa25n8piBOkhwbwdbvmg3DRJiLR9bqw+nAPt/n +1ggTcwazm1Bw7y112Ardop+buWirS3w2C6au2OdloaaLz5N1eHEHQuRpnmD+UoVR +OgGeaLaU7FxKkpOps4Giu4vgjcefGlM3MrqG4FAzDMtgGZdJm4U+bldYmk0+J1yv +JA0FGd9g9GhjHMT9UznxXccw7PhHQsXn4lQfOn47uO9KIq170t8FeHKEzbCMsmyA +2QIDAQAB +-----END PUBLIC KEY----- From e1f73b09546332a17232c6fe9a24f3bdc8c507ae Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Thu, 30 Aug 2018 22:21:01 +0400 Subject: [PATCH 12/14] add documentation --- website/aws.erb | 3 ++ .../r/cloudfront_public_key.html.markdown | 38 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 website/docs/r/cloudfront_public_key.html.markdown diff --git a/website/aws.erb b/website/aws.erb index c5710e4ddc89..1ee142c34771 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -566,6 +566,9 @@ > aws_cloudfront_origin_access_identity + > + aws_cloudfront_public_key + diff --git a/website/docs/r/cloudfront_public_key.html.markdown b/website/docs/r/cloudfront_public_key.html.markdown new file mode 100644 index 000000000000..11b4174be14b --- /dev/null +++ b/website/docs/r/cloudfront_public_key.html.markdown @@ -0,0 +1,38 @@ +--- +layout: "aws" +page_title: "AWS: cloudfront_public_key" +sidebar_current: "docs-aws-resource-cloudfront-public-key" +description: |- + Provides a CloudFront Public Key which you add to CloudFront to use with features like field-level encryption. +--- + +# aws_cloudfront_distribution + +## Example Usage + +The following example below creates a CloudFront public key. + +```hcl +resource "aws_cloudfront_public_key" "example" { + comment = "test public key" + encoded_key = "${file("public_key.pem")}" + name = "test_key" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `comment` - (Optional) An optional comment about the public key. +* `encoded_key` - (Required) The encoded public key that you want to add to CloudFront to use with features like field-level encryption. +* `name` - (Optional) The name for the public key. By default generated by Terraform. +* `name_prefix` - (Optional) The name for the public key. Conflicts with `name`. + +## Attributes Reference + +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`. From abfe7c49e90810657e9ed6b9401f2e3c439fcefc Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Thu, 30 Aug 2018 22:29:52 +0400 Subject: [PATCH 13/14] arrange the attributes order properly --- aws/resource_aws_cloudfront_public_key.go | 34 +++++++++++------------ 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/aws/resource_aws_cloudfront_public_key.go b/aws/resource_aws_cloudfront_public_key.go index 4c6f6592982b..7a6e819909f4 100644 --- a/aws/resource_aws_cloudfront_public_key.go +++ b/aws/resource_aws_cloudfront_public_key.go @@ -19,6 +19,23 @@ func resourceAwsCloudFrontPublicKey() *schema.Resource { Delete: resourceAwsCloudFrontPublicKeyDelete, Schema: map[string]*schema.Schema{ + "caller_reference": { + Type: schema.TypeString, + Computed: true, + }, + "comment": { + Type: schema.TypeString, + Optional: true, + }, + "encoded_key": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "etag": { + Type: schema.TypeString, + Computed: true, + }, "name": { Type: schema.TypeString, Optional: true, @@ -35,23 +52,6 @@ func resourceAwsCloudFrontPublicKey() *schema.Resource { ConflictsWith: []string{"name"}, ValidateFunc: validateCloudFrontPublicKeyNamePrefix, }, - "encoded_key": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - "comment": { - Type: schema.TypeString, - Optional: true, - }, - "etag": { - Type: schema.TypeString, - Computed: true, - }, - "caller_reference": { - Type: schema.TypeString, - Computed: true, - }, }, } } From 8f807218a36cc820b4a3cdc5067aba30f96e705c Mon Sep 17 00:00:00 2001 From: saravanan30erd Date: Wed, 12 Sep 2018 22:51:55 +0400 Subject: [PATCH 14/14] corrections based on PR #5737 feedback --- aws/resource_aws_cloudfront_public_key.go | 21 +++++++------------ ...resource_aws_cloudfront_public_key_test.go | 8 +++++-- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/aws/resource_aws_cloudfront_public_key.go b/aws/resource_aws_cloudfront_public_key.go index 7a6e819909f4..c0e20bcb7f56 100644 --- a/aws/resource_aws_cloudfront_public_key.go +++ b/aws/resource_aws_cloudfront_public_key.go @@ -98,20 +98,17 @@ func resourceAwsCloudFrontPublicKeyRead(d *schema.ResourceData, meta interface{} return err } - var publicKeyConfig *cloudfront.PublicKeyConfig - publicKeyConfig = output.PublicKey.PublicKeyConfig + if output == nil || output.PublicKey == nil || output.PublicKey.PublicKeyConfig == nil { + log.Printf("[WARN] No PublicKey found: %s, removing from state", d.Id()) + d.SetId("") + return nil + } + publicKeyConfig := output.PublicKey.PublicKeyConfig d.Set("encoded_key", publicKeyConfig.EncodedKey) d.Set("name", publicKeyConfig.Name) - - if publicKeyConfig.Comment != nil { - d.Set("comment", publicKeyConfig.Comment) - } - - if publicKeyConfig.CallerReference != nil { - d.Set("caller_reference", publicKeyConfig.CallerReference) - } - + d.Set("comment", publicKeyConfig.Comment) + d.Set("caller_reference", publicKeyConfig.CallerReference) d.Set("etag", output.ETag) return nil @@ -145,8 +142,6 @@ func resourceAwsCloudFrontPublicKeyDelete(d *schema.ResourceData, meta interface _, err := conn.DeletePublicKey(request) if err != nil { if isAWSErr(err, cloudfront.ErrCodeNoSuchPublicKey, "") { - log.Printf("[WARN] No PublicKey found: %s, removing from state", d.Id()) - d.SetId("") return nil } return err diff --git a/aws/resource_aws_cloudfront_public_key_test.go b/aws/resource_aws_cloudfront_public_key_test.go index f3bf1787d29e..cf3c38fde841 100644 --- a/aws/resource_aws_cloudfront_public_key_test.go +++ b/aws/resource_aws_cloudfront_public_key_test.go @@ -117,9 +117,13 @@ func testAccCheckCloudFrontPublicKeyDestroy(s *terraform.State) error { } _, err := conn.GetPublicKey(params) - if err == nil { - return fmt.Errorf("CloudFront PublicKey was not deleted") + if isAWSErr(err, cloudfront.ErrCodeNoSuchPublicKey, "") { + continue + } + if err != nil { + return err } + return fmt.Errorf("CloudFront PublicKey (%s) was not deleted", rs.Primary.ID) } return nil