github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_cloudwatch_log_destination.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsCloudWatchLogDestination() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsCloudWatchLogDestinationPut,
    18  		Update: resourceAwsCloudWatchLogDestinationPut,
    19  		Read:   resourceAwsCloudWatchLogDestinationRead,
    20  		Delete: resourceAwsCloudWatchLogDestinationDelete,
    21  
    22  		Importer: &schema.ResourceImporter{
    23  			State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
    24  				d.Set("name", d.Id())
    25  				return []*schema.ResourceData{d}, nil
    26  			},
    27  		},
    28  
    29  		Schema: map[string]*schema.Schema{
    30  			"name": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  				ForceNew: true,
    34  			},
    35  
    36  			"role_arn": &schema.Schema{
    37  				Type:     schema.TypeString,
    38  				Required: true,
    39  			},
    40  
    41  			"target_arn": &schema.Schema{
    42  				Type:     schema.TypeString,
    43  				Required: true,
    44  			},
    45  
    46  			"arn": &schema.Schema{
    47  				Type:     schema.TypeString,
    48  				Computed: true,
    49  			},
    50  		},
    51  	}
    52  }
    53  
    54  func resourceAwsCloudWatchLogDestinationPut(d *schema.ResourceData, meta interface{}) error {
    55  	conn := meta.(*AWSClient).cloudwatchlogsconn
    56  
    57  	name := d.Get("name").(string)
    58  	role_arn := d.Get("role_arn").(string)
    59  	target_arn := d.Get("target_arn").(string)
    60  
    61  	params := &cloudwatchlogs.PutDestinationInput{
    62  		DestinationName: aws.String(name),
    63  		RoleArn:         aws.String(role_arn),
    64  		TargetArn:       aws.String(target_arn),
    65  	}
    66  
    67  	return resource.Retry(3*time.Minute, func() *resource.RetryError {
    68  		resp, err := conn.PutDestination(params)
    69  
    70  		if err == nil {
    71  			d.SetId(name)
    72  			d.Set("arn", *resp.Destination.Arn)
    73  		}
    74  
    75  		awsErr, ok := err.(awserr.Error)
    76  		if !ok {
    77  			return resource.RetryableError(err)
    78  		}
    79  
    80  		if awsErr.Code() == "InvalidParameterException" {
    81  			if strings.Contains(awsErr.Message(), "Could not deliver test message to specified") {
    82  				return resource.RetryableError(err)
    83  			}
    84  			return resource.NonRetryableError(err)
    85  		}
    86  
    87  		return resource.NonRetryableError(err)
    88  	})
    89  }
    90  
    91  func resourceAwsCloudWatchLogDestinationRead(d *schema.ResourceData, meta interface{}) error {
    92  	conn := meta.(*AWSClient).cloudwatchlogsconn
    93  	name := d.Get("name").(string)
    94  	destination, exists, err := lookupCloudWatchLogDestination(conn, name, nil)
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	if !exists {
   100  		d.SetId("")
   101  		return nil
   102  	}
   103  
   104  	d.SetId(name)
   105  	d.Set("arn", destination.Arn)
   106  	d.Set("role_arn", destination.RoleArn)
   107  	d.Set("target_arn", destination.TargetArn)
   108  
   109  	return nil
   110  }
   111  
   112  func resourceAwsCloudWatchLogDestinationDelete(d *schema.ResourceData, meta interface{}) error {
   113  	conn := meta.(*AWSClient).cloudwatchlogsconn
   114  
   115  	name := d.Get("name").(string)
   116  
   117  	params := &cloudwatchlogs.DeleteDestinationInput{
   118  		DestinationName: aws.String(name),
   119  	}
   120  	_, err := conn.DeleteDestination(params)
   121  	if err != nil {
   122  		return fmt.Errorf("Error deleting Destination with name %s", name)
   123  	}
   124  	d.SetId("")
   125  	return nil
   126  }
   127  
   128  func lookupCloudWatchLogDestination(conn *cloudwatchlogs.CloudWatchLogs,
   129  	name string, nextToken *string) (*cloudwatchlogs.Destination, bool, error) {
   130  	input := &cloudwatchlogs.DescribeDestinationsInput{
   131  		DestinationNamePrefix: aws.String(name),
   132  		NextToken:             nextToken,
   133  	}
   134  	resp, err := conn.DescribeDestinations(input)
   135  	if err != nil {
   136  		return nil, true, err
   137  	}
   138  
   139  	for _, destination := range resp.Destinations {
   140  		if *destination.DestinationName == name {
   141  			return destination, true, nil
   142  		}
   143  	}
   144  
   145  	if resp.NextToken != nil {
   146  		return lookupCloudWatchLogDestination(conn, name, resp.NextToken)
   147  	}
   148  
   149  	return nil, false, nil
   150  }