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