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