Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_api_gateway_stage: Support resource import #5575

Merged
merged 1 commit into from
Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions aws/resource_aws_api_gateway_stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ func resourceAwsApiGatewayStage() *schema.Resource {
Read: resourceAwsApiGatewayStageRead,
Update: resourceAwsApiGatewayStageUpdate,
Delete: resourceAwsApiGatewayStageDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), "/")
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
return nil, fmt.Errorf("Unexpected format of ID (%q), expected REST-API-ID/STAGE-NAME", d.Id())
}
restApiID := idParts[0]
stageName := idParts[1]
d.Set("stage_name", stageName)
d.Set("rest_api_id", restApiID)
d.SetId(fmt.Sprintf("ags-%s-%s", restApiID, stageName))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(as in the other PR's) - why not set the ID and then call read?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting for future posterity: in these PRs we're just doing the minimum to support resource import (with a consistent format of REST-API-ID/THING) -- we can eventually switch to read/update/delete via the resource ID, but this implementation provides the bare minimum needed for import without the unrelated code changes right now.

return []*schema.ResourceData{d}, nil
},
},

Schema: map[string]*schema.Schema{
"access_log_settings": {
Expand Down Expand Up @@ -219,8 +233,14 @@ func resourceAwsApiGatewayStageRead(d *schema.ResourceData, meta interface{}) er
d.Set("deployment_id", stage.DeploymentId)
d.Set("description", stage.Description)
d.Set("documentation_version", stage.DocumentationVersion)
d.Set("variables", aws.StringValueMap(stage.Variables))
d.Set("tags", aws.StringValueMap(stage.Tags))

if err := d.Set("tags", aws.StringValueMap(stage.Tags)); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

if err := d.Set("variables", aws.StringValueMap(stage.Variables)); err != nil {
return fmt.Errorf("error setting variables: %s", err)
}

region := meta.(*AWSClient).region
d.Set("invoke_url", buildApiGatewayInvokeURL(restApiId, region, stageName))
Expand Down
18 changes: 18 additions & 0 deletions aws/resource_aws_api_gateway_stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func TestAccAWSAPIGatewayStage_basic(t *testing.T) {
resource.TestCheckResourceAttrSet("aws_api_gateway_stage.test", "invoke_url"),
),
},
{
ResourceName: "aws_api_gateway_stage.test",
ImportState: true,
ImportStateIdFunc: testAccAWSAPIGatewayStageImportStateIdFunc("aws_api_gateway_stage.test"),
ImportStateVerify: true,
},
resource.TestStep{
Config: testAccAWSAPIGatewayStageConfig_updated(rName),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -82,6 +88,7 @@ func TestAccAWSAPIGatewayStage_accessLogSettings(t *testing.T) {
resource.TestCheckResourceAttr("aws_api_gateway_stage.test", "access_log_settings.0.format", clf),
),
},

{
Config: testAccAWSAPIGatewayStageConfig_accessLogSettings(rName, json),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -179,6 +186,17 @@ func testAccCheckAWSAPIGatewayStageDestroy(s *terraform.State) error {
return nil
}

func testAccAWSAPIGatewayStageImportStateIdFunc(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.Attributes["stage_name"]), nil
}
}

func testAccAWSAPIGatewayStageConfig_base(rName string) string {
return fmt.Sprintf(`
resource "aws_api_gateway_rest_api" "test" {
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/api_gateway_stage.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,11 @@ In addition to all arguments above, the following attributes are exported:
* `execution_arn` - The execution ARN to be used in [`lambda_permission`](/docs/providers/aws/r/lambda_permission.html)'s `source_arn`
when allowing API Gateway to invoke a Lambda function,
e.g. `arn:aws:execute-api:eu-west-2:123456789012:z4675bid1j/prod`

## Import

`aws_api_gateway_stage` can be imported using `REST-API-ID/STAGE-NAME`, e.g.

```
$ terraform import aws_api_gateway_stage.example 12345abcde/example
```