github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_lightsail_static_ip_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 TestAccAWSLightsailStaticIp_basic(t *testing.T) {
    17  	var staticIp lightsail.StaticIp
    18  	staticIpName := fmt.Sprintf("tf-test-lightsail-%s", acctest.RandString(5))
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckAWSLightsailStaticIpDestroy,
    24  		Steps: []resource.TestStep{
    25  			{
    26  				Config: testAccAWSLightsailStaticIpConfig_basic(staticIpName),
    27  				Check: resource.ComposeAggregateTestCheckFunc(
    28  					testAccCheckAWSLightsailStaticIpExists("aws_lightsail_static_ip.test", &staticIp),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func TestAccAWSLightsailStaticIp_disappears(t *testing.T) {
    36  	var staticIp lightsail.StaticIp
    37  	staticIpName := fmt.Sprintf("tf-test-lightsail-%s", acctest.RandString(5))
    38  
    39  	staticIpDestroy := func(*terraform.State) error {
    40  		conn := testAccProvider.Meta().(*AWSClient).lightsailconn
    41  		_, err := conn.ReleaseStaticIp(&lightsail.ReleaseStaticIpInput{
    42  			StaticIpName: aws.String(staticIpName),
    43  		})
    44  
    45  		if err != nil {
    46  			return fmt.Errorf("Error deleting Lightsail Static IP in disapear test")
    47  		}
    48  
    49  		return nil
    50  	}
    51  
    52  	resource.Test(t, resource.TestCase{
    53  		PreCheck:     func() { testAccPreCheck(t) },
    54  		Providers:    testAccProviders,
    55  		CheckDestroy: testAccCheckAWSLightsailStaticIpDestroy,
    56  		Steps: []resource.TestStep{
    57  			{
    58  				Config: testAccAWSLightsailStaticIpConfig_basic(staticIpName),
    59  				Check: resource.ComposeAggregateTestCheckFunc(
    60  					testAccCheckAWSLightsailStaticIpExists("aws_lightsail_static_ip.test", &staticIp),
    61  					staticIpDestroy,
    62  				),
    63  				ExpectNonEmptyPlan: true,
    64  			},
    65  		},
    66  	})
    67  }
    68  
    69  func testAccCheckAWSLightsailStaticIpExists(n string, staticIp *lightsail.StaticIp) resource.TestCheckFunc {
    70  	return func(s *terraform.State) error {
    71  		rs, ok := s.RootModule().Resources[n]
    72  		if !ok {
    73  			return fmt.Errorf("Not found: %s", n)
    74  		}
    75  
    76  		if rs.Primary.ID == "" {
    77  			return errors.New("No Lightsail Static IP ID is set")
    78  		}
    79  
    80  		conn := testAccProvider.Meta().(*AWSClient).lightsailconn
    81  
    82  		resp, err := conn.GetStaticIp(&lightsail.GetStaticIpInput{
    83  			StaticIpName: aws.String(rs.Primary.ID),
    84  		})
    85  
    86  		if err != nil {
    87  			return err
    88  		}
    89  
    90  		if resp == nil || resp.StaticIp == nil {
    91  			return fmt.Errorf("Static IP (%s) not found", rs.Primary.ID)
    92  		}
    93  		*staticIp = *resp.StaticIp
    94  		return nil
    95  	}
    96  }
    97  
    98  func testAccCheckAWSLightsailStaticIpDestroy(s *terraform.State) error {
    99  
   100  	for _, rs := range s.RootModule().Resources {
   101  		if rs.Type != "aws_lightsail_static_ip" {
   102  			continue
   103  		}
   104  
   105  		conn := testAccProvider.Meta().(*AWSClient).lightsailconn
   106  
   107  		resp, err := conn.GetStaticIp(&lightsail.GetStaticIpInput{
   108  			StaticIpName: aws.String(rs.Primary.ID),
   109  		})
   110  
   111  		if err == nil {
   112  			if resp.StaticIp != nil {
   113  				return fmt.Errorf("Lightsail Static IP %q still exists", rs.Primary.ID)
   114  			}
   115  		}
   116  
   117  		// Verify the error
   118  		if awsErr, ok := err.(awserr.Error); ok {
   119  			if awsErr.Code() == "NotFoundException" {
   120  				return nil
   121  			}
   122  		}
   123  		return err
   124  	}
   125  
   126  	return nil
   127  }
   128  
   129  func testAccAWSLightsailStaticIpConfig_basic(staticIpName string) string {
   130  	return fmt.Sprintf(`
   131  provider "aws" {
   132    region = "us-east-1"
   133  }
   134  resource "aws_lightsail_static_ip" "test" {
   135    name = "%s"
   136  }
   137  `, staticIpName)
   138  }