github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_lightsail_static_ip.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/lightsail"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceAwsLightsailStaticIp() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceAwsLightsailStaticIpCreate,
    15  		Read:   resourceAwsLightsailStaticIpRead,
    16  		Delete: resourceAwsLightsailStaticIpDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"name": {
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  			"ip_address": {
    25  				Type:     schema.TypeString,
    26  				Computed: true,
    27  			},
    28  			"arn": {
    29  				Type:     schema.TypeString,
    30  				Computed: true,
    31  			},
    32  			"support_code": {
    33  				Type:     schema.TypeString,
    34  				Computed: true,
    35  			},
    36  		},
    37  	}
    38  }
    39  
    40  func resourceAwsLightsailStaticIpCreate(d *schema.ResourceData, meta interface{}) error {
    41  	conn := meta.(*AWSClient).lightsailconn
    42  
    43  	name := d.Get("name").(string)
    44  	log.Printf("[INFO] Allocating Lightsail Static IP: %q", name)
    45  	out, err := conn.AllocateStaticIp(&lightsail.AllocateStaticIpInput{
    46  		StaticIpName: aws.String(name),
    47  	})
    48  	if err != nil {
    49  		return err
    50  	}
    51  	log.Printf("[INFO] Lightsail Static IP allocated: %s", *out)
    52  
    53  	d.SetId(name)
    54  
    55  	return resourceAwsLightsailStaticIpRead(d, meta)
    56  }
    57  
    58  func resourceAwsLightsailStaticIpRead(d *schema.ResourceData, meta interface{}) error {
    59  	conn := meta.(*AWSClient).lightsailconn
    60  
    61  	name := d.Get("name").(string)
    62  	log.Printf("[INFO] Reading Lightsail Static IP: %q", name)
    63  	out, err := conn.GetStaticIp(&lightsail.GetStaticIpInput{
    64  		StaticIpName: aws.String(name),
    65  	})
    66  	if err != nil {
    67  		if awsErr, ok := err.(awserr.Error); ok {
    68  			if awsErr.Code() == "NotFoundException" {
    69  				log.Printf("[WARN] Lightsail Static IP (%s) not found, removing from state", d.Id())
    70  				d.SetId("")
    71  				return nil
    72  			}
    73  		}
    74  		return err
    75  	}
    76  	log.Printf("[INFO] Received Lightsail Static IP: %s", *out)
    77  
    78  	d.Set("arn", out.StaticIp.Arn)
    79  	d.Set("ip_address", out.StaticIp.IpAddress)
    80  	d.Set("support_code", out.StaticIp.SupportCode)
    81  
    82  	return nil
    83  }
    84  
    85  func resourceAwsLightsailStaticIpDelete(d *schema.ResourceData, meta interface{}) error {
    86  	conn := meta.(*AWSClient).lightsailconn
    87  
    88  	name := d.Get("name").(string)
    89  	log.Printf("[INFO] Deleting Lightsail Static IP: %q", name)
    90  	out, err := conn.ReleaseStaticIp(&lightsail.ReleaseStaticIpInput{
    91  		StaticIpName: aws.String(name),
    92  	})
    93  	if err != nil {
    94  		return err
    95  	}
    96  	log.Printf("[INFO] Deleted Lightsail Static IP: %s", *out)
    97  	return nil
    98  }