github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_elastic_beanstalk_application_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSBeanstalkApp_basic(t *testing.T) {
    15  	var app elasticbeanstalk.ApplicationDescription
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckBeanstalkAppDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccBeanstalkAppConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckBeanstalkAppExists("aws_elastic_beanstalk_application.tftest", &app),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func testAccCheckBeanstalkAppDestroy(s *terraform.State) error {
    33  	conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn
    34  
    35  	for _, rs := range s.RootModule().Resources {
    36  		if rs.Type != "aws_elastic_beanstalk_application" {
    37  			continue
    38  		}
    39  
    40  		// Try to find the application
    41  		DescribeBeanstalkAppOpts := &elasticbeanstalk.DescribeApplicationsInput{
    42  			ApplicationNames: []*string{aws.String(rs.Primary.ID)},
    43  		}
    44  		resp, err := conn.DescribeApplications(DescribeBeanstalkAppOpts)
    45  		if err == nil {
    46  			if len(resp.Applications) > 0 {
    47  				return fmt.Errorf("Elastic Beanstalk Application still exists.")
    48  			}
    49  
    50  			return nil
    51  		}
    52  
    53  		// Verify the error is what we want
    54  		ec2err, ok := err.(awserr.Error)
    55  		if !ok {
    56  			return err
    57  		}
    58  		if ec2err.Code() != "InvalidBeanstalkAppID.NotFound" {
    59  			return err
    60  		}
    61  	}
    62  
    63  	return nil
    64  }
    65  
    66  func testAccCheckBeanstalkAppExists(n string, app *elasticbeanstalk.ApplicationDescription) resource.TestCheckFunc {
    67  	return func(s *terraform.State) error {
    68  		rs, ok := s.RootModule().Resources[n]
    69  		if !ok {
    70  			return fmt.Errorf("Not found: %s", n)
    71  		}
    72  
    73  		if rs.Primary.ID == "" {
    74  			return fmt.Errorf("Elastic Beanstalk app ID is not set")
    75  		}
    76  
    77  		conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn
    78  		DescribeBeanstalkAppOpts := &elasticbeanstalk.DescribeApplicationsInput{
    79  			ApplicationNames: []*string{aws.String(rs.Primary.ID)},
    80  		}
    81  		resp, err := conn.DescribeApplications(DescribeBeanstalkAppOpts)
    82  		if err != nil {
    83  			return err
    84  		}
    85  		if len(resp.Applications) == 0 {
    86  			return fmt.Errorf("Elastic Beanstalk Application not found.")
    87  		}
    88  
    89  		*app = *resp.Applications[0]
    90  
    91  		return nil
    92  	}
    93  }
    94  
    95  const testAccBeanstalkAppConfig = `
    96  resource "aws_elastic_beanstalk_application" "tftest" {
    97    name = "tf-test-name"
    98    description = "tf-test-desc"
    99  }
   100  `