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_method: Support resource import #5571

Merged
merged 1 commit into from
Aug 22, 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
46 changes: 39 additions & 7 deletions aws/resource_aws_api_gateway_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -20,6 +21,22 @@ func resourceAwsApiGatewayMethod() *schema.Resource {
Read: resourceAwsApiGatewayMethodRead,
Update: resourceAwsApiGatewayMethodUpdate,
Delete: resourceAwsApiGatewayMethodDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), "/")
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
return nil, fmt.Errorf("Unexpected format of ID (%q), expected REST-API-ID/RESOURCE-ID/HTTP-METHOD", d.Id())
}
restApiID := idParts[0]
resourceID := idParts[1]
httpMethod := idParts[2]
d.Set("http_method", httpMethod)
d.Set("resource_id", resourceID)
d.Set("rest_api_id", restApiID)
d.SetId(fmt.Sprintf("agm-%s-%s-%s", restApiID, resourceID, httpMethod))
return []*schema.ResourceData{d}, nil
},
},

Schema: map[string]*schema.Schema{
"rest_api_id": &schema.Schema{
Expand Down Expand Up @@ -170,19 +187,34 @@ func resourceAwsApiGatewayMethodRead(d *schema.ResourceData, meta interface{}) e
return err
}
log.Printf("[DEBUG] Received API Gateway Method: %s", out)
d.SetId(fmt.Sprintf("agm-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string)))
d.Set("request_parameters", aws.BoolValueMap(out.RequestParameters))
d.Set("request_parameters_in_json", aws.BoolValueMap(out.RequestParameters))

d.Set("api_key_required", out.ApiKeyRequired)
d.Set("authorization", out.AuthorizationType)
d.Set("authorizer_id", out.AuthorizerId)
d.Set("request_models", aws.StringValueMap(out.RequestModels))
d.Set("request_validator_id", out.RequestValidatorId)

if err := d.Set("authorization_scopes", flattenStringList(out.AuthorizationScopes)); err != nil {
return fmt.Errorf("error setting authorization_scopes: %s", err)
}

d.Set("authorization", out.AuthorizationType)
d.Set("authorizer_id", out.AuthorizerId)

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

// KNOWN ISSUE: This next d.Set() is broken as it should be a JSON string of the map,
// however leaving as-is since this attribute has been deprecated
// for a very long time and will be removed soon in the next major release.
// Not worth the effort of fixing, acceptance testing, and potential JSON equivalence bugs.
if _, ok := d.GetOk("request_parameters_in_json"); ok {
d.Set("request_parameters_in_json", aws.BoolValueMap(out.RequestParameters))
}

if err := d.Set("request_parameters", aws.BoolValueMap(out.RequestParameters)); err != nil {
return fmt.Errorf("error setting request_models: %s", err)
}

d.Set("request_validator_id", out.RequestValidatorId)

return nil
}

Expand Down
35 changes: 35 additions & 0 deletions aws/resource_aws_api_gateway_method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ func TestAccAWSAPIGatewayMethod_basic(t *testing.T) {
"aws_api_gateway_method.test", "request_models.application/json", "Error"),
),
},
{
ResourceName: "aws_api_gateway_method.test",
ImportState: true,
ImportStateIdFunc: testAccAWSAPIGatewayMethodImportStateIdFunc("aws_api_gateway_method.test"),
ImportStateVerify: true,
},

{
Config: testAccAWSAPIGatewayMethodConfigUpdate(rInt),
Expand Down Expand Up @@ -71,6 +77,12 @@ func TestAccAWSAPIGatewayMethod_customauthorizer(t *testing.T) {
"aws_api_gateway_method.test", "request_models.application/json", "Error"),
),
},
{
ResourceName: "aws_api_gateway_method.test",
ImportState: true,
ImportStateIdFunc: testAccAWSAPIGatewayMethodImportStateIdFunc("aws_api_gateway_method.test"),
ImportStateVerify: true,
},

{
Config: testAccAWSAPIGatewayMethodConfigUpdate(rInt),
Expand Down Expand Up @@ -129,6 +141,12 @@ func TestAccAWSAPIGatewayMethod_cognitoauthorizer(t *testing.T) {
"aws_api_gateway_method.test", "authorization_scopes.#", "3"),
),
},
{
ResourceName: "aws_api_gateway_method.test",
ImportState: true,
ImportStateIdFunc: testAccAWSAPIGatewayMethodImportStateIdFunc("aws_api_gateway_method.test"),
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -157,6 +175,12 @@ func TestAccAWSAPIGatewayMethod_customrequestvalidator(t *testing.T) {
"aws_api_gateway_method.test", "request_validator_id", regexp.MustCompile("^[a-z0-9]{6}$")),
),
},
{
ResourceName: "aws_api_gateway_method.test",
ImportState: true,
ImportStateIdFunc: testAccAWSAPIGatewayMethodImportStateIdFunc("aws_api_gateway_method.test"),
ImportStateVerify: true,
},

{
Config: testAccAWSAPIGatewayMethodConfigWithCustomRequestValidatorUpdate(rInt),
Expand Down Expand Up @@ -281,6 +305,17 @@ func testAccCheckAWSAPIGatewayMethodDestroy(s *terraform.State) error {
return nil
}

func testAccAWSAPIGatewayMethodImportStateIdFunc(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/%s", rs.Primary.Attributes["rest_api_id"], rs.Primary.Attributes["resource_id"], rs.Primary.Attributes["http_method"]), nil
}
}

func testAccAWSAPIGatewayMethodConfigWithCustomAuthorizer(rInt int) string {
return fmt.Sprintf(`
resource "aws_api_gateway_rest_api" "test" {
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/api_gateway_method.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,11 @@ request_parameters = {
```
would define that the header `X-Some-Header` and the query string `some-query-param` must be provided on the request, or
* `request_parameters_in_json` - **Deprecated**, use `request_parameters` instead.

## Import

`aws_api_gateway_method` can be imported using `REST-API-ID/RESOURCE-ID/HTTP-METHOD`, e.g.

```
$ terraform import aws_api_gateway_method.example 12345abcde/67890fghij/GET
```