From 7a55fe4bcc2f1fb0ada387f1f49070d767a1e7a3 Mon Sep 17 00:00:00 2001 From: Matt Dainty Date: Tue, 14 Feb 2017 14:41:26 +0000 Subject: [PATCH] First stab at fixing hashicorp/terraform#9930 Add the following resources: * `aws_cloudwatch_log_destination` * `aws_cloudwatch_log_destination_policy` --- builtin/providers/aws/provider.go | 2 + ...resource_aws_cloudwatch_log_destination.go | 111 ++++++++++++++++++ ...e_aws_cloudwatch_log_destination_policy.go | 87 ++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 builtin/providers/aws/resource_aws_cloudwatch_log_destination.go create mode 100644 builtin/providers/aws/resource_aws_cloudwatch_log_destination_policy.go diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index 308d01a27..b5ca1bc0d 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -232,6 +232,8 @@ func Provider() terraform.ResourceProvider { "aws_cloudtrail": resourceAwsCloudTrail(), "aws_cloudwatch_event_rule": resourceAwsCloudWatchEventRule(), "aws_cloudwatch_event_target": resourceAwsCloudWatchEventTarget(), + "aws_cloudwatch_log_destination": resourceAwsCloudWatchLogDestination(), + "aws_cloudwatch_log_destination_policy": resourceAwsCloudWatchLogDestinationPolicy(), "aws_cloudwatch_log_group": resourceAwsCloudWatchLogGroup(), "aws_cloudwatch_log_metric_filter": resourceAwsCloudWatchLogMetricFilter(), "aws_cloudwatch_log_stream": resourceAwsCloudWatchLogStream(), diff --git a/builtin/providers/aws/resource_aws_cloudwatch_log_destination.go b/builtin/providers/aws/resource_aws_cloudwatch_log_destination.go new file mode 100644 index 000000000..f959919f6 --- /dev/null +++ b/builtin/providers/aws/resource_aws_cloudwatch_log_destination.go @@ -0,0 +1,111 @@ +package aws + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/cloudwatchlogs" +) + +func resourceAwsCloudWatchLogDestination() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsCloudWatchLogDestinationPut, + Update: resourceAwsCloudWatchLogDestinationPut, + + Read: resourceAwsCloudWatchLogDestinationRead, + Delete: resourceAwsCloudWatchLogDestinationDelete, + + Schema: map[string]*schema.Schema{ + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "role_arn": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + + "target_arn": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + + "arn": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func resourceAwsCloudWatchLogDestinationPut(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cloudwatchlogsconn + + name := d.Get("name").(string) + role_arn := d.Get("role_arn").(string) + target_arn := d.Get("target_arn").(string) + + params := &cloudwatchlogs.PutDestinationInput{ + DestinationName: aws.String(name), + RoleArn: aws.String(role_arn), + TargetArn: aws.String(target_arn), + } + + resp, err := conn.PutDestination(params) + + if err != nil { + return fmt.Errorf("Error creating Destination with name %s: %#v", name, err) + } + + d.SetId(*resp.Destination.Arn) + d.Set("arn", *resp.Destination.Arn) + return resourceAwsCloudWatchLogDestinationRead(d, meta) +} + +func resourceAwsCloudWatchLogDestinationRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cloudwatchlogsconn + + name := d.Get("name").(string) + + params := &cloudwatchlogs.DescribeDestinationsInput{ + DestinationNamePrefix: aws.String(name), + } + + resp, err := conn.DescribeDestinations(params) + if err != nil { + return fmt.Errorf("Error reading Destinations with name prefix %s: %#v", name, err) + } + + for _, destination := range resp.Destinations { + if *destination.DestinationName == name { + d.SetId(*destination.Arn) + d.Set("arn", *destination.Arn) + d.Set("role_arn", *destination.RoleArn) + d.Set("target_arn", *destination.TargetArn) + return nil + } + } + + d.SetId("") + return nil +} + +func resourceAwsCloudWatchLogDestinationDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cloudwatchlogsconn + + name := d.Get("name").(string) + + params := &cloudwatchlogs.DeleteDestinationInput{ + DestinationName: aws.String(name), + } + _, err := conn.DeleteDestination(params) + if err != nil { + return fmt.Errorf("Error deleting Destination with name %s", name) + } + d.SetId("") + return nil +} diff --git a/builtin/providers/aws/resource_aws_cloudwatch_log_destination_policy.go b/builtin/providers/aws/resource_aws_cloudwatch_log_destination_policy.go new file mode 100644 index 000000000..f5f06d1fb --- /dev/null +++ b/builtin/providers/aws/resource_aws_cloudwatch_log_destination_policy.go @@ -0,0 +1,87 @@ +package aws + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/cloudwatchlogs" +) + +func resourceAwsCloudWatchLogDestinationPolicy() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsCloudWatchLogDestinationPolicyPut, + Update: resourceAwsCloudWatchLogDestinationPolicyPut, + + Read: resourceAwsCloudWatchLogDestinationPolicyRead, + Delete: resourceAwsCloudWatchLogDestinationPolicyDelete, + + Schema: map[string]*schema.Schema{ + "destination_name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "access_policy": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + }, + } +} + +func resourceAwsCloudWatchLogDestinationPolicyPut(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cloudwatchlogsconn + + destination_name := d.Get("destination_name").(string) + access_policy := d.Get("access_policy").(string) + + params := &cloudwatchlogs.PutDestinationPolicyInput{ + DestinationName: aws.String(destination_name), + AccessPolicy: aws.String(access_policy), + } + + _, err := conn.PutDestinationPolicy(params) + + if err != nil { + return fmt.Errorf("Error creating DestinationPolicy with destination_name %s: %#v", destination_name, err) + } + + d.SetId(destination_name) + return resourceAwsCloudWatchLogDestinationPolicyRead(d, meta) +} + +func resourceAwsCloudWatchLogDestinationPolicyRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).cloudwatchlogsconn + + destination_name := d.Get("destination_name").(string) + + params := &cloudwatchlogs.DescribeDestinationsInput{ + DestinationNamePrefix: aws.String(destination_name), + } + + resp, err := conn.DescribeDestinations(params) + if err != nil { + return fmt.Errorf("Error reading Destinations with name prefix %s: %#v", destination_name, err) + } + + for _, destination := range resp.Destinations { + if *destination.DestinationName == destination_name { + if destination.AccessPolicy != nil { + d.Set("access_policy", *destination.AccessPolicy) + } + d.SetId(destination_name) + return nil + } + } + + d.SetId("") + return nil +} + +func resourceAwsCloudWatchLogDestinationPolicyDelete(d *schema.ResourceData, meta interface{}) error { + d.SetId("") + return nil +}