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