github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/resource_aws_lightsail_static_ip_attachment_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/lightsail"
    11  	"github.com/hashicorp/terraform/helper/acctest"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  func TestAccAWSLightsailStaticIpAttachment_basic(t *testing.T) {
    17  	var staticIp lightsail.StaticIp
    18  	staticIpName := fmt.Sprintf("tf-test-lightsail-%s", acctest.RandString(5))
    19  	instanceName := fmt.Sprintf("tf-test-lightsail-%s", acctest.RandString(5))
    20  	keypairName := fmt.Sprintf("tf-test-lightsail-%s", acctest.RandString(5))
    21  
    22  	resource.Test(t, resource.TestCase{
    23  		PreCheck:     func() { testAccPreCheck(t) },
    24  		Providers:    testAccProviders,
    25  		CheckDestroy: testAccCheckAWSLightsailStaticIpAttachmentDestroy,
    26  		Steps: []resource.TestStep{
    27  			{
    28  				Config: testAccAWSLightsailStaticIpAttachmentConfig_basic(staticIpName, instanceName, keypairName),
    29  				Check: resource.ComposeAggregateTestCheckFunc(
    30  					testAccCheckAWSLightsailStaticIpAttachmentExists("aws_lightsail_static_ip_attachment.test", &staticIp),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func TestAccAWSLightsailStaticIpAttachment_disappears(t *testing.T) {
    38  	var staticIp lightsail.StaticIp
    39  	staticIpName := fmt.Sprintf("tf-test-lightsail-%s", acctest.RandString(5))
    40  	instanceName := fmt.Sprintf("tf-test-lightsail-%s", acctest.RandString(5))
    41  	keypairName := fmt.Sprintf("tf-test-lightsail-%s", acctest.RandString(5))
    42  
    43  	staticIpDestroy := func(*terraform.State) error {
    44  		conn := testAccProvider.Meta().(*AWSClient).lightsailconn
    45  		_, err := conn.DetachStaticIp(&lightsail.DetachStaticIpInput{
    46  			StaticIpName: aws.String(staticIpName),
    47  		})
    48  
    49  		if err != nil {
    50  			return fmt.Errorf("Error deleting Lightsail Static IP in disappear test")
    51  		}
    52  
    53  		return nil
    54  	}
    55  
    56  	resource.Test(t, resource.TestCase{
    57  		PreCheck:     func() { testAccPreCheck(t) },
    58  		Providers:    testAccProviders,
    59  		CheckDestroy: testAccCheckAWSLightsailStaticIpAttachmentDestroy,
    60  		Steps: []resource.TestStep{
    61  			{
    62  				Config: testAccAWSLightsailStaticIpAttachmentConfig_basic(staticIpName, instanceName, keypairName),
    63  				Check: resource.ComposeAggregateTestCheckFunc(
    64  					testAccCheckAWSLightsailStaticIpAttachmentExists("aws_lightsail_static_ip_attachment.test", &staticIp),
    65  					staticIpDestroy,
    66  				),
    67  				ExpectNonEmptyPlan: true,
    68  			},
    69  		},
    70  	})
    71  }
    72  
    73  func testAccCheckAWSLightsailStaticIpAttachmentExists(n string, staticIp *lightsail.StaticIp) resource.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  		rs, ok := s.RootModule().Resources[n]
    76  		if !ok {
    77  			return fmt.Errorf("Not found: %s", n)
    78  		}
    79  
    80  		if rs.Primary.ID == "" {
    81  			return errors.New("No Lightsail Static IP Attachment ID is set")
    82  		}
    83  
    84  		conn := testAccProvider.Meta().(*AWSClient).lightsailconn
    85  
    86  		resp, err := conn.GetStaticIp(&lightsail.GetStaticIpInput{
    87  			StaticIpName: aws.String(rs.Primary.ID),
    88  		})
    89  		if err != nil {
    90  			return err
    91  		}
    92  
    93  		if resp == nil || resp.StaticIp == nil {
    94  			return fmt.Errorf("Static IP (%s) not found", rs.Primary.ID)
    95  		}
    96  
    97  		if !*resp.StaticIp.IsAttached {
    98  			return fmt.Errorf("Static IP (%s) not attached", rs.Primary.ID)
    99  		}
   100  
   101  		*staticIp = *resp.StaticIp
   102  		return nil
   103  	}
   104  }
   105  
   106  func testAccCheckAWSLightsailStaticIpAttachmentDestroy(s *terraform.State) error {
   107  	for _, rs := range s.RootModule().Resources {
   108  		if rs.Type != "aws_lightsail_static_ip_attachment" {
   109  			continue
   110  		}
   111  
   112  		conn := testAccProvider.Meta().(*AWSClient).lightsailconn
   113  
   114  		resp, err := conn.GetStaticIp(&lightsail.GetStaticIpInput{
   115  			StaticIpName: aws.String(rs.Primary.ID),
   116  		})
   117  
   118  		if err == nil {
   119  			if *resp.StaticIp.IsAttached {
   120  				return fmt.Errorf("Lightsail Static IP %q is still attached (to %q)", rs.Primary.ID, *resp.StaticIp.AttachedTo)
   121  			}
   122  		}
   123  
   124  		// Verify the error
   125  		if awsErr, ok := err.(awserr.Error); ok {
   126  			if awsErr.Code() == "NotFoundException" {
   127  				return nil
   128  			}
   129  		}
   130  		return err
   131  	}
   132  
   133  	return nil
   134  }
   135  
   136  func testAccAWSLightsailStaticIpAttachmentConfig_basic(staticIpName, instanceName, keypairName string) string {
   137  	return fmt.Sprintf(`
   138  provider "aws" {
   139    region = "us-east-1"
   140  }
   141  
   142  resource "aws_lightsail_static_ip_attachment" "test" {
   143    static_ip_name = "${aws_lightsail_static_ip.test.name}"
   144    instance_name = "${aws_lightsail_instance.test.name}"
   145  }
   146  
   147  resource "aws_lightsail_static_ip" "test" {
   148    name = "%s"
   149  }
   150  
   151  resource "aws_lightsail_instance" "test" {
   152    name              = "%s"
   153    availability_zone = "us-east-1b"
   154    blueprint_id      = "wordpress_4_6_1"
   155    bundle_id         = "micro_1_0"
   156    key_pair_name     = "${aws_lightsail_key_pair.test.name}"
   157  }
   158  
   159  resource "aws_lightsail_key_pair" "test" {
   160    name = "%s"
   161  }
   162  `, staticIpName, instanceName, keypairName)
   163  }