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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  
    11  	"github.com/aws/aws-sdk-go/aws"
    12  	"github.com/aws/aws-sdk-go/aws/awserr"
    13  	"github.com/aws/aws-sdk-go/service/configservice"
    14  )
    15  
    16  func resourceAwsConfigDeliveryChannel() *schema.Resource {
    17  	return &schema.Resource{
    18  		Create: resourceAwsConfigDeliveryChannelPut,
    19  		Read:   resourceAwsConfigDeliveryChannelRead,
    20  		Update: resourceAwsConfigDeliveryChannelPut,
    21  		Delete: resourceAwsConfigDeliveryChannelDelete,
    22  
    23  		Importer: &schema.ResourceImporter{
    24  			State: schema.ImportStatePassthrough,
    25  		},
    26  
    27  		Schema: map[string]*schema.Schema{
    28  			"name": {
    29  				Type:         schema.TypeString,
    30  				Optional:     true,
    31  				Default:      "default",
    32  				ValidateFunc: validateMaxLength(256),
    33  			},
    34  			"s3_bucket_name": {
    35  				Type:     schema.TypeString,
    36  				Required: true,
    37  			},
    38  			"s3_key_prefix": {
    39  				Type:     schema.TypeString,
    40  				Optional: true,
    41  			},
    42  			"sns_topic_arn": {
    43  				Type:         schema.TypeString,
    44  				Optional:     true,
    45  				ValidateFunc: validateArn,
    46  			},
    47  			"snapshot_delivery_properties": {
    48  				Type:     schema.TypeList,
    49  				Optional: true,
    50  				MaxItems: 1,
    51  				Elem: &schema.Resource{
    52  					Schema: map[string]*schema.Schema{
    53  						"delivery_frequency": {
    54  							Type:         schema.TypeString,
    55  							Optional:     true,
    56  							ValidateFunc: validateConfigExecutionFrequency,
    57  						},
    58  					},
    59  				},
    60  			},
    61  		},
    62  	}
    63  }
    64  
    65  func resourceAwsConfigDeliveryChannelPut(d *schema.ResourceData, meta interface{}) error {
    66  	conn := meta.(*AWSClient).configconn
    67  
    68  	name := d.Get("name").(string)
    69  	channel := configservice.DeliveryChannel{
    70  		Name:         aws.String(name),
    71  		S3BucketName: aws.String(d.Get("s3_bucket_name").(string)),
    72  	}
    73  
    74  	if v, ok := d.GetOk("s3_key_prefix"); ok {
    75  		channel.S3KeyPrefix = aws.String(v.(string))
    76  	}
    77  	if v, ok := d.GetOk("sns_topic_arn"); ok {
    78  		channel.SnsTopicARN = aws.String(v.(string))
    79  	}
    80  
    81  	if p, ok := d.GetOk("snapshot_delivery_properties"); ok {
    82  		propertiesBlocks := p.([]interface{})
    83  		block := propertiesBlocks[0].(map[string]interface{})
    84  
    85  		if v, ok := block["delivery_frequency"]; ok {
    86  			channel.ConfigSnapshotDeliveryProperties = &configservice.ConfigSnapshotDeliveryProperties{
    87  				DeliveryFrequency: aws.String(v.(string)),
    88  			}
    89  		}
    90  	}
    91  
    92  	input := configservice.PutDeliveryChannelInput{DeliveryChannel: &channel}
    93  
    94  	err := resource.Retry(2*time.Minute, func() *resource.RetryError {
    95  		_, err := conn.PutDeliveryChannel(&input)
    96  		if err == nil {
    97  			return nil
    98  		}
    99  
   100  		awsErr, ok := err.(awserr.Error)
   101  		if ok && awsErr.Code() == "InsufficientDeliveryPolicyException" {
   102  			return resource.RetryableError(err)
   103  		}
   104  
   105  		return resource.NonRetryableError(err)
   106  	})
   107  	if err != nil {
   108  		return fmt.Errorf("Creating Delivery Channel failed: %s", err)
   109  	}
   110  
   111  	d.SetId(name)
   112  
   113  	return resourceAwsConfigDeliveryChannelRead(d, meta)
   114  }
   115  
   116  func resourceAwsConfigDeliveryChannelRead(d *schema.ResourceData, meta interface{}) error {
   117  	conn := meta.(*AWSClient).configconn
   118  
   119  	input := configservice.DescribeDeliveryChannelsInput{
   120  		DeliveryChannelNames: []*string{aws.String(d.Id())},
   121  	}
   122  	out, err := conn.DescribeDeliveryChannels(&input)
   123  	if err != nil {
   124  		if awsErr, ok := err.(awserr.Error); ok {
   125  			if awsErr.Code() == "NoSuchDeliveryChannelException" {
   126  				log.Printf("[WARN] Delivery Channel %q is gone (NoSuchDeliveryChannelException)", d.Id())
   127  				d.SetId("")
   128  				return nil
   129  			}
   130  		}
   131  		return fmt.Errorf("Getting Delivery Channel failed: %s", err)
   132  	}
   133  
   134  	if len(out.DeliveryChannels) < 1 {
   135  		log.Printf("[WARN] Delivery Channel %q is gone (no channels found)", d.Id())
   136  		d.SetId("")
   137  		return nil
   138  	}
   139  
   140  	if len(out.DeliveryChannels) > 1 {
   141  		return fmt.Errorf("Received %d delivery channels under %s (expected exactly 1): %s",
   142  			len(out.DeliveryChannels), d.Id(), out.DeliveryChannels)
   143  	}
   144  
   145  	channel := out.DeliveryChannels[0]
   146  
   147  	d.Set("name", channel.Name)
   148  	d.Set("s3_bucket_name", channel.S3BucketName)
   149  	d.Set("s3_key_prefix", channel.S3KeyPrefix)
   150  	d.Set("sns_topic_arn", channel.SnsTopicARN)
   151  
   152  	if channel.ConfigSnapshotDeliveryProperties != nil {
   153  		d.Set("snapshot_delivery_properties", flattenConfigSnapshotDeliveryProperties(channel.ConfigSnapshotDeliveryProperties))
   154  	}
   155  
   156  	return nil
   157  }
   158  
   159  func resourceAwsConfigDeliveryChannelDelete(d *schema.ResourceData, meta interface{}) error {
   160  	conn := meta.(*AWSClient).configconn
   161  	input := configservice.DeleteDeliveryChannelInput{
   162  		DeliveryChannelName: aws.String(d.Id()),
   163  	}
   164  	_, err := conn.DeleteDeliveryChannel(&input)
   165  	if err != nil {
   166  		return fmt.Errorf("Unable to delete delivery channel: %s", err)
   167  	}
   168  
   169  	d.SetId("")
   170  	return nil
   171  }