github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  			resource.TestStep{
    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  				),
    31  			},
    32  
    33  			resource.TestStep{
    34  				Config: testAccAWSAPIGatewayRestAPIUpdateConfig,
    35  				Check: resource.ComposeTestCheckFunc(
    36  					testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf),
    37  					testAccCheckAWSAPIGatewayRestAPINameAttribute(&conf, "test"),
    38  					testAccCheckAWSAPIGatewayRestAPIDescriptionAttribute(&conf, "test"),
    39  					resource.TestCheckResourceAttr(
    40  						"aws_api_gateway_rest_api.test", "name", "test"),
    41  					resource.TestCheckResourceAttr(
    42  						"aws_api_gateway_rest_api.test", "description", "test"),
    43  				),
    44  			},
    45  		},
    46  	})
    47  }
    48  
    49  func testAccCheckAWSAPIGatewayRestAPINameAttribute(conf *apigateway.RestApi, name string) resource.TestCheckFunc {
    50  	return func(s *terraform.State) error {
    51  		if *conf.Name != name {
    52  			return fmt.Errorf("Wrong Name: %q", *conf.Name)
    53  		}
    54  
    55  		return nil
    56  	}
    57  }
    58  
    59  func testAccCheckAWSAPIGatewayRestAPIDescriptionAttribute(conf *apigateway.RestApi, description string) resource.TestCheckFunc {
    60  	return func(s *terraform.State) error {
    61  		if *conf.Description != description {
    62  			return fmt.Errorf("Wrong Description: %q", *conf.Description)
    63  		}
    64  
    65  		return nil
    66  	}
    67  }
    68  
    69  func testAccCheckAWSAPIGatewayRestAPIExists(n string, res *apigateway.RestApi) 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 fmt.Errorf("No API Gateway ID is set")
    78  		}
    79  
    80  		conn := testAccProvider.Meta().(*AWSClient).apigateway
    81  
    82  		req := &apigateway.GetRestApiInput{
    83  			RestApiId: aws.String(rs.Primary.ID),
    84  		}
    85  		describe, err := conn.GetRestApi(req)
    86  		if err != nil {
    87  			return err
    88  		}
    89  
    90  		if *describe.Id != rs.Primary.ID {
    91  			return fmt.Errorf("APIGateway not found")
    92  		}
    93  
    94  		*res = *describe
    95  
    96  		return nil
    97  	}
    98  }
    99  
   100  func testAccCheckAWSAPIGatewayRestAPIDestroy(s *terraform.State) error {
   101  	conn := testAccProvider.Meta().(*AWSClient).apigateway
   102  
   103  	for _, rs := range s.RootModule().Resources {
   104  		if rs.Type != "aws_api_gateway_rest_api" {
   105  			continue
   106  		}
   107  
   108  		req := &apigateway.GetRestApisInput{}
   109  		describe, err := conn.GetRestApis(req)
   110  
   111  		if err == nil {
   112  			if len(describe.Items) != 0 &&
   113  				*describe.Items[0].Id == rs.Primary.ID {
   114  				return fmt.Errorf("API Gateway still exists")
   115  			}
   116  		}
   117  
   118  		return err
   119  	}
   120  
   121  	return nil
   122  }
   123  
   124  const testAccAWSAPIGatewayRestAPIConfig = `
   125  resource "aws_api_gateway_rest_api" "test" {
   126    name = "bar"
   127  }
   128  `
   129  
   130  const testAccAWSAPIGatewayRestAPIUpdateConfig = `
   131  resource "aws_api_gateway_rest_api" "test" {
   132    name = "test"
   133    description = "test"
   134  }
   135  `