github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_api_gateway_api_key_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/apigateway"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSAPIGatewayApiKey_basic(t *testing.T) {
    16  	var conf apigateway.ApiKey
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckAWSAPIGatewayApiKeyDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccAWSAPIGatewayApiKeyConfig,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckAWSAPIGatewayApiKeyExists("aws_api_gateway_api_key.test", &conf),
    27  					testAccCheckAWSAPIGatewayApiKeyStageKeyAttribute(&conf),
    28  					resource.TestCheckResourceAttr(
    29  						"aws_api_gateway_api_key.test", "name", "foo"),
    30  					resource.TestCheckResourceAttr(
    31  						"aws_api_gateway_api_key.test", "description", "Managed by Terraform"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func testAccCheckAWSAPIGatewayApiKeyStageKeyAttribute(conf *apigateway.ApiKey) resource.TestCheckFunc {
    39  	return func(s *terraform.State) error {
    40  		if len(conf.StageKeys) != 1 {
    41  			return fmt.Errorf("Expected one apikey. Got %d", len(conf.StageKeys))
    42  		}
    43  		if !strings.Contains(*conf.StageKeys[0], "test") {
    44  			return fmt.Errorf("Expected apikey for test. Got %q", *conf.StageKeys[0])
    45  		}
    46  		return nil
    47  	}
    48  }
    49  
    50  func testAccCheckAWSAPIGatewayApiKeyExists(n string, res *apigateway.ApiKey) resource.TestCheckFunc {
    51  	return func(s *terraform.State) error {
    52  		rs, ok := s.RootModule().Resources[n]
    53  		if !ok {
    54  			return fmt.Errorf("Not found: %s", n)
    55  		}
    56  
    57  		if rs.Primary.ID == "" {
    58  			return fmt.Errorf("No API Gateway ApiKey ID is set")
    59  		}
    60  
    61  		conn := testAccProvider.Meta().(*AWSClient).apigateway
    62  
    63  		req := &apigateway.GetApiKeyInput{
    64  			ApiKey: aws.String(rs.Primary.ID),
    65  		}
    66  		describe, err := conn.GetApiKey(req)
    67  		if err != nil {
    68  			return err
    69  		}
    70  
    71  		if *describe.Id != rs.Primary.ID {
    72  			return fmt.Errorf("APIGateway ApiKey not found")
    73  		}
    74  
    75  		*res = *describe
    76  
    77  		return nil
    78  	}
    79  }
    80  
    81  func testAccCheckAWSAPIGatewayApiKeyDestroy(s *terraform.State) error {
    82  	conn := testAccProvider.Meta().(*AWSClient).apigateway
    83  
    84  	for _, rs := range s.RootModule().Resources {
    85  		if rs.Type != "aws_api_gateway_api_key" {
    86  			continue
    87  		}
    88  
    89  		describe, err := conn.GetApiKeys(&apigateway.GetApiKeysInput{})
    90  
    91  		if err == nil {
    92  			if len(describe.Items) != 0 &&
    93  				*describe.Items[0].Id == rs.Primary.ID {
    94  				return fmt.Errorf("API Gateway ApiKey still exists")
    95  			}
    96  		}
    97  
    98  		aws2err, ok := err.(awserr.Error)
    99  		if !ok {
   100  			return err
   101  		}
   102  		if aws2err.Code() != "NotFoundException" {
   103  			return err
   104  		}
   105  
   106  		return nil
   107  	}
   108  
   109  	return nil
   110  }
   111  
   112  const testAccAWSAPIGatewayApiKeyConfig = `
   113  resource "aws_api_gateway_rest_api" "test" {
   114    name = "test"
   115  }
   116  
   117  resource "aws_api_gateway_resource" "test" {
   118    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   119    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   120    path_part = "test"
   121  }
   122  
   123  resource "aws_api_gateway_method" "test" {
   124    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   125    resource_id = "${aws_api_gateway_resource.test.id}"
   126    http_method = "GET"
   127    authorization = "NONE"
   128  }
   129  
   130  resource "aws_api_gateway_method_response" "error" {
   131    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   132    resource_id = "${aws_api_gateway_resource.test.id}"
   133    http_method = "${aws_api_gateway_method.test.http_method}"
   134    status_code = "400"
   135  }
   136  
   137  resource "aws_api_gateway_integration" "test" {
   138    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   139    resource_id = "${aws_api_gateway_resource.test.id}"
   140    http_method = "${aws_api_gateway_method.test.http_method}"
   141  
   142    type = "HTTP"
   143    uri = "https://www.google.de"
   144    integration_http_method = "GET"
   145  }
   146  
   147  resource "aws_api_gateway_integration_response" "test" {
   148    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   149    resource_id = "${aws_api_gateway_resource.test.id}"
   150    http_method = "${aws_api_gateway_integration.test.http_method}"
   151    status_code = "${aws_api_gateway_method_response.error.status_code}"
   152  }
   153  
   154  resource "aws_api_gateway_deployment" "test" {
   155    depends_on = ["aws_api_gateway_integration.test"]
   156  
   157    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   158    stage_name = "test"
   159    description = "This is a test"
   160  
   161    variables = {
   162      "a" = "2"
   163    }
   164  }
   165  
   166  resource "aws_api_gateway_api_key" "test" {
   167    name = "foo"
   168    enabled = true
   169  
   170    stage_key {
   171      rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   172      stage_name = "${aws_api_gateway_deployment.test.stage_name}"
   173    }
   174  }
   175  `