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_model: Support resource import #5572

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
32 changes: 30 additions & 2 deletions aws/resource_aws_api_gateway_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -18,6 +19,33 @@ func resourceAwsApiGatewayModel() *schema.Resource {
Read: resourceAwsApiGatewayModelRead,
Update: resourceAwsApiGatewayModelUpdate,
Delete: resourceAwsApiGatewayModelDelete,
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/NAME", d.Id())
}
restApiID := idParts[0]
name := idParts[1]
d.Set("name", name)
d.Set("rest_api_id", restApiID)

conn := meta.(*AWSClient).apigateway

output, err := conn.GetModel(&apigateway.GetModelInput{
ModelName: aws.String(name),
RestApiId: aws.String(restApiID),
})

if err != nil {
return nil, err
}

d.SetId(aws.StringValue(output.Id))
Copy link
Contributor

Choose a reason for hiding this comment

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

why not call d.SetId and then the Read function? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case, the model ID is actually worthless for read. At some point we should switch the resource ID to be the REST API ID and model name instead of the model ID, but that could be perceived as a breaking change.


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

Schema: map[string]*schema.Schema{
"rest_api_id": &schema.Schema{
Expand Down Expand Up @@ -100,10 +128,10 @@ func resourceAwsApiGatewayModelRead(d *schema.ResourceData, meta interface{}) er
return err
}
log.Printf("[DEBUG] Received API Gateway Model: %s", out)
d.SetId(*out.Id)

d.Set("content_type", out.ContentType)
d.Set("description", out.Description)
d.Set("schema", out.Schema)
d.Set("content_type", out.ContentType)

return nil
}
Expand Down
17 changes: 17 additions & 0 deletions aws/resource_aws_api_gateway_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func TestAccAWSAPIGatewayModel_basic(t *testing.T) {
"aws_api_gateway_model.test", "content_type", "application/json"),
),
},
{
ResourceName: "aws_api_gateway_model.test",
ImportState: true,
ImportStateIdFunc: testAccAWSAPIGatewayModelImportStateIdFunc("aws_api_gateway_model.test"),
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -117,6 +123,17 @@ func testAccCheckAWSAPIGatewayModelDestroy(s *terraform.State) error {
return nil
}

func testAccAWSAPIGatewayModelImportStateIdFunc(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["name"]), nil
}
}

const testAccAWSAPIGatewayModelConfig = `
resource "aws_api_gateway_rest_api" "test" {
name = "test"
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/api_gateway_model.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ The following arguments are supported:
In addition to all arguments above, the following attributes are exported:

* `id` - The ID of the model

## Import

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

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