github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Optional: true,
    26  				Default:  "",
    27  			},
    28  			"caller_reference": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Computed: true,
    31  			},
    32  			"cloudfront_access_identity_path": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Computed: true,
    35  			},
    36  			"etag": &schema.Schema{
    37  				Type:     schema.TypeString,
    38  				Computed: true,
    39  			},
    40  			"iam_arn": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Computed: true,
    43  			},
    44  			"s3_canonical_user_id": &schema.Schema{
    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:aws:iam::cloudfront:user/CloudFront Origin Access Identity %s", *resp.CloudFrontOriginAccessIdentity.Id))
    85  	return nil
    86  }
    87  
    88  func resourceAwsCloudFrontOriginAccessIdentityUpdate(d *schema.ResourceData, meta interface{}) error {
    89  	conn := meta.(*AWSClient).cloudfrontconn
    90  	params := &cloudfront.UpdateCloudFrontOriginAccessIdentityInput{
    91  		Id: aws.String(d.Id()),
    92  		CloudFrontOriginAccessIdentityConfig: expandOriginAccessIdentityConfig(d),
    93  		IfMatch: aws.String(d.Get("etag").(string)),
    94  	}
    95  	_, err := conn.UpdateCloudFrontOriginAccessIdentity(params)
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	return resourceAwsCloudFrontOriginAccessIdentityRead(d, meta)
   101  }
   102  
   103  func resourceAwsCloudFrontOriginAccessIdentityDelete(d *schema.ResourceData, meta interface{}) error {
   104  	conn := meta.(*AWSClient).cloudfrontconn
   105  	params := &cloudfront.DeleteCloudFrontOriginAccessIdentityInput{
   106  		Id:      aws.String(d.Id()),
   107  		IfMatch: aws.String(d.Get("etag").(string)),
   108  	}
   109  
   110  	_, err := conn.DeleteCloudFrontOriginAccessIdentity(params)
   111  	if err != nil {
   112  		return err
   113  	}
   114  
   115  	// Done
   116  	d.SetId("")
   117  	return nil
   118  }
   119  
   120  func expandOriginAccessIdentityConfig(d *schema.ResourceData) *cloudfront.OriginAccessIdentityConfig {
   121  	originAccessIdentityConfig := &cloudfront.OriginAccessIdentityConfig{
   122  		Comment: aws.String(d.Get("comment").(string)),
   123  	}
   124  	// This sets CallerReference if it's still pending computation (ie: new resource)
   125  	if v, ok := d.GetOk("caller_reference"); ok == false {
   126  		originAccessIdentityConfig.CallerReference = aws.String(time.Now().Format(time.RFC3339Nano))
   127  	} else {
   128  		originAccessIdentityConfig.CallerReference = aws.String(v.(string))
   129  	}
   130  	return originAccessIdentityConfig
   131  }
   132  
   133  func flattenOriginAccessIdentityConfig(d *schema.ResourceData, originAccessIdentityConfig *cloudfront.OriginAccessIdentityConfig) {
   134  	if originAccessIdentityConfig.Comment != nil {
   135  		d.Set("comment", originAccessIdentityConfig.Comment)
   136  	}
   137  	d.Set("caller_reference", originAccessIdentityConfig.CallerReference)
   138  }