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