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