github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_spot_datafeed_subscription.go (about)

     1  package aws
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/aws/aws-sdk-go/aws"
     7  	"github.com/aws/aws-sdk-go/aws/awserr"
     8  	"github.com/aws/aws-sdk-go/service/ec2"
     9  	"github.com/hashicorp/errwrap"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceAwsSpotDataFeedSubscription() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceAwsSpotDataFeedSubscriptionCreate,
    16  		Read:   resourceAwsSpotDataFeedSubscriptionRead,
    17  		Delete: resourceAwsSpotDataFeedSubscriptionDelete,
    18  		Importer: &schema.ResourceImporter{
    19  			State: schema.ImportStatePassthrough,
    20  		},
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"bucket": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  			"prefix": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Optional: true,
    31  				ForceNew: true,
    32  			},
    33  		},
    34  	}
    35  }
    36  
    37  func resourceAwsSpotDataFeedSubscriptionCreate(d *schema.ResourceData, meta interface{}) error {
    38  	conn := meta.(*AWSClient).ec2conn
    39  
    40  	params := &ec2.CreateSpotDatafeedSubscriptionInput{
    41  		Bucket: aws.String(d.Get("bucket").(string)),
    42  	}
    43  
    44  	if v, ok := d.GetOk("prefix"); ok {
    45  		params.Prefix = aws.String(v.(string))
    46  	}
    47  
    48  	log.Printf("[INFO] Creating Spot Datafeed Subscription")
    49  	_, err := conn.CreateSpotDatafeedSubscription(params)
    50  	if err != nil {
    51  		return errwrap.Wrapf("Error Creating Spot Datafeed Subscription: {{err}}", err)
    52  	}
    53  
    54  	d.SetId("spot-datafeed-subscription")
    55  
    56  	return resourceAwsSpotDataFeedSubscriptionRead(d, meta)
    57  }
    58  func resourceAwsSpotDataFeedSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
    59  	conn := meta.(*AWSClient).ec2conn
    60  
    61  	resp, err := conn.DescribeSpotDatafeedSubscription(&ec2.DescribeSpotDatafeedSubscriptionInput{})
    62  	if err != nil {
    63  		cgw, ok := err.(awserr.Error)
    64  		if ok && cgw.Code() == "InvalidSpotDatafeed.NotFound" {
    65  			log.Printf("[WARNING] Spot Datafeed Subscription Not Found so refreshing from state")
    66  			d.SetId("")
    67  			return nil
    68  		}
    69  		return errwrap.Wrapf("Error Describing Spot Datafeed Subscription: {{err}}", err)
    70  	}
    71  
    72  	if resp == nil {
    73  		log.Printf("[WARNING] Spot Datafeed Subscription Not Found so refreshing from state")
    74  		d.SetId("")
    75  		return nil
    76  	}
    77  
    78  	subscription := *resp.SpotDatafeedSubscription
    79  	d.Set("bucket", subscription.Bucket)
    80  	d.Set("prefix", subscription.Prefix)
    81  
    82  	return nil
    83  }
    84  func resourceAwsSpotDataFeedSubscriptionDelete(d *schema.ResourceData, meta interface{}) error {
    85  	conn := meta.(*AWSClient).ec2conn
    86  
    87  	log.Printf("[INFO] Deleting Spot Datafeed Subscription")
    88  	_, err := conn.DeleteSpotDatafeedSubscription(&ec2.DeleteSpotDatafeedSubscriptionInput{})
    89  	if err != nil {
    90  		return errwrap.Wrapf("Error deleting Spot Datafeed Subscription: {{err}}", err)
    91  	}
    92  	return nil
    93  }