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