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)
This commit is contained in:
Ross Delinger 2016-07-12 14:18:37 -07:00
parent d6d0c9087e
commit 6ef7f9cd4d
4 changed files with 72 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"reflect" "reflect"
"sort"
"strconv" "strconv"
"time" "time"
@ -25,6 +26,14 @@ import (
// is used to set the zone_id attribute. // is used to set the zone_id attribute.
const cloudFrontRoute53ZoneID = "Z2FDTNDATAQYW2" 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 // Assemble the *cloudfront.DistributionConfig variable. Calls out to various
// expander functions to convert attributes and sub-attributes to the various // expander functions to convert attributes and sub-attributes to the various
// complex structures which are necessary to properly build the // 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 { if v, ok := m["locations"]; ok {
gr.Quantity = aws.Int64(int64(len(v.([]interface{})))) gr.Quantity = aws.Int64(int64(len(v.([]interface{}))))
gr.Items = expandStringList(v.([]interface{})) gr.Items = expandStringList(v.([]interface{}))
sort.Sort(StringPtrSlice(gr.Items))
} else { } else {
gr.Quantity = aws.Int64(0) gr.Quantity = aws.Int64(0)
} }
@ -884,6 +894,7 @@ func flattenGeoRestriction(gr *cloudfront.GeoRestriction) map[string]interface{}
m["restriction_type"] = *gr.RestrictionType m["restriction_type"] = *gr.RestrictionType
if gr.Items != nil { if gr.Items != nil {
sort.Sort(StringPtrSlice(gr.Items))
m["locations"] = flattenStringList(gr.Items) m["locations"] = flattenStringList(gr.Items)
} }
return m return m

View File

@ -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
}

View File

@ -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"},
},
},
})
}

View File

@ -16,6 +16,9 @@ func resourceAwsCloudFrontDistribution() *schema.Resource {
Read: resourceAwsCloudFrontDistributionRead, Read: resourceAwsCloudFrontDistributionRead,
Update: resourceAwsCloudFrontDistributionUpdate, Update: resourceAwsCloudFrontDistributionUpdate,
Delete: resourceAwsCloudFrontDistributionDelete, Delete: resourceAwsCloudFrontDistributionDelete,
Importer: &schema.ResourceImporter{
State: resourceAwsCloudFrontDistributionImport,
},
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"aliases": &schema.Schema{ "aliases": &schema.Schema{