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