From 7877b2e03a74123d9410c44cb713b646b80cff31 Mon Sep 17 00:00:00 2001 From: Nick Dunne Date: Sun, 27 Nov 2022 15:01:12 -0600 Subject: [PATCH 1/6] added import function for API gateway deployment --- internal/service/apigateway/deployment.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/internal/service/apigateway/deployment.go b/internal/service/apigateway/deployment.go index 0cf2c0c3450..74e84587d06 100644 --- a/internal/service/apigateway/deployment.go +++ b/internal/service/apigateway/deployment.go @@ -1,8 +1,10 @@ package apigateway import ( + "context" "fmt" "log" + "strings" "time" "github.com/aws/aws-sdk-go/aws" @@ -22,6 +24,10 @@ func ResourceDeployment() *schema.Resource { Update: resourceDeploymentUpdate, Delete: resourceDeploymentDelete, + Importer: &schema.ResourceImporter{ + StateContext: resourceDeploymentImport, + }, + Schema: map[string]*schema.Schema{ "rest_api_id": { Type: schema.TypeString, @@ -221,3 +227,18 @@ func resourceDeploymentDelete(d *schema.ResourceData, meta interface{}) error { return nil } + +func resourceDeploymentImport(_ context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + idParts := strings.Split(d.Id(), "/") + if len(idParts) != 2 { + return nil, fmt.Errorf("Unexpected format of ID (%s), use: 'REST-API-ID/DEPLOYMENT-ID'", d.Id()) + } + + restApiID := idParts[0] + deploymentID := idParts[1] + + d.SetId(deploymentID) + d.Set("rest_api_id", restApiID) + + return []*schema.ResourceData{d}, nil +} From 58a4799e443725e4fa7afd30878284082b754c3a Mon Sep 17 00:00:00 2001 From: Nick Dunne Date: Sun, 27 Nov 2022 15:01:41 -0600 Subject: [PATCH 2/6] added import to api gateway deployment docs --- website/docs/r/api_gateway_deployment.html.markdown | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/website/docs/r/api_gateway_deployment.html.markdown b/website/docs/r/api_gateway_deployment.html.markdown index 84a9881a84c..6ae934e4285 100644 --- a/website/docs/r/api_gateway_deployment.html.markdown +++ b/website/docs/r/api_gateway_deployment.html.markdown @@ -148,3 +148,15 @@ In addition to all arguments above, the following attributes are exported: when allowing API Gateway to invoke a Lambda function, e.g., `arn:aws:execute-api:eu-west-2:123456789012:z4675bid1j/prod` * `created_date` - Creation date of the deployment + +## Import + +`aws_api_gateway_deployment` can be imported using `REST-API-ID/DEPLOYMENT-ID`, e.g., + +``` +$ terraform import aws_api_gateway_deployment.example aabbccddee/1122334 +``` + +The `stage_name` and `stage_description` arguments cannot be imported. Use the [`aws_api_gateway_stage` resource](api_gateway_stage.html) to import and manage stages. + +The `triggers` argument cannot be imported. \ No newline at end of file From 8262e2bba2d021d0786f277f91c268a4e5609022 Mon Sep 17 00:00:00 2001 From: Nick Dunne Date: Sun, 27 Nov 2022 15:30:28 -0600 Subject: [PATCH 3/6] added changelog entry --- .changelog/28030.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/28030.txt diff --git a/.changelog/28030.txt b/.changelog/28030.txt new file mode 100644 index 00000000000..b9ce2306bc0 --- /dev/null +++ b/.changelog/28030.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_api_gateway_deployment: Added ability to import +``` From a86a8c35c044b1d3541ff7efd33771f6bc6b2185 Mon Sep 17 00:00:00 2001 From: Nick Dunne Date: Sun, 27 Nov 2022 15:32:21 -0600 Subject: [PATCH 4/6] updated docs to add variables attribute --- website/docs/r/api_gateway_deployment.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/api_gateway_deployment.html.markdown b/website/docs/r/api_gateway_deployment.html.markdown index 6ae934e4285..83a66aff0eb 100644 --- a/website/docs/r/api_gateway_deployment.html.markdown +++ b/website/docs/r/api_gateway_deployment.html.markdown @@ -157,6 +157,6 @@ In addition to all arguments above, the following attributes are exported: $ terraform import aws_api_gateway_deployment.example aabbccddee/1122334 ``` -The `stage_name` and `stage_description` arguments cannot be imported. Use the [`aws_api_gateway_stage` resource](api_gateway_stage.html) to import and manage stages. +The `stage_name`, `stage_description`, and `variables` arguments cannot be imported. Use the [`aws_api_gateway_stage` resource](api_gateway_stage.html) to import and manage stages. The `triggers` argument cannot be imported. \ No newline at end of file From 53fc79c8b7f7a6f51aa7cd7605f35535374fa9e0 Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 27 Nov 2022 19:02:55 -0600 Subject: [PATCH 5/6] refactored api gateway deployment test moved stage url validation to stage specific config and removed stage name/desc from basic test --- .../service/apigateway/deployment_test.go | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/internal/service/apigateway/deployment_test.go b/internal/service/apigateway/deployment_test.go index ade68071cc4..5d2955b2ebf 100644 --- a/internal/service/apigateway/deployment_test.go +++ b/internal/service/apigateway/deployment_test.go @@ -20,7 +20,6 @@ func TestAccAPIGatewayDeployment_basic(t *testing.T) { var deployment apigateway.Deployment resourceName := "aws_api_gateway_deployment.test" restApiResourceName := "aws_api_gateway_rest_api.test" - rName := sdkacctest.RandomWithPrefix("tf-acc-test-deployment") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(t); acctest.PreCheckAPIGatewayTypeEDGE(t) }, @@ -29,19 +28,25 @@ func TestAccAPIGatewayDeployment_basic(t *testing.T) { CheckDestroy: testAccCheckDeploymentDestroy, Steps: []resource.TestStep{ { - Config: testAccDeploymentConfig_stageName(rName), + Config: testAccDeploymentConfig_required(), Check: resource.ComposeTestCheckFunc( testAccCheckDeploymentExists(resourceName, &deployment), resource.TestCheckResourceAttrSet(resourceName, "created_date"), resource.TestCheckResourceAttr(resourceName, "description", ""), - acctest.MatchResourceAttrRegionalARN(resourceName, "execution_arn", "execute-api", regexp.MustCompile(fmt.Sprintf(".+/%s", rName))), - resource.TestMatchResourceAttr(resourceName, "invoke_url", regexp.MustCompile(fmt.Sprintf("https://.+\\.execute-api\\.%s.amazonaws\\.com/%s", acctest.Region(), rName))), + acctest.MatchResourceAttrRegionalARN(resourceName, "execution_arn", "execute-api", regexp.MustCompile(".+/")), + resource.TestMatchResourceAttr(resourceName, "invoke_url", regexp.MustCompile(fmt.Sprintf("https://.+\\.execute-api\\.%s.amazonaws\\.com/", acctest.Region()))), resource.TestCheckResourceAttrPair(resourceName, "rest_api_id", restApiResourceName, "id"), resource.TestCheckNoResourceAttr(resourceName, "stage_description"), - resource.TestCheckResourceAttr(resourceName, "stage_name", rName), + resource.TestCheckNoResourceAttr(resourceName, "stage_name"), resource.TestCheckNoResourceAttr(resourceName, "variables.%"), ), }, + { + ResourceName: resourceName, + ImportStateIdFunc: testAccDeploymentImportStateIdFunc(resourceName), + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -184,6 +189,7 @@ func TestAccAPIGatewayDeployment_stageName(t *testing.T) { var deployment apigateway.Deployment var stage apigateway.Stage resourceName := "aws_api_gateway_deployment.test" + rName := sdkacctest.RandomWithPrefix("tf-acc-test-deployment") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(t); acctest.PreCheckAPIGatewayTypeEDGE(t) }, @@ -192,17 +198,21 @@ func TestAccAPIGatewayDeployment_stageName(t *testing.T) { CheckDestroy: testAccCheckDeploymentDestroy, Steps: []resource.TestStep{ { - Config: testAccDeploymentConfig_stageName("test"), + Config: testAccDeploymentConfig_stageName(rName), Check: resource.ComposeTestCheckFunc( testAccCheckDeploymentExists(resourceName, &deployment), testAccCheckStageExists(resourceName, &stage), - resource.TestCheckResourceAttr(resourceName, "stage_name", "test"), + resource.TestCheckResourceAttr(resourceName, "stage_name", rName), + acctest.MatchResourceAttrRegionalARN(resourceName, "execution_arn", "execute-api", regexp.MustCompile(fmt.Sprintf(".+/%s", rName))), + resource.TestMatchResourceAttr(resourceName, "invoke_url", regexp.MustCompile(fmt.Sprintf("https://.+\\.execute-api\\.%s.amazonaws\\.com/%s", acctest.Region(), rName))), ), }, { Config: testAccDeploymentConfig_required(), Check: resource.ComposeTestCheckFunc( resource.TestCheckNoResourceAttr(resourceName, "stage_name"), + acctest.MatchResourceAttrRegionalARN(resourceName, "execution_arn", "execute-api", regexp.MustCompile(".+/")), + resource.TestMatchResourceAttr(resourceName, "invoke_url", regexp.MustCompile(fmt.Sprintf("https://.+\\.execute-api\\.%s.amazonaws\\.com/", acctest.Region()))), ), }, }, @@ -337,6 +347,17 @@ func testAccCheckDeploymentRecreated(i, j *apigateway.Deployment) resource.TestC } } +func testAccDeploymentImportStateIdFunc(resourceName string) resource.ImportStateIdFunc { + return func(s *terraform.State) (string, error) { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return "", fmt.Errorf("Not Found: %s", resourceName) + } + + return fmt.Sprintf("%s/%s", rs.Primary.Attributes["rest_api_id"], rs.Primary.ID), nil + } +} + func testAccDeploymentBaseConfig(uri string) string { return fmt.Sprintf(` resource "aws_api_gateway_rest_api" "test" { From 7ad918fa11493fe45a25247f2e0ceebf971958a0 Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 27 Nov 2022 19:42:07 -0600 Subject: [PATCH 6/6] modified new changelog file --- .changelog/28030.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changelog/28030.txt b/.changelog/28030.txt index b9ce2306bc0..9f3e3bb9f41 100644 --- a/.changelog/28030.txt +++ b/.changelog/28030.txt @@ -1,3 +1,3 @@ ```release-note:enhancement -resource/aws_api_gateway_deployment: Added ability to import +resource/aws_api_gateway_deployment: Add importability ```