Skip to content

Commit

Permalink
Merge pull request #18083 from shuheiktgw/add_aws_cloudfront_monitori…
Browse files Browse the repository at this point in the history
…ng_subscription_resource

Add aws_cloudfront_monitoring_subscription resource
  • Loading branch information
bill-rich authored Jun 30, 2021
2 parents d8b8725 + 26a7810 commit 97b02df
Show file tree
Hide file tree
Showing 6 changed files with 513 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/18083.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_cloudfront_monitoring_subscription
```
19 changes: 19 additions & 0 deletions aws/internal/service/cloudfront/finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,22 @@ func RealtimeLogConfigByARN(conn *cloudfront.CloudFront, arn string) (*cloudfron

return output.RealtimeLogConfig, nil
}

// MonitoringSubscriptionByDistributionId returns the monitoring subscription corresponding to the specified distribution id.
// Returns nil if no subscription is found.
func MonitoringSubscriptionByDistributionId(conn *cloudfront.CloudFront, id string) (*cloudfront.MonitoringSubscription, error) {
input := &cloudfront.GetMonitoringSubscriptionInput{
DistributionId: aws.String(id),
}

output, err := conn.GetMonitoringSubscription(input)
if err != nil {
return nil, err
}

if output == nil {
return nil, nil
}

return output.MonitoringSubscription, nil
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ func Provider() *schema.Provider {
"aws_cloudfront_distribution": resourceAwsCloudFrontDistribution(),
"aws_cloudfront_function": resourceAwsCloudFrontFunction(),
"aws_cloudfront_key_group": resourceAwsCloudFrontKeyGroup(),
"aws_cloudfront_monitoring_subscription": resourceAwsCloudFrontMonitoringSubscription(),
"aws_cloudfront_origin_access_identity": resourceAwsCloudFrontOriginAccessIdentity(),
"aws_cloudfront_origin_request_policy": resourceAwsCloudFrontOriginRequestPolicy(),
"aws_cloudfront_public_key": resourceAwsCloudFrontPublicKey(),
Expand Down
167 changes: 167 additions & 0 deletions aws/resource_aws_cloudfront_monitoring_subscription.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/cloudfront/finder"
)

func resourceAwsCloudFrontMonitoringSubscription() *schema.Resource {
return &schema.Resource{
Create: resourceAwsCloudFrontMonitoringSubscriptionCreate,
Read: resourceAwsCloudFrontMonitoringSubscriptionRead,
Update: resourceAwsCloudFrontMonitoringSubscriptionCreate,
Delete: resourceAwsCloudFrontMonitoringSubscriptionDelete,
Importer: &schema.ResourceImporter{
State: resourceAwsCloudFrontMonitoringSubscriptionImport,
},

Schema: map[string]*schema.Schema{
"distribution_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"monitoring_subscription": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"realtime_metrics_subscription_config": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"realtime_metrics_subscription_status": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(cloudfront.RealtimeMetricsSubscriptionStatus_Values(), false),
},
},
},
},
},
},
},
},
}
}

func resourceAwsCloudFrontMonitoringSubscriptionCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudfrontconn

id := d.Get("distribution_id").(string)
input := &cloudfront.CreateMonitoringSubscriptionInput{
DistributionId: aws.String(id),
MonitoringSubscription: expandCloudFrontMonitoringSubscription(d.Get("monitoring_subscription").([]interface{})),
}

log.Printf("[DEBUG] Creating CloudFront Monitoring Subscription: %s", input)
_, err := conn.CreateMonitoringSubscription(input)

if err != nil {
return fmt.Errorf("error creating CloudFront Monitoring Subscription (%s): %w", id, err)
}

d.SetId(id)

return resourceAwsCloudFrontMonitoringSubscriptionRead(d, meta)
}

func resourceAwsCloudFrontMonitoringSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudfrontconn

subscription, err := finder.MonitoringSubscriptionByDistributionId(conn, d.Id())

if tfawserr.ErrCodeEquals(err, cloudfront.ErrCodeNoSuchDistribution) {
log.Printf("[WARN] CloudFront Distribution (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading CloudFront Monitoring Subscription (%s): %w", d.Id(), err)
}

if subscription == nil {
if d.IsNewResource() {
return fmt.Errorf("error reading CloudFront Monitoring Subscription (%s): not found", d.Id())
}
log.Printf("[WARN] CloudFront Monitoring Subscription (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err := d.Set("monitoring_subscription", flattenCloudFrontMonitoringSubscription(subscription)); err != nil {
return fmt.Errorf("error setting monitoring_subscription: %w", err)
}

return nil
}

func resourceAwsCloudFrontMonitoringSubscriptionDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudfrontconn

log.Printf("[DEBUG] Deleting CloudFront Monitoring Subscription (%s)", d.Id())
_, err := conn.DeleteMonitoringSubscription(&cloudfront.DeleteMonitoringSubscriptionInput{
DistributionId: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, cloudfront.ErrCodeNoSuchDistribution) {
return nil
}

if err != nil {
return fmt.Errorf("error deleting CloudFront Monitoring Subscription (%s): %w", d.Id(), err)
}

return nil
}

func expandCloudFrontMonitoringSubscription(vSubscription []interface{}) *cloudfront.MonitoringSubscription {
if len(vSubscription) == 0 || vSubscription[0] == nil {
return nil
}

mSubscription := vSubscription[0].(map[string]interface{})

return &cloudfront.MonitoringSubscription{
RealtimeMetricsSubscriptionConfig: expandCloudFrontRealtimeMetricsSubscriptionConfig(mSubscription["realtime_metrics_subscription_config"].([]interface{})),
}
}

func expandCloudFrontRealtimeMetricsSubscriptionConfig(vConfig []interface{}) *cloudfront.RealtimeMetricsSubscriptionConfig {
if len(vConfig) == 0 || vConfig[0] == nil {
return nil
}

mConfig := vConfig[0].(map[string]interface{})

return &cloudfront.RealtimeMetricsSubscriptionConfig{
RealtimeMetricsSubscriptionStatus: aws.String(mConfig["realtime_metrics_subscription_status"].(string)),
}
}

func flattenCloudFrontMonitoringSubscription(subscription *cloudfront.MonitoringSubscription) []interface{} {
return []interface{}{map[string]interface{}{"realtime_metrics_subscription_config": flattenCloudFrontRealtimeMetricsSubscriptionConfig(subscription.RealtimeMetricsSubscriptionConfig)}}
}

func flattenCloudFrontRealtimeMetricsSubscriptionConfig(config *cloudfront.RealtimeMetricsSubscriptionConfig) []interface{} {
return []interface{}{map[string]interface{}{"realtime_metrics_subscription_status": config.RealtimeMetricsSubscriptionStatus}}
}

func resourceAwsCloudFrontMonitoringSubscriptionImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
d.Set("distribution_id", d.Id())
return []*schema.ResourceData{d}, nil
}
Loading

0 comments on commit 97b02df

Please sign in to comment.