-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
resource/aws_cloudfront_key_group - new resource #17041
Changes from 3 commits
95d13c4
d1e7ecb
aaa09e4
6fa6b9b
a4a0633
05ddc58
5b55171
5cdd440
92ae055
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,146 @@ | ||||||||||
package aws | ||||||||||
|
||||||||||
import ( | ||||||||||
"fmt" | ||||||||||
"log" | ||||||||||
|
||||||||||
"github.com/aws/aws-sdk-go/aws" | ||||||||||
"github.com/aws/aws-sdk-go/service/cloudfront" | ||||||||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||||||||||
) | ||||||||||
|
||||||||||
func resourceAwsCloudFrontKeyGroup() *schema.Resource { | ||||||||||
return &schema.Resource{ | ||||||||||
Create: resourceAwsCloudFrontKeyGroupCreate, | ||||||||||
Read: resourceAwsCloudFrontKeyGroupRead, | ||||||||||
Update: resourceAwsCloudFrontKeyGroupUpdate, | ||||||||||
Delete: resourceAwsCloudFrontKeyGroupDelete, | ||||||||||
Importer: &schema.ResourceImporter{ | ||||||||||
State: schema.ImportStatePassthrough, | ||||||||||
}, | ||||||||||
|
||||||||||
Schema: map[string]*schema.Schema{ | ||||||||||
"comment": { | ||||||||||
Type: schema.TypeString, | ||||||||||
Optional: true, | ||||||||||
}, | ||||||||||
"etag": { | ||||||||||
Type: schema.TypeString, | ||||||||||
Computed: true, | ||||||||||
}, | ||||||||||
"id": { | ||||||||||
Type: schema.TypeString, | ||||||||||
Computed: true, | ||||||||||
}, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A computed |
||||||||||
"items": { | ||||||||||
Type: schema.TypeSet, | ||||||||||
Elem: &schema.Schema{Type: schema.TypeString}, | ||||||||||
Set: schema.HashString, | ||||||||||
Required: true, | ||||||||||
}, | ||||||||||
"name": { | ||||||||||
Type: schema.TypeString, | ||||||||||
Required: true, | ||||||||||
}, | ||||||||||
}, | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
func resourceAwsCloudFrontKeyGroupCreate(d *schema.ResourceData, meta interface{}) error { | ||||||||||
conn := meta.(*AWSClient).cloudfrontconn | ||||||||||
|
||||||||||
input := &cloudfront.CreateKeyGroupInput{ | ||||||||||
KeyGroupConfig: expandCloudFrontKeyGroupConfig(d), | ||||||||||
} | ||||||||||
|
||||||||||
log.Println("[DEBUG] Create CloudFront key group:", input) | ||||||||||
|
||||||||||
output, err := conn.CreateKeyGroup(input) | ||||||||||
if err != nil { | ||||||||||
return fmt.Errorf("error creating CloudFront key group: %s", err) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: To help with some future static analysis we'll be enabling
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
bflad marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
d.SetId(aws.StringValue(output.KeyGroup.Id)) | ||||||||||
return resourceAwsCloudFrontKeyGroupRead(d, meta) | ||||||||||
} | ||||||||||
|
||||||||||
func resourceAwsCloudFrontKeyGroupRead(d *schema.ResourceData, meta interface{}) error { | ||||||||||
conn := meta.(*AWSClient).cloudfrontconn | ||||||||||
input := &cloudfront.GetKeyGroupInput{ | ||||||||||
Id: aws.String(d.Id()), | ||||||||||
} | ||||||||||
|
||||||||||
output, err := conn.GetKeyGroup(input) | ||||||||||
if err != nil { | ||||||||||
if isAWSErr(err, cloudfront.ErrCodeNoSuchResource, "") { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reference: #16796
Suggested change
|
||||||||||
log.Printf("[WARN] No key group found: %s, removing from state", d.Id()) | ||||||||||
d.SetId("") | ||||||||||
return nil | ||||||||||
} | ||||||||||
return err | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
if output == nil || output.KeyGroup == nil || output.KeyGroup.KeyGroupConfig == nil { | ||||||||||
log.Printf("[WARN] No key group found: %s, removing from state", d.Id()) | ||||||||||
d.SetId("") | ||||||||||
return nil | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should error here instead since it would indicate a faulty API (or resource) implementation:
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
keyGroupConfig := output.KeyGroup.KeyGroupConfig | ||||||||||
|
||||||||||
d.Set("name", keyGroupConfig.Name) | ||||||||||
d.Set("comment", keyGroupConfig.Comment) | ||||||||||
d.Set("items", flattenStringSet(keyGroupConfig.Items)) | ||||||||||
d.Set("etag", output.ETag) | ||||||||||
|
||||||||||
return nil | ||||||||||
} | ||||||||||
|
||||||||||
func resourceAwsCloudFrontKeyGroupUpdate(d *schema.ResourceData, meta interface{}) error { | ||||||||||
conn := meta.(*AWSClient).cloudfrontconn | ||||||||||
|
||||||||||
input := &cloudfront.UpdateKeyGroupInput{ | ||||||||||
Id: aws.String(d.Id()), | ||||||||||
KeyGroupConfig: expandCloudFrontKeyGroupConfig(d), | ||||||||||
IfMatch: aws.String(d.Get("etag").(string)), | ||||||||||
} | ||||||||||
|
||||||||||
_, err := conn.UpdateKeyGroup(input) | ||||||||||
if err != nil { | ||||||||||
return fmt.Errorf("error updating CloudFront key group (%s): %s", d.Id(), err) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit:
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
return resourceAwsCloudFrontKeyGroupRead(d, meta) | ||||||||||
} | ||||||||||
|
||||||||||
func resourceAwsCloudFrontKeyGroupDelete(d *schema.ResourceData, meta interface{}) error { | ||||||||||
conn := meta.(*AWSClient).cloudfrontconn | ||||||||||
|
||||||||||
input := &cloudfront.DeleteKeyGroupInput{ | ||||||||||
Id: aws.String(d.Id()), | ||||||||||
IfMatch: aws.String(d.Get("etag").(string)), | ||||||||||
} | ||||||||||
|
||||||||||
_, err := conn.DeleteKeyGroup(input) | ||||||||||
if err != nil { | ||||||||||
if isAWSErr(err, cloudfront.ErrCodeNoSuchResource, "") { | ||||||||||
return nil | ||||||||||
} | ||||||||||
return err | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
return nil | ||||||||||
} | ||||||||||
|
||||||||||
func expandCloudFrontKeyGroupConfig(d *schema.ResourceData) *cloudfront.KeyGroupConfig { | ||||||||||
keyGroupConfig := &cloudfront.KeyGroupConfig{ | ||||||||||
Items: expandStringSet(d.Get("items").(*schema.Set)), | ||||||||||
Name: aws.String(d.Get("name").(string)), | ||||||||||
} | ||||||||||
|
||||||||||
if v, ok := d.GetOk("comment"); ok { | ||||||||||
keyGroupConfig.Comment = aws.String(v.(string)) | ||||||||||
} | ||||||||||
|
||||||||||
return keyGroupConfig | ||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can changelog this by adding a
.changelog/17041.txt
file with contents such as: