github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_api_gateway_deployment_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/apigateway"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSAPIGatewayDeployment_basic(t *testing.T) {
    15  	var conf apigateway.Deployment
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSAPIGatewayDeploymentDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAWSAPIGatewayDeploymentConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckAWSAPIGatewayDeploymentExists("aws_api_gateway_deployment.test", &conf),
    26  					resource.TestCheckResourceAttr(
    27  						"aws_api_gateway_deployment.test", "stage_name", "test"),
    28  					resource.TestCheckResourceAttr(
    29  						"aws_api_gateway_deployment.test", "description", "This is a test"),
    30  					resource.TestCheckResourceAttr(
    31  						"aws_api_gateway_deployment.test", "variables.a", "2"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func testAccCheckAWSAPIGatewayDeploymentExists(n string, res *apigateway.Deployment) resource.TestCheckFunc {
    39  	return func(s *terraform.State) error {
    40  		rs, ok := s.RootModule().Resources[n]
    41  		if !ok {
    42  			return fmt.Errorf("Not found: %s", n)
    43  		}
    44  
    45  		if rs.Primary.ID == "" {
    46  			return fmt.Errorf("No API Gateway Deployment ID is set")
    47  		}
    48  
    49  		conn := testAccProvider.Meta().(*AWSClient).apigateway
    50  
    51  		req := &apigateway.GetDeploymentInput{
    52  			DeploymentId: aws.String(rs.Primary.ID),
    53  			RestApiId:    aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
    54  		}
    55  		describe, err := conn.GetDeployment(req)
    56  		if err != nil {
    57  			return err
    58  		}
    59  
    60  		if *describe.Id != rs.Primary.ID {
    61  			return fmt.Errorf("APIGateway Deployment not found")
    62  		}
    63  
    64  		*res = *describe
    65  
    66  		return nil
    67  	}
    68  }
    69  
    70  func testAccCheckAWSAPIGatewayDeploymentDestroy(s *terraform.State) error {
    71  	conn := testAccProvider.Meta().(*AWSClient).apigateway
    72  
    73  	for _, rs := range s.RootModule().Resources {
    74  		if rs.Type != "aws_api_gateway_resource" {
    75  			continue
    76  		}
    77  
    78  		req := &apigateway.GetDeploymentsInput{
    79  			RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
    80  		}
    81  		describe, err := conn.GetDeployments(req)
    82  
    83  		if err == nil {
    84  			if len(describe.Items) != 0 &&
    85  				*describe.Items[0].Id == rs.Primary.ID {
    86  				return fmt.Errorf("API Gateway Deployment still exists")
    87  			}
    88  		}
    89  
    90  		aws2err, ok := err.(awserr.Error)
    91  		if !ok {
    92  			return err
    93  		}
    94  		if aws2err.Code() != "NotFoundException" {
    95  			return err
    96  		}
    97  
    98  		return nil
    99  	}
   100  
   101  	return nil
   102  }
   103  
   104  const testAccAWSAPIGatewayDeploymentConfig = `
   105  resource "aws_api_gateway_rest_api" "test" {
   106    name = "test"
   107  }
   108  
   109  resource "aws_api_gateway_resource" "test" {
   110    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   111    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   112    path_part = "test"
   113  }
   114  
   115  resource "aws_api_gateway_method" "test" {
   116    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   117    resource_id = "${aws_api_gateway_resource.test.id}"
   118    http_method = "GET"
   119    authorization = "NONE"
   120  }
   121  
   122  resource "aws_api_gateway_method_response" "error" {
   123    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   124    resource_id = "${aws_api_gateway_resource.test.id}"
   125    http_method = "${aws_api_gateway_method.test.http_method}"
   126    status_code = "400"
   127  }
   128  
   129  resource "aws_api_gateway_integration" "test" {
   130    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   131    resource_id = "${aws_api_gateway_resource.test.id}"
   132    http_method = "${aws_api_gateway_method.test.http_method}"
   133  
   134    type = "HTTP"
   135    uri = "https://www.google.de"
   136    integration_http_method = "GET"
   137  }
   138  
   139  resource "aws_api_gateway_integration_response" "test" {
   140    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   141    resource_id = "${aws_api_gateway_resource.test.id}"
   142    http_method = "${aws_api_gateway_integration.test.http_method}"
   143    status_code = "${aws_api_gateway_method_response.error.status_code}"
   144  }
   145  
   146  resource "aws_api_gateway_deployment" "test" {
   147    depends_on = ["aws_api_gateway_integration.test"]
   148  
   149    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   150    stage_name = "test"
   151    description = "This is a test"
   152  
   153    variables = {
   154      "a" = "2"
   155    }
   156  }
   157  `