From 6ef7f9cd4d641419ad89ec101753ab88fed18cad Mon Sep 17 00:00:00 2001 From: Ross Delinger Date: Tue, 12 Jul 2016 14:18:37 -0700 Subject: [PATCH 1/2] Add import support to CloudFront Distributions. * Import support and acceptance tests for import support have been added. * geo_restriction.location is now guarnteed to be in sorted order (was causing a failure in the test) --- ...nt_distribution_configuration_structure.go | 11 +++++++ .../aws/import_aws_cloudfront_distribution.go | 28 +++++++++++++++++ ...import_aws_cloudfront_distribution_test.go | 30 +++++++++++++++++++ .../resource_aws_cloudfront_distribution.go | 3 ++ 4 files changed, 72 insertions(+) create mode 100644 builtin/providers/aws/import_aws_cloudfront_distribution.go create mode 100644 builtin/providers/aws/import_aws_cloudfront_distribution_test.go diff --git a/builtin/providers/aws/cloudfront_distribution_configuration_structure.go b/builtin/providers/aws/cloudfront_distribution_configuration_structure.go index e6c2d998e..6d0896c01 100644 --- a/builtin/providers/aws/cloudfront_distribution_configuration_structure.go +++ b/builtin/providers/aws/cloudfront_distribution_configuration_structure.go @@ -11,6 +11,7 @@ import ( "bytes" "fmt" "reflect" + "sort" "strconv" "time" @@ -25,6 +26,14 @@ import ( // is used to set the zone_id attribute. const cloudFrontRoute53ZoneID = "Z2FDTNDATAQYW2" +// Define Sort interface for []*string so we can ensure the order of +// geo_restrictions.locations +type StringPtrSlice []*string + +func (p StringPtrSlice) Len() int { return len(p) } +func (p StringPtrSlice) Less(i, j int) bool { return *p[i] < *p[j] } +func (p StringPtrSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } + // Assemble the *cloudfront.DistributionConfig variable. Calls out to various // expander functions to convert attributes and sub-attributes to the various // complex structures which are necessary to properly build the @@ -873,6 +882,7 @@ func expandGeoRestriction(m map[string]interface{}) *cloudfront.GeoRestriction { if v, ok := m["locations"]; ok { gr.Quantity = aws.Int64(int64(len(v.([]interface{})))) gr.Items = expandStringList(v.([]interface{})) + sort.Sort(StringPtrSlice(gr.Items)) } else { gr.Quantity = aws.Int64(0) } @@ -884,6 +894,7 @@ func flattenGeoRestriction(gr *cloudfront.GeoRestriction) map[string]interface{} m["restriction_type"] = *gr.RestrictionType if gr.Items != nil { + sort.Sort(StringPtrSlice(gr.Items)) m["locations"] = flattenStringList(gr.Items) } return m diff --git a/builtin/providers/aws/import_aws_cloudfront_distribution.go b/builtin/providers/aws/import_aws_cloudfront_distribution.go new file mode 100644 index 000000000..dcb8792a3 --- /dev/null +++ b/builtin/providers/aws/import_aws_cloudfront_distribution.go @@ -0,0 +1,28 @@ +package aws + +import ( + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/cloudfront" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceAwsCloudFrontDistributionImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + conn := meta.(*AWSClient).cloudfrontconn + id := d.Id() + resp, err := conn.GetDistributionConfig(&cloudfront.GetDistributionConfigInput{ + Id: aws.String(id), + }) + + if err != nil { + return nil, err + } + + distConfig := resp.DistributionConfig + results := make([]*schema.ResourceData, 1) + err = flattenDistributionConfig(d, distConfig) + if err != nil { + return nil, err + } + results[0] = d + return results, nil +} diff --git a/builtin/providers/aws/import_aws_cloudfront_distribution_test.go b/builtin/providers/aws/import_aws_cloudfront_distribution_test.go new file mode 100644 index 000000000..9c2bbed63 --- /dev/null +++ b/builtin/providers/aws/import_aws_cloudfront_distribution_test.go @@ -0,0 +1,30 @@ +package aws + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccCloudFrontDistribution_importBasic(t *testing.T) { + resourceName := "aws_cloudfront_distribution.s3_distribution" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCloudFrontDistributionDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSCloudFrontDistributionS3Config, + }, + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + // Ignore retain_on_delete since it doesn't come from the AWS + // API. + ImportStateVerifyIgnore: []string{"retain_on_delete"}, + }, + }, + }) +} diff --git a/builtin/providers/aws/resource_aws_cloudfront_distribution.go b/builtin/providers/aws/resource_aws_cloudfront_distribution.go index 9fc333279..4d6af9d94 100644 --- a/builtin/providers/aws/resource_aws_cloudfront_distribution.go +++ b/builtin/providers/aws/resource_aws_cloudfront_distribution.go @@ -16,6 +16,9 @@ func resourceAwsCloudFrontDistribution() *schema.Resource { Read: resourceAwsCloudFrontDistributionRead, Update: resourceAwsCloudFrontDistributionUpdate, Delete: resourceAwsCloudFrontDistributionDelete, + Importer: &schema.ResourceImporter{ + State: resourceAwsCloudFrontDistributionImport, + }, Schema: map[string]*schema.Schema{ "aliases": &schema.Schema{ From 399f1c20e0fccaec8c40ccb4ba1e6fca7c6e29fd Mon Sep 17 00:00:00 2001 From: stack72 Date: Thu, 21 Jul 2016 12:41:01 +0100 Subject: [PATCH 2/2] provider/aws: Rename the Basic Import test for CloudFront distributions --- .../providers/aws/import_aws_cloudfront_distribution_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/providers/aws/import_aws_cloudfront_distribution_test.go b/builtin/providers/aws/import_aws_cloudfront_distribution_test.go index 9c2bbed63..237a26d4b 100644 --- a/builtin/providers/aws/import_aws_cloudfront_distribution_test.go +++ b/builtin/providers/aws/import_aws_cloudfront_distribution_test.go @@ -6,7 +6,7 @@ import ( "github.com/hashicorp/terraform/helper/resource" ) -func TestAccCloudFrontDistribution_importBasic(t *testing.T) { +func TestAccAWSCloudFrontDistribution_importBasic(t *testing.T) { resourceName := "aws_cloudfront_distribution.s3_distribution" resource.Test(t, resource.TestCase{