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

provider/aws: API Gateway additional resources #6175

Closed
wants to merge 5 commits into from
Closed
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
3 changes: 3 additions & 0 deletions builtin/providers/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,17 @@ func Provider() terraform.ResourceProvider {
"aws_ami_copy": resourceAwsAmiCopy(),
"aws_ami_from_instance": resourceAwsAmiFromInstance(),
"aws_api_gateway_api_key": resourceAwsApiGatewayApiKey(),
"aws_api_gateway_base_path_mapping": resourceAwsApiGatewayBasePathMapping(),
"aws_api_gateway_deployment": resourceAwsApiGatewayDeployment(),
"aws_api_gateway_domain": resourceAwsApiGatewayDomain(),
"aws_api_gateway_integration": resourceAwsApiGatewayIntegration(),
"aws_api_gateway_integration_response": resourceAwsApiGatewayIntegrationResponse(),
"aws_api_gateway_method": resourceAwsApiGatewayMethod(),
"aws_api_gateway_method_response": resourceAwsApiGatewayMethodResponse(),
"aws_api_gateway_model": resourceAwsApiGatewayModel(),
"aws_api_gateway_resource": resourceAwsApiGatewayResource(),
"aws_api_gateway_rest_api": resourceAwsApiGatewayRestApi(),
"aws_api_gateway_swagger_api": resourceAwsApiGatewaySwaggerAPI(),
"aws_app_cookie_stickiness_policy": resourceAwsAppCookieStickinessPolicy(),
"aws_autoscaling_group": resourceAwsAutoscalingGroup(),
"aws_autoscaling_notification": resourceAwsAutoscalingNotification(),
Expand Down
105 changes: 105 additions & 0 deletions builtin/providers/aws/resource_aws_api_gateway_base_path_mapping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package aws

import (
"fmt"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsApiGatewayBasePathMapping() *schema.Resource {
return &schema.Resource{
Create: resourceAwsApiGatewayBasePathMappingCreate,
Read: resourceAwsApiGatewayBasePathMappingRead,
Delete: resourceAwsApiGatewayBasePathMappingDelete,

Schema: map[string]*schema.Schema{
"api_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"path": &schema.Schema{
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"stage": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"domain_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceAwsApiGatewayBasePathMappingCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway

err := resource.Retry(30*time.Second, func() *resource.RetryError {
r, err := conn.CreateBasePathMapping(&apigateway.CreateBasePathMappingInput{
RestApiId: aws.String(d.Get("api_id").(string)),
DomainName: aws.String(d.Get("domain_name").(string)),
BasePath: aws.String(d.Get("path").(string)),
Stage: aws.String(d.Get("stage").(string)),
})

if err != nil {
if err, ok := err.(awserr.Error); ok && err.Code() != "BadRequestException" {
return &resource.RetryError{
Err: err,
Retryable: false,
}
}

return &resource.RetryError{
Err: fmt.Errorf("Error creating Gateway base path mapping: %s", err),
Retryable: true,
}
}

id := fmt.Sprintf("apigateway-base-path-mapping-%s-%s", r.BasePath, d.Get("domain_name"))

d.SetId(id)

return nil
})

if err != nil {
return fmt.Errorf("Error creating Gateway base path mapping: %s", err)
}

return resourceAwsApiGatewayBasePathMappingRead(d, meta)
}

func resourceAwsApiGatewayBasePathMappingRead(d *schema.ResourceData, meta interface{}) error {

return nil
}

func resourceAwsApiGatewayBasePathMappingDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway

_, err := conn.DeleteBasePathMapping(&apigateway.DeleteBasePathMappingInput{
DomainName: aws.String(d.Get("domain_name").(string)),
BasePath: aws.String(d.Get("path").(string)),
})

if err != nil {
if err, ok := err.(awserr.Error); ok && err.Code() == "NotFoundException" {
return nil
}
return nil
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package aws

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSAPIGatewayBasePath_basic(t *testing.T) {
var conf apigateway.BasePathMapping
name := domainNameFromTime()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayBasePathDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSAPIGatewayBasePathConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayBasePathExists("aws_api_gateway_base_path.test", name, &conf),
),
},
},
})
}

func testAccCheckAWSAPIGatewayBasePathExists(n string, name string, res *apigateway.BasePathMapping) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No API Gateway ID is set")
}

conn := testAccProvider.Meta().(*AWSClient).apigateway

req := &apigateway.GetBasePathMappingInput{
DomainName: aws.String(name),
BasePath: aws.String("/mrtest"),
}
describe, err := conn.GetBasePathMapping(req)
if err != nil {
return err
}

if *describe.BasePath != "/mrtest" {
return fmt.Errorf("APIGateway not found")
}

*res = *describe

return nil
}
}

func testAccCheckAWSAPIGatewayBasePathDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).apigateway

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_api_gateway_rest_api" {
continue
}

req := &apigateway.GetBasePathMappingsInput{}
describe, err := conn.GetBasePathMappings(req)

if err == nil {
if len(describe.Items) != 0 &&
*describe.Items[0].BasePath == "/mrtest" {
return fmt.Errorf("Base path mapping still exists")
}
}

return err
}

return nil
}

func testAccAWSAPIGatewayBasePathConfig(name string) string {
return fmt.Sprintf(`
resource "aws_api_gateway_swagger_api" "test" {
swagger = <<EOF
{
"swagger": "2.0",
"info": {
"version": "1.0",
"title": "Hello World API"
},
"paths": {
"/hello/{user}": {
"get": {
"description": "Returns a greeting to the user!",
"parameters": [
{
"name": "user",
"in": "path",
"type": "string",
"required": true,
"description": "The name of the user to greet."
}
],
"responses": {
"200": {
"description": "Returns the greeting.",
"schema": {
"type": "string"
}
},
"400": {
"description": "Invalid characters in \"user\" were provided."
}
}
}
}
}
}
EOF
}

resource "aws_api_gateway_deployment" "test" {
rest_api_id = "${aws_api_gateway_swagger_api.test.id}"
stage_name = "test"
}

resource "aws_api_gateway_base_path_mapping" "test" {
api_id = "${aws_api_gateway_swagger_api.test.id}"
path = "/mrtest"
stage = "${aws_api_gateway_deployment.test.stage_name}"
domain_name = "${aws_api_gateway_domain.test.id}"
}

resource "aws_api_gateway_domain" "test" {
domain_name = "%s"
certificate_name = "test_api_cert"
certificate_body = "${file("test-fixtures/apigateway.crt")}"
certificate_private_key = "${file("test-fixtures/apigateway.key")}"
certificate_chain = "${file("test-fixtures/apigateway.crt")}"
}
`, name)
}
Loading