-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resource/aws_cloudfront_key_group - new resource (#17041)
Output from acceptance testing in AWS Commercial: ``` --- PASS: TestAccAWSCloudFrontKeyGroup_disappears (10.83s) --- PASS: TestAccAWSCloudFrontKeyGroup_basic (12.39s) --- PASS: TestAccAWSCloudFrontKeyGroup_Comment (19.26s) --- PASS: TestAccAWSCloudFrontKeyGroup_Items (19.63s) ``` Output from acceptance testing in AWS GovCloud (US): ``` --- SKIP: TestAccAWSCloudFrontKeyGroup_basic (1.33s) --- SKIP: TestAccAWSCloudFrontKeyGroup_disappears (1.33s) --- SKIP: TestAccAWSCloudFrontKeyGroup_Comment (1.33s) --- SKIP: TestAccAWSCloudFrontKeyGroup_Items (1.33s) ```
- Loading branch information
1 parent
9346e5c
commit 29c750a
Showing
6 changed files
with
477 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
aws_cloudfront_key_group | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
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, | ||
}, | ||
"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: %w", err) | ||
} | ||
|
||
if output == nil || output.KeyGroup == nil { | ||
return fmt.Errorf("error creating CloudFront Key Group: empty response") | ||
} | ||
|
||
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 !d.IsNewResource() && isAWSErr(err, cloudfront.ErrCodeNoSuchResource, "") { | ||
log.Printf("[WARN] No key group found: %s, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("error reading CloudFront Key Group (%s): %w", d.Id(), err) | ||
} | ||
|
||
if output == nil || output.KeyGroup == nil || output.KeyGroup.KeyGroupConfig == nil { | ||
return fmt.Errorf("error reading CloudFront Key Group: empty response") | ||
} | ||
|
||
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): %w", d.Id(), err) | ||
} | ||
|
||
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 fmt.Errorf("error deleting CloudFront Key Group (%s): %w", d.Id(), err) | ||
} | ||
|
||
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 | ||
} |
Oops, something went wrong.