2017-02-14 15:41:26 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-02-15 12:35:39 +01:00
|
|
|
"strings"
|
2017-02-15 10:39:37 +01:00
|
|
|
"time"
|
2017-02-14 15:41:26 +01:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2017-02-15 10:39:37 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2017-02-14 15:41:26 +01:00
|
|
|
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
|
2017-02-15 10:39:37 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2017-02-14 15:41:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsCloudWatchLogDestination() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsCloudWatchLogDestinationPut,
|
|
|
|
Update: resourceAwsCloudWatchLogDestinationPut,
|
|
|
|
Read: resourceAwsCloudWatchLogDestinationRead,
|
|
|
|
Delete: resourceAwsCloudWatchLogDestinationDelete,
|
|
|
|
|
2017-02-14 19:09:48 +01:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
|
|
|
d.Set("name", d.Id())
|
|
|
|
return []*schema.ResourceData{d}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2017-02-14 15:41:26 +01:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
|
2017-02-15 12:35:39 +01:00
|
|
|
return resource.Retry(3*time.Minute, func() *resource.RetryError {
|
2017-02-15 10:39:37 +01:00
|
|
|
resp, err := conn.PutDestination(params)
|
2017-02-14 15:41:26 +01:00
|
|
|
|
2017-02-15 10:39:37 +01:00
|
|
|
if err == nil {
|
2017-02-15 15:59:28 +01:00
|
|
|
d.SetId(name)
|
2017-02-15 10:39:37 +01:00
|
|
|
d.Set("arn", *resp.Destination.Arn)
|
|
|
|
}
|
|
|
|
|
|
|
|
awsErr, ok := err.(awserr.Error)
|
|
|
|
if !ok {
|
|
|
|
return resource.RetryableError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if awsErr.Code() == "InvalidParameterException" {
|
2017-02-15 12:35:39 +01:00
|
|
|
if strings.Contains(awsErr.Message(), "Could not deliver test message to specified") {
|
|
|
|
return resource.RetryableError(err)
|
|
|
|
}
|
2017-02-15 10:39:37 +01:00
|
|
|
return resource.NonRetryableError(err)
|
|
|
|
}
|
2017-02-14 15:41:26 +01:00
|
|
|
|
2017-02-15 10:39:37 +01:00
|
|
|
return resource.NonRetryableError(err)
|
|
|
|
})
|
2017-02-14 15:41:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsCloudWatchLogDestinationRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).cloudwatchlogsconn
|
|
|
|
name := d.Get("name").(string)
|
2017-02-15 15:59:28 +01:00
|
|
|
destination, exists, err := lookupCloudWatchLogDestination(conn, name, nil)
|
2017-02-14 15:41:26 +01:00
|
|
|
if err != nil {
|
2017-02-15 15:59:28 +01:00
|
|
|
return err
|
2017-02-14 15:41:26 +01:00
|
|
|
}
|
|
|
|
|
2017-02-15 15:59:28 +01:00
|
|
|
if !exists {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
2017-02-14 15:41:26 +01:00
|
|
|
}
|
|
|
|
|
2017-02-15 15:59:28 +01:00
|
|
|
d.SetId(name)
|
|
|
|
d.Set("arn", destination.Arn)
|
|
|
|
d.Set("role_arn", destination.RoleArn)
|
|
|
|
d.Set("target_arn", destination.TargetArn)
|
|
|
|
|
2017-02-14 15:41:26 +01:00
|
|
|
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
|
|
|
|
}
|
2017-02-15 15:59:28 +01:00
|
|
|
|
|
|
|
func lookupCloudWatchLogDestination(conn *cloudwatchlogs.CloudWatchLogs,
|
|
|
|
name string, nextToken *string) (*cloudwatchlogs.Destination, bool, error) {
|
|
|
|
input := &cloudwatchlogs.DescribeDestinationsInput{
|
|
|
|
DestinationNamePrefix: aws.String(name),
|
|
|
|
NextToken: nextToken,
|
|
|
|
}
|
|
|
|
resp, err := conn.DescribeDestinations(input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, destination := range resp.Destinations {
|
|
|
|
if *destination.DestinationName == name {
|
|
|
|
return destination, true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.NextToken != nil {
|
|
|
|
return lookupCloudWatchLogDestination(conn, name, resp.NextToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false, nil
|
|
|
|
}
|