github.com/rhenning/terraform@v0.8.0-beta2/builtin/providers/aws/resource_aws_api_gateway_rest_api_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/service/apigateway"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSAPIGatewayRestApi_basic(t *testing.T) {
    14  	var conf apigateway.RestApi
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAWSAPIGatewayRestAPIDestroy,
    20  		Steps: []resource.TestStep{
    21  			{
    22  				Config: testAccAWSAPIGatewayRestAPIConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf),
    25  					testAccCheckAWSAPIGatewayRestAPINameAttribute(&conf, "bar"),
    26  					resource.TestCheckResourceAttr(
    27  						"aws_api_gateway_rest_api.test", "name", "bar"),
    28  					resource.TestCheckResourceAttr(
    29  						"aws_api_gateway_rest_api.test", "description", ""),
    30  					resource.TestCheckResourceAttrSet(
    31  						"aws_api_gateway_rest_api.test", "created_date"),
    32  				),
    33  			},
    34  
    35  			{
    36  				Config: testAccAWSAPIGatewayRestAPIUpdateConfig,
    37  				Check: resource.ComposeTestCheckFunc(
    38  					testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf),
    39  					testAccCheckAWSAPIGatewayRestAPINameAttribute(&conf, "test"),
    40  					testAccCheckAWSAPIGatewayRestAPIDescriptionAttribute(&conf, "test"),
    41  					resource.TestCheckResourceAttr(
    42  						"aws_api_gateway_rest_api.test", "name", "test"),
    43  					resource.TestCheckResourceAttr(
    44  						"aws_api_gateway_rest_api.test", "description", "test"),
    45  					resource.TestCheckResourceAttrSet(
    46  						"aws_api_gateway_rest_api.test", "created_date"),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func testAccCheckAWSAPIGatewayRestAPINameAttribute(conf *apigateway.RestApi, name string) resource.TestCheckFunc {
    54  	return func(s *terraform.State) error {
    55  		if *conf.Name != name {
    56  			return fmt.Errorf("Wrong Name: %q", *conf.Name)
    57  		}
    58  
    59  		return nil
    60  	}
    61  }
    62  
    63  func testAccCheckAWSAPIGatewayRestAPIDescriptionAttribute(conf *apigateway.RestApi, description string) resource.TestCheckFunc {
    64  	return func(s *terraform.State) error {
    65  		if *conf.Description != description {
    66  			return fmt.Errorf("Wrong Description: %q", *conf.Description)
    67  		}
    68  
    69  		return nil
    70  	}
    71  }
    72  
    73  func testAccCheckAWSAPIGatewayRestAPIExists(n string, res *apigateway.RestApi) 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 fmt.Errorf("No API Gateway ID is set")
    82  		}
    83  
    84  		conn := testAccProvider.Meta().(*AWSClient).apigateway
    85  
    86  		req := &apigateway.GetRestApiInput{
    87  			RestApiId: aws.String(rs.Primary.ID),
    88  		}
    89  		describe, err := conn.GetRestApi(req)
    90  		if err != nil {
    91  			return err
    92  		}
    93  
    94  		if *describe.Id != rs.Primary.ID {
    95  			return fmt.Errorf("APIGateway not found")
    96  		}
    97  
    98  		*res = *describe
    99  
   100  		return nil
   101  	}
   102  }
   103  
   104  func testAccCheckAWSAPIGatewayRestAPIDestroy(s *terraform.State) error {
   105  	conn := testAccProvider.Meta().(*AWSClient).apigateway
   106  
   107  	for _, rs := range s.RootModule().Resources {
   108  		if rs.Type != "aws_api_gateway_rest_api" {
   109  			continue
   110  		}
   111  
   112  		req := &apigateway.GetRestApisInput{}
   113  		describe, err := conn.GetRestApis(req)
   114  
   115  		if err == nil {
   116  			if len(describe.Items) != 0 &&
   117  				*describe.Items[0].Id == rs.Primary.ID {
   118  				return fmt.Errorf("API Gateway still exists")
   119  			}
   120  		}
   121  
   122  		return err
   123  	}
   124  
   125  	return nil
   126  }
   127  
   128  const testAccAWSAPIGatewayRestAPIConfig = `
   129  resource "aws_api_gateway_rest_api" "test" {
   130    name = "bar"
   131  }
   132  `
   133  
   134  const testAccAWSAPIGatewayRestAPIUpdateConfig = `
   135  resource "aws_api_gateway_rest_api" "test" {
   136    name = "test"
   137    description = "test"
   138  }
   139  `