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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
    10  )
    11  
    12  func resourceAwsCloudWatchLogDestinationPolicy() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceAwsCloudWatchLogDestinationPolicyPut,
    15  		Update: resourceAwsCloudWatchLogDestinationPolicyPut,
    16  		Read:   resourceAwsCloudWatchLogDestinationPolicyRead,
    17  		Delete: resourceAwsCloudWatchLogDestinationPolicyDelete,
    18  
    19  		Importer: &schema.ResourceImporter{
    20  			State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
    21  				d.Set("destination_name", d.Id())
    22  				return []*schema.ResourceData{d}, nil
    23  			},
    24  		},
    25  
    26  		Schema: map[string]*schema.Schema{
    27  			"destination_name": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Required: true,
    30  				ForceNew: true,
    31  			},
    32  
    33  			"access_policy": &schema.Schema{
    34  				Type:     schema.TypeString,
    35  				Required: true,
    36  			},
    37  		},
    38  	}
    39  }
    40  
    41  func resourceAwsCloudWatchLogDestinationPolicyPut(d *schema.ResourceData, meta interface{}) error {
    42  	conn := meta.(*AWSClient).cloudwatchlogsconn
    43  
    44  	destination_name := d.Get("destination_name").(string)
    45  	access_policy := d.Get("access_policy").(string)
    46  
    47  	params := &cloudwatchlogs.PutDestinationPolicyInput{
    48  		DestinationName: aws.String(destination_name),
    49  		AccessPolicy:    aws.String(access_policy),
    50  	}
    51  
    52  	_, err := conn.PutDestinationPolicy(params)
    53  
    54  	if err != nil {
    55  		return fmt.Errorf("Error creating DestinationPolicy with destination_name %s: %#v", destination_name, err)
    56  	}
    57  
    58  	d.SetId(destination_name)
    59  	return resourceAwsCloudWatchLogDestinationPolicyRead(d, meta)
    60  }
    61  
    62  func resourceAwsCloudWatchLogDestinationPolicyRead(d *schema.ResourceData, meta interface{}) error {
    63  	conn := meta.(*AWSClient).cloudwatchlogsconn
    64  	destination_name := d.Get("destination_name").(string)
    65  	destination, exists, err := lookupCloudWatchLogDestination(conn, destination_name, nil)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	if !exists {
    71  		d.SetId("")
    72  		return nil
    73  	}
    74  
    75  	if destination.AccessPolicy != nil {
    76  		d.SetId(destination_name)
    77  		d.Set("access_policy", *destination.AccessPolicy)
    78  	} else {
    79  		d.SetId("")
    80  	}
    81  
    82  	return nil
    83  }
    84  
    85  func resourceAwsCloudWatchLogDestinationPolicyDelete(d *schema.ResourceData, meta interface{}) error {
    86  	d.SetId("")
    87  	return nil
    88  }