Skip to content

Commit

Permalink
Merge pull request #28030 from StateFarmIns/f-aws_api_gateway_deploym…
Browse files Browse the repository at this point in the history
…ent-import

Added import support for API Gateway Deployment
  • Loading branch information
ewbankkit authored Nov 30, 2022
2 parents 83ca7ff + 41d45bf commit ab9fa23
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .changelog/28030.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_api_gateway_deployment: Add importability
```
21 changes: 21 additions & 0 deletions internal/service/apigateway/deployment.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package apigateway

import (
"context"
"fmt"
"log"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
35 changes: 28 additions & 7 deletions internal/service/apigateway/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand All @@ -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,
},
},
})
}
Expand Down Expand Up @@ -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) },
Expand All @@ -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()))),
),
},
},
Expand Down Expand Up @@ -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" {
Expand Down
12 changes: 12 additions & 0 deletions website/docs/r/api_gateway_deployment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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`, `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.

0 comments on commit ab9fa23

Please sign in to comment.