Add aws_api_gateway_resource resource

This commit is contained in:
Raphael Randschau 2016-03-05 23:14:35 +01:00
parent a73721d248
commit 7ead800f6a
5 changed files with 309 additions and 0 deletions

View File

@ -115,6 +115,7 @@ func Provider() terraform.ResourceProvider {
"aws_ami_copy": resourceAwsAmiCopy(),
"aws_ami_from_instance": resourceAwsAmiFromInstance(),
"aws_api_gateway_rest_api": resourceAwsApiGatewayRestApi(),
"aws_api_gateway_resource": resourceAwsApiGatewayResource(),
"aws_app_cookie_stickiness_policy": resourceAwsAppCookieStickinessPolicy(),
"aws_autoscaling_group": resourceAwsAutoscalingGroup(),
"aws_autoscaling_notification": resourceAwsAutoscalingNotification(),

View File

@ -0,0 +1,144 @@
package aws
import (
"fmt"
"log"
"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 resourceAwsApiGatewayResource() *schema.Resource {
return &schema.Resource{
Create: resourceAwsApiGatewayResourceCreate,
Read: resourceAwsApiGatewayResourceRead,
Update: resourceAwsApiGatewayResourceUpdate,
Delete: resourceAwsApiGatewayResourceDelete,
Schema: map[string]*schema.Schema{
"rest_api_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"parent_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"path_part": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"path": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsApiGatewayResourceCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Creating API Gateway Resource for API %s", d.Get("rest_api_id").(string))
var err error
resource, err := conn.CreateResource(&apigateway.CreateResourceInput{
ParentId: aws.String(d.Get("parent_id").(string)),
PathPart: aws.String(d.Get("path_part").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
})
if err != nil {
return fmt.Errorf("Error creating API Gateway Resource: %s", err)
}
d.SetId(*resource.Id)
d.Set("path", resource.Path)
return nil
}
func resourceAwsApiGatewayResourceRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Reading API Gateway Resource %s", d.Id())
resource, err := conn.GetResource(&apigateway.GetResourceInput{
ResourceId: aws.String(d.Id()),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
})
if err != nil {
return err
}
d.Set("parent_id", resource.ParentId)
d.Set("path_part", resource.PathPart)
return nil
}
func resourceAwsApiGatewayResourceUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation {
operations := make([]*apigateway.PatchOperation, 0)
if d.HasChange("path_part") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/pathPart"),
Value: aws.String(d.Get("path_part").(string)),
})
}
if d.HasChange("parent_id") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/parentId"),
Value: aws.String(d.Get("parent_id").(string)),
})
}
return operations
}
func resourceAwsApiGatewayResourceUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Updating API Gateway Resource %s", d.Id())
_, err := conn.UpdateResource(&apigateway.UpdateResourceInput{
ResourceId: aws.String(d.Id()),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
PatchOperations: resourceAwsApiGatewayResourceUpdateOperations(d),
})
if err != nil {
return err
}
return resourceAwsApiGatewayResourceRead(d, meta)
}
func resourceAwsApiGatewayResourceDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Deleting API Gateway Resource: %s", d.Id())
return resource.Retry(5*time.Minute, func() error {
log.Printf("[DEBUG] schema is %#v", d)
_, err := conn.DeleteResource(&apigateway.DeleteResourceInput{
ResourceId: aws.String(d.Id()),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
})
if err == nil {
return nil
}
if apigatewayErr, ok := err.(awserr.Error); ok && apigatewayErr.Code() == "NotFoundException" {
return nil
}
return resource.RetryError{Err: err}
})
}

View File

@ -0,0 +1,121 @@
package aws
import (
"fmt"
"testing"
"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/terraform"
)
func TestAccAWSAPIGatewayResource_basic(t *testing.T) {
var conf apigateway.Resource
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSAPIGatewayResourceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayResourceExists("aws_api_gateway_resource.test", &conf),
testAccCheckAWSAPIGatewayResourceAttributes(&conf),
resource.TestCheckResourceAttr(
"aws_api_gateway_resource.test", "path_part", "test"),
),
},
},
})
}
func testAccCheckAWSAPIGatewayResourceAttributes(conf *apigateway.Resource) resource.TestCheckFunc {
return func(s *terraform.State) error {
if *conf.Path != "/test" {
return fmt.Errorf("Wrong Path: %q", conf.Path)
}
return nil
}
}
func testAccCheckAWSAPIGatewayResourceExists(n string, res *apigateway.Resource) 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 Resource ID is set")
}
conn := testAccProvider.Meta().(*AWSClient).apigateway
req := &apigateway.GetResourceInput{
ResourceId: aws.String(rs.Primary.ID),
RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
}
describe, err := conn.GetResource(req)
if err != nil {
return err
}
if *describe.Id != rs.Primary.ID {
return fmt.Errorf("APIGateway Resource not found")
}
*res = *describe
return nil
}
}
func testAccCheckAWSAPIGatewayResourceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).apigateway
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_api_gateway_resource" {
continue
}
req := &apigateway.GetResourcesInput{
RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
}
describe, err := conn.GetResources(req)
if err == nil {
if len(describe.Items) != 0 &&
*describe.Items[0].Id == rs.Primary.ID {
return fmt.Errorf("API Gateway Resource still exists")
}
}
aws2err, ok := err.(awserr.Error)
if !ok {
return err
}
if aws2err.Code() != "NotFoundException" {
return err
}
return nil
}
return nil
}
const testAccAWSAPIGatewayResourceConfig = `
resource "aws_api_gateway_rest_api" "test" {
name = "test"
}
resource "aws_api_gateway_resource" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
path_part = "test"
}
`

View File

@ -0,0 +1,40 @@
---
layout: "aws"
page_title: "AWS: aws_api_gateway_resource"
sidebar_current: "docs-aws-resource-api-gateway-resource"
description: |-
Provides an API Gateway Resource.
---
# aws\_api\_gateway\_resource
Provides an API Gateway REST API Resource.
## Example Usage
```
resource "aws_api_gateway_rest_api" "MyDemoAPI" {
name = "MyDemoAPI"
description = "This is my API for demonstration purposes"
}
resource "aws_api_gateway_resource" "MyDemoResource" {
rest_api_id = "${aws_api_gateway_rest_api.MyDemoAPI.id}"
parent_resource_id = "${aws_api_gateway_rest_api.MyDemoAPI.root_resource_id}"
path_part = "mydemoresource"
}
```
## Argument Reference
The following arguments are supported:
* `rest_api_id` - (Required) API Gateway ID
* `parent_resource_id` - (Required) Parent resource ID
* `path_part` - (Required) The resource path
## Attributes Reference
The following attributes are exported:
* `path` - The complete path for this resource, including all parent paths

View File

@ -16,6 +16,9 @@
<li<%= sidebar_current("docs-aws-resource-api-gateway-rest-api") %>>
<a href="/docs/providers/aws/r/api_gateway_rest_api.html">aws_api_gateway_rest_api</a>
</li>
<li<%= sidebar_current("docs-aws-resource-api-gateway-resource") %>>
<a href="/docs/providers/aws/r/api_gateway_resource.html">aws_api_gateway_resource</a>
</li>
</ul>
</li>