github.com/ottenhoff/terraform@v0.7.0-rc1.0.20160607213102-ac2d195cc560/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  			"iam_arn": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Computed: true,
    40  			},
    41  			"s3_canonical_user_id": &schema.Schema{
    42  				Type:     schema.TypeString,
    43  				Computed: true,
    44  			},
    45  		},
    46  	}
    47  }
    48  
    49  func resourceAwsCloudFrontOriginAccessIdentityCreate(d *schema.ResourceData, meta interface{}) error {
    50  	conn := meta.(*AWSClient).cloudfrontconn
    51  	params := &cloudfront.CreateCloudFrontOriginAccessIdentityInput{
    52  		CloudFrontOriginAccessIdentityConfig: expandOriginAccessIdentityConfig(d),
    53  	}
    54  
    55  	resp, err := conn.CreateCloudFrontOriginAccessIdentity(params)
    56  	if err != nil {
    57  		return err
    58  	}
    59  	d.SetId(*resp.CloudFrontOriginAccessIdentity.Id)
    60  	return resourceAwsCloudFrontOriginAccessIdentityRead(d, meta)
    61  }
    62  
    63  func resourceAwsCloudFrontOriginAccessIdentityRead(d *schema.ResourceData, meta interface{}) error {
    64  	conn := meta.(*AWSClient).cloudfrontconn
    65  	params := &cloudfront.GetCloudFrontOriginAccessIdentityInput{
    66  		Id: aws.String(d.Id()),
    67  	}
    68  
    69  	resp, err := conn.GetCloudFrontOriginAccessIdentity(params)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	// Update attributes from DistributionConfig
    75  	flattenOriginAccessIdentityConfig(d, resp.CloudFrontOriginAccessIdentity.CloudFrontOriginAccessIdentityConfig)
    76  	// Update other attributes outside of DistributionConfig
    77  	d.SetId(*resp.CloudFrontOriginAccessIdentity.Id)
    78  	d.Set("etag", resp.ETag)
    79  	d.Set("s3_canonical_user_id", resp.CloudFrontOriginAccessIdentity.S3CanonicalUserId)
    80  	d.Set("cloudfront_access_identity_path", fmt.Sprintf("origin-access-identity/cloudfront/%s", *resp.CloudFrontOriginAccessIdentity.Id))
    81  	d.Set("iam_arn", fmt.Sprintf("arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity %s", *resp.CloudFrontOriginAccessIdentity.Id))
    82  	return nil
    83  }
    84  
    85  func resourceAwsCloudFrontOriginAccessIdentityUpdate(d *schema.ResourceData, meta interface{}) error {
    86  	conn := meta.(*AWSClient).cloudfrontconn
    87  	params := &cloudfront.UpdateCloudFrontOriginAccessIdentityInput{
    88  		Id: aws.String(d.Id()),
    89  		CloudFrontOriginAccessIdentityConfig: expandOriginAccessIdentityConfig(d),
    90  		IfMatch: aws.String(d.Get("etag").(string)),
    91  	}
    92  	_, err := conn.UpdateCloudFrontOriginAccessIdentity(params)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	return resourceAwsCloudFrontOriginAccessIdentityRead(d, meta)
    98  }
    99  
   100  func resourceAwsCloudFrontOriginAccessIdentityDelete(d *schema.ResourceData, meta interface{}) error {
   101  	conn := meta.(*AWSClient).cloudfrontconn
   102  	params := &cloudfront.DeleteCloudFrontOriginAccessIdentityInput{
   103  		Id:      aws.String(d.Id()),
   104  		IfMatch: aws.String(d.Get("etag").(string)),
   105  	}
   106  
   107  	_, err := conn.DeleteCloudFrontOriginAccessIdentity(params)
   108  	if err != nil {
   109  		return err
   110  	}
   111  
   112  	// Done
   113  	d.SetId("")
   114  	return nil
   115  }
   116  
   117  func expandOriginAccessIdentityConfig(d *schema.ResourceData) *cloudfront.OriginAccessIdentityConfig {
   118  	originAccessIdentityConfig := &cloudfront.OriginAccessIdentityConfig{
   119  		Comment: aws.String(d.Get("comment").(string)),
   120  	}
   121  	// This sets CallerReference if it's still pending computation (ie: new resource)
   122  	if v, ok := d.GetOk("caller_reference"); ok == false {
   123  		originAccessIdentityConfig.CallerReference = aws.String(time.Now().Format(time.RFC3339Nano))
   124  	} else {
   125  		originAccessIdentityConfig.CallerReference = aws.String(v.(string))
   126  	}
   127  	return originAccessIdentityConfig
   128  }
   129  
   130  func flattenOriginAccessIdentityConfig(d *schema.ResourceData, originAccessIdentityConfig *cloudfront.OriginAccessIdentityConfig) {
   131  	if originAccessIdentityConfig.Comment != nil {
   132  		d.Set("comment", originAccessIdentityConfig.Comment)
   133  	}
   134  	d.Set("caller_reference", originAccessIdentityConfig.CallerReference)
   135  }