Skip to content

Commit

Permalink
Merge pull request #7698 from terraform-providers/td-aws_api_gateway_…
Browse files Browse the repository at this point in the history
…api_key-remove-deprecated

resource/aws_api_gateway_api_key: Remove deprecated stage_key configuration block
  • Loading branch information
bflad authored Feb 27, 2019
2 parents 3e15c40 + 5f6d5da commit edc186e
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 258 deletions.
40 changes: 15 additions & 25 deletions aws/resource_aws_api_gateway_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
Expand Down Expand Up @@ -42,9 +41,9 @@ func resourceAwsApiGatewayApiKey() *schema.Resource {
},

"stage_key": {
Type: schema.TypeSet,
Optional: true,
Deprecated: "Since the API Gateway usage plans feature was launched on August 11, 2016, usage plans are now required to associate an API key with an API stage",
Type: schema.TypeSet,
Optional: true,
Removed: "Since the API Gateway usage plans feature was launched on August 11, 2016, usage plans are now required to associate an API key with an API stage",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"rest_api_id": {
Expand Down Expand Up @@ -91,7 +90,6 @@ func resourceAwsApiGatewayApiKeyCreate(d *schema.ResourceData, meta interface{})
Description: aws.String(d.Get("description").(string)),
Enabled: aws.Bool(d.Get("enabled").(bool)),
Value: aws.String(d.Get("value").(string)),
StageKeys: expandApiGatewayStageKeys(d),
})
if err != nil {
return fmt.Errorf("Error creating API Gateway: %s", err)
Expand Down Expand Up @@ -123,15 +121,14 @@ func resourceAwsApiGatewayApiKeyRead(d *schema.ResourceData, meta interface{}) e
d.Set("name", apiKey.Name)
d.Set("description", apiKey.Description)
d.Set("enabled", apiKey.Enabled)
d.Set("stage_key", flattenApiGatewayStageKeys(apiKey.StageKeys))
d.Set("value", apiKey.Value)

if err := d.Set("created_date", apiKey.CreatedDate.Format(time.RFC3339)); err != nil {
log.Printf("[DEBUG] Error setting created_date: %s", err)
return fmt.Errorf("error setting created_date: %s", err)
}

if err := d.Set("last_updated_date", apiKey.LastUpdatedDate.Format(time.RFC3339)); err != nil {
log.Printf("[DEBUG] Error setting last_updated_date: %s", err)
return fmt.Errorf("error setting last_updated_date: %s", err)
}

return nil
Expand Down Expand Up @@ -159,9 +156,6 @@ func resourceAwsApiGatewayApiKeyUpdateOperations(d *schema.ResourceData) []*apig
})
}

if d.HasChange("stage_key") {
operations = append(operations, expandApiGatewayStageKeyOperations(d)...)
}
return operations
}

Expand All @@ -185,21 +179,17 @@ func resourceAwsApiGatewayApiKeyDelete(d *schema.ResourceData, meta interface{})
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Deleting API Gateway API Key: %s", d.Id())

return resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := conn.DeleteApiKey(&apigateway.DeleteApiKeyInput{
ApiKey: aws.String(d.Id()),
})
_, err := conn.DeleteApiKey(&apigateway.DeleteApiKeyInput{
ApiKey: aws.String(d.Id()),
})

if err == nil {
return nil
}
if isAWSErr(err, apigateway.ErrCodeNotFoundException, "") {
return nil
}

if err != nil {
if isAWSErr(err, apigateway.ErrCodeNotFoundException, "") {
return nil
}
}
if err != nil {
return fmt.Errorf("error deleting API Gateway API Key (%s): %s", d.Id(), err)
}

return resource.NonRetryableError(err)
})
return nil
}
210 changes: 121 additions & 89 deletions aws/resource_aws_api_gateway_api_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,139 @@ package aws

import (
"fmt"
"strings"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSAPIGatewayApiKey_importBasic(t *testing.T) {
func TestAccAWSAPIGatewayApiKey_basic(t *testing.T) {
var apiKey1 apigateway.ApiKey
resourceName := "aws_api_gateway_api_key.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayApiKeyDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAPIGatewayApiKeyConfig,
Config: testAccAWSAPIGatewayApiKeyConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayApiKeyExists(resourceName, &apiKey1),
resource.TestCheckResourceAttrSet(resourceName, "created_date"),
resource.TestCheckResourceAttr(resourceName, "description", "Managed by Terraform"),
resource.TestCheckResourceAttr(resourceName, "enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttrSet(resourceName, "last_updated_date"),
resource.TestCheckResourceAttrSet(resourceName, "value"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSAPIGatewayApiKey_Description(t *testing.T) {
var apiKey1, apiKey2 apigateway.ApiKey
resourceName := "aws_api_gateway_api_key.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayApiKeyDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAPIGatewayApiKeyConfigDescription(rName, "description1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayApiKeyExists(resourceName, &apiKey1),
resource.TestCheckResourceAttr(resourceName, "description", "description1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSAPIGatewayApiKeyConfigDescription(rName, "description2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayApiKeyExists(resourceName, &apiKey2),
testAccCheckAWSAPIGatewayApiKeyNotRecreated(&apiKey1, &apiKey2),
resource.TestCheckResourceAttr(resourceName, "description", "description2"),
),
},
},
})
}

func TestAccAWSAPIGatewayApiKey_basic(t *testing.T) {
var conf apigateway.ApiKey
func TestAccAWSAPIGatewayApiKey_Enabled(t *testing.T) {
var apiKey1, apiKey2 apigateway.ApiKey
resourceName := "aws_api_gateway_api_key.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayApiKeyDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAPIGatewayApiKeyConfig,
Config: testAccAWSAPIGatewayApiKeyConfigEnabled(rName, false),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayApiKeyExists("aws_api_gateway_api_key.test", &conf),
testAccCheckAWSAPIGatewayApiKeyStageKeyAttribute(&conf),
resource.TestCheckResourceAttr(
"aws_api_gateway_api_key.test", "name", "foo"),
resource.TestCheckResourceAttr(
"aws_api_gateway_api_key.test", "description", "Managed by Terraform"),
resource.TestCheckResourceAttrSet(
"aws_api_gateway_api_key.test", "created_date"),
resource.TestCheckResourceAttrSet(
"aws_api_gateway_api_key.test", "last_updated_date"),
resource.TestCheckResourceAttr(
"aws_api_gateway_api_key.custom", "value", "MyCustomToken#@&\"'(§!ç)-_*$€¨^£%ù+=/:.;?,|"),
testAccCheckAWSAPIGatewayApiKeyExists(resourceName, &apiKey1),
resource.TestCheckResourceAttr(resourceName, "enabled", "false"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSAPIGatewayApiKeyConfigEnabled(rName, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayApiKeyExists(resourceName, &apiKey2),
testAccCheckAWSAPIGatewayApiKeyNotRecreated(&apiKey1, &apiKey2),
resource.TestCheckResourceAttr(resourceName, "enabled", "true"),
),
},
},
})
}

func testAccCheckAWSAPIGatewayApiKeyStageKeyAttribute(conf *apigateway.ApiKey) resource.TestCheckFunc {
return func(s *terraform.State) error {
if len(conf.StageKeys) != 1 {
return fmt.Errorf("Expected one apikey. Got %d", len(conf.StageKeys))
}
if !strings.Contains(*conf.StageKeys[0], "test") {
return fmt.Errorf("Expected apikey for test. Got %q", *conf.StageKeys[0])
}
return nil
}
func TestAccAWSAPIGatewayApiKey_Value(t *testing.T) {
var apiKey1 apigateway.ApiKey
resourceName := "aws_api_gateway_api_key.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayApiKeyDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAPIGatewayApiKeyConfigValue(rName, `MyCustomToken#@&\"'(§!ç)-_*$€¨^£%ù+=/:.;?,|`),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayApiKeyExists(resourceName, &apiKey1),
resource.TestCheckResourceAttr(resourceName, "value", `MyCustomToken#@&\"'(§!ç)-_*$€¨^£%ù+=/:.;?,|`),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckAWSAPIGatewayApiKeyExists(n string, res *apigateway.ApiKey) resource.TestCheckFunc {
Expand Down Expand Up @@ -136,78 +199,47 @@ func testAccCheckAWSAPIGatewayApiKeyDestroy(s *terraform.State) error {
return nil
}

const testAccAWSAPIGatewayApiKeyConfig = `
resource "aws_api_gateway_rest_api" "test" {
name = "test"
}
func testAccCheckAWSAPIGatewayApiKeyNotRecreated(i, j *apigateway.ApiKey) resource.TestCheckFunc {
return func(s *terraform.State) error {
if aws.TimeValue(i.CreatedDate) != aws.TimeValue(j.CreatedDate) {
return fmt.Errorf("API Gateway API Key recreated")
}

resource "aws_api_gateway_resource" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
path_part = "test"
return nil
}
}

resource "aws_api_gateway_method" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "GET"
authorization = "NONE"
func testAccAWSAPIGatewayApiKeyConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_api_gateway_api_key" "test" {
name = %[1]q
}
resource "aws_api_gateway_method_response" "error" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "${aws_api_gateway_method.test.http_method}"
status_code = "400"
`, rName)
}

resource "aws_api_gateway_integration" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "${aws_api_gateway_method.test.http_method}"
type = "HTTP"
uri = "https://www.google.de"
integration_http_method = "GET"
func testAccAWSAPIGatewayApiKeyConfigDescription(rName, description string) string {
return fmt.Sprintf(`
resource "aws_api_gateway_api_key" "test" {
description = %[2]q
name = %[1]q
}
resource "aws_api_gateway_integration_response" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "${aws_api_gateway_integration.test.http_method}"
status_code = "${aws_api_gateway_method_response.error.status_code}"
`, rName, description)
}

resource "aws_api_gateway_deployment" "test" {
depends_on = ["aws_api_gateway_integration.test"]
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
stage_name = "test"
description = "This is a test"
variables = {
"a" = "2"
}
func testAccAWSAPIGatewayApiKeyConfigEnabled(rName string, enabled bool) string {
return fmt.Sprintf(`
resource "aws_api_gateway_api_key" "test" {
enabled = %[2]t
name = %[1]q
}
`, rName, enabled)
}

func testAccAWSAPIGatewayApiKeyConfigValue(rName, value string) string {
return fmt.Sprintf(`
resource "aws_api_gateway_api_key" "test" {
name = "foo"
enabled = true
stage_key {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
stage_name = "${aws_api_gateway_deployment.test.stage_name}"
}
name = %[1]q
value = %[2]q
}
resource "aws_api_gateway_api_key" "custom" {
name = "bar"
enabled = true
value = "MyCustomToken#@&\"'(§!ç)-_*$€¨^£%ù+=/:.;?,|"
stage_key {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
stage_name = "${aws_api_gateway_deployment.test.stage_name}"
}
`, rName, value)
}
`
Loading

0 comments on commit edc186e

Please sign in to comment.