github.com/ricardclau/terraform@v0.6.17-0.20160519222547-283e3ae6b5a9/builtin/providers/aws/resource_aws_cloudfront_origin_access_identity.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/service/cloudfront" 9 "github.com/hashicorp/terraform/helper/schema" 10 ) 11 12 func resourceAwsCloudFrontOriginAccessIdentity() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceAwsCloudFrontOriginAccessIdentityCreate, 15 Read: resourceAwsCloudFrontOriginAccessIdentityRead, 16 Update: resourceAwsCloudFrontOriginAccessIdentityUpdate, 17 Delete: resourceAwsCloudFrontOriginAccessIdentityDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "comment": &schema.Schema{ 21 Type: schema.TypeString, 22 Optional: true, 23 Default: "", 24 }, 25 "caller_reference": &schema.Schema{ 26 Type: schema.TypeString, 27 Computed: true, 28 }, 29 "cloudfront_access_identity_path": &schema.Schema{ 30 Type: schema.TypeString, 31 Computed: true, 32 }, 33 "etag": &schema.Schema{ 34 Type: schema.TypeString, 35 Computed: true, 36 }, 37 "s3_canonical_user_id": &schema.Schema{ 38 Type: schema.TypeString, 39 Computed: true, 40 }, 41 }, 42 } 43 } 44 45 func resourceAwsCloudFrontOriginAccessIdentityCreate(d *schema.ResourceData, meta interface{}) error { 46 conn := meta.(*AWSClient).cloudfrontconn 47 params := &cloudfront.CreateCloudFrontOriginAccessIdentityInput{ 48 CloudFrontOriginAccessIdentityConfig: expandOriginAccessIdentityConfig(d), 49 } 50 51 resp, err := conn.CreateCloudFrontOriginAccessIdentity(params) 52 if err != nil { 53 return err 54 } 55 d.SetId(*resp.CloudFrontOriginAccessIdentity.Id) 56 return resourceAwsCloudFrontOriginAccessIdentityRead(d, meta) 57 } 58 59 func resourceAwsCloudFrontOriginAccessIdentityRead(d *schema.ResourceData, meta interface{}) error { 60 conn := meta.(*AWSClient).cloudfrontconn 61 params := &cloudfront.GetCloudFrontOriginAccessIdentityInput{ 62 Id: aws.String(d.Id()), 63 } 64 65 resp, err := conn.GetCloudFrontOriginAccessIdentity(params) 66 if err != nil { 67 return err 68 } 69 70 // Update attributes from DistributionConfig 71 flattenOriginAccessIdentityConfig(d, resp.CloudFrontOriginAccessIdentity.CloudFrontOriginAccessIdentityConfig) 72 // Update other attributes outside of DistributionConfig 73 d.SetId(*resp.CloudFrontOriginAccessIdentity.Id) 74 d.Set("etag", resp.ETag) 75 d.Set("s3_canonical_user_id", resp.CloudFrontOriginAccessIdentity.S3CanonicalUserId) 76 d.Set("cloudfront_access_identity_path", fmt.Sprintf("origin-access-identity/cloudfront/%s", *resp.CloudFrontOriginAccessIdentity.Id)) 77 return nil 78 } 79 80 func resourceAwsCloudFrontOriginAccessIdentityUpdate(d *schema.ResourceData, meta interface{}) error { 81 conn := meta.(*AWSClient).cloudfrontconn 82 params := &cloudfront.UpdateCloudFrontOriginAccessIdentityInput{ 83 Id: aws.String(d.Id()), 84 CloudFrontOriginAccessIdentityConfig: expandOriginAccessIdentityConfig(d), 85 IfMatch: aws.String(d.Get("etag").(string)), 86 } 87 _, err := conn.UpdateCloudFrontOriginAccessIdentity(params) 88 if err != nil { 89 return err 90 } 91 92 return resourceAwsCloudFrontOriginAccessIdentityRead(d, meta) 93 } 94 95 func resourceAwsCloudFrontOriginAccessIdentityDelete(d *schema.ResourceData, meta interface{}) error { 96 conn := meta.(*AWSClient).cloudfrontconn 97 params := &cloudfront.DeleteCloudFrontOriginAccessIdentityInput{ 98 Id: aws.String(d.Id()), 99 IfMatch: aws.String(d.Get("etag").(string)), 100 } 101 102 _, err := conn.DeleteCloudFrontOriginAccessIdentity(params) 103 if err != nil { 104 return err 105 } 106 107 // Done 108 d.SetId("") 109 return nil 110 } 111 112 func expandOriginAccessIdentityConfig(d *schema.ResourceData) *cloudfront.OriginAccessIdentityConfig { 113 originAccessIdentityConfig := &cloudfront.OriginAccessIdentityConfig{ 114 Comment: aws.String(d.Get("comment").(string)), 115 } 116 // This sets CallerReference if it's still pending computation (ie: new resource) 117 if v, ok := d.GetOk("caller_reference"); ok == false { 118 originAccessIdentityConfig.CallerReference = aws.String(time.Now().Format(time.RFC3339Nano)) 119 } else { 120 originAccessIdentityConfig.CallerReference = aws.String(v.(string)) 121 } 122 return originAccessIdentityConfig 123 } 124 125 func flattenOriginAccessIdentityConfig(d *schema.ResourceData, originAccessIdentityConfig *cloudfront.OriginAccessIdentityConfig) { 126 if originAccessIdentityConfig.Comment != nil { 127 d.Set("comment", originAccessIdentityConfig.Comment) 128 } 129 d.Set("caller_reference", originAccessIdentityConfig.CallerReference) 130 }