github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_lightsail_static_ip_attachment.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 resourceAwsLightsailStaticIpAttachment() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceAwsLightsailStaticIpAttachmentCreate,
    15  		Read:   resourceAwsLightsailStaticIpAttachmentRead,
    16  		Delete: resourceAwsLightsailStaticIpAttachmentDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"static_ip_name": {
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  			"instance_name": {
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  				ForceNew: true,
    28  			},
    29  		},
    30  	}
    31  }
    32  
    33  func resourceAwsLightsailStaticIpAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
    34  	conn := meta.(*AWSClient).lightsailconn
    35  
    36  	staticIpName := d.Get("static_ip_name").(string)
    37  	log.Printf("[INFO] Attaching Lightsail Static IP: %q", staticIpName)
    38  	out, err := conn.AttachStaticIp(&lightsail.AttachStaticIpInput{
    39  		StaticIpName: aws.String(staticIpName),
    40  		InstanceName: aws.String(d.Get("instance_name").(string)),
    41  	})
    42  	if err != nil {
    43  		return err
    44  	}
    45  	log.Printf("[INFO] Lightsail Static IP attached: %s", *out)
    46  
    47  	d.SetId(staticIpName)
    48  
    49  	return resourceAwsLightsailStaticIpAttachmentRead(d, meta)
    50  }
    51  
    52  func resourceAwsLightsailStaticIpAttachmentRead(d *schema.ResourceData, meta interface{}) error {
    53  	conn := meta.(*AWSClient).lightsailconn
    54  
    55  	staticIpName := d.Get("static_ip_name").(string)
    56  	log.Printf("[INFO] Reading Lightsail Static IP: %q", staticIpName)
    57  	out, err := conn.GetStaticIp(&lightsail.GetStaticIpInput{
    58  		StaticIpName: aws.String(staticIpName),
    59  	})
    60  	if err != nil {
    61  		if awsErr, ok := err.(awserr.Error); ok {
    62  			if awsErr.Code() == "NotFoundException" {
    63  				log.Printf("[WARN] Lightsail Static IP (%s) not found, removing from state", d.Id())
    64  				d.SetId("")
    65  				return nil
    66  			}
    67  		}
    68  		return err
    69  	}
    70  	if !*out.StaticIp.IsAttached {
    71  		log.Printf("[WARN] Lightsail Static IP (%s) is not attached, removing from state", d.Id())
    72  		d.SetId("")
    73  		return nil
    74  	}
    75  
    76  	log.Printf("[INFO] Received Lightsail Static IP: %s", *out)
    77  
    78  	d.Set("instance_name", out.StaticIp.AttachedTo)
    79  
    80  	return nil
    81  }
    82  
    83  func resourceAwsLightsailStaticIpAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
    84  	conn := meta.(*AWSClient).lightsailconn
    85  
    86  	name := d.Get("static_ip_name").(string)
    87  	log.Printf("[INFO] Detaching Lightsail Static IP: %q", name)
    88  	out, err := conn.DetachStaticIp(&lightsail.DetachStaticIpInput{
    89  		StaticIpName: aws.String(name),
    90  	})
    91  	if err != nil {
    92  		return err
    93  	}
    94  	log.Printf("[INFO] Detached Lightsail Static IP: %s", *out)
    95  	return nil
    96  }