Add aws_api_gateway_rest_api resource

This commit is contained in:
Raphael Randschau 2016-03-05 23:13:38 +01:00
parent fdf0cfa66d
commit a73721d248
5 changed files with 335 additions and 0 deletions

View File

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

View File

@ -0,0 +1,157 @@
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 resourceAwsApiGatewayRestApi() *schema.Resource {
return &schema.Resource{
Create: resourceAwsApiGatewayRestApiCreate,
Read: resourceAwsApiGatewayRestApiRead,
Update: resourceAwsApiGatewayRestApiUpdate,
Delete: resourceAwsApiGatewayRestApiDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"root_resource_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsApiGatewayRestApiCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Creating API Gateway")
var description *string
if d.Get("description").(string) != "" {
description = aws.String(d.Get("description").(string))
}
gateway, err := conn.CreateRestApi(&apigateway.CreateRestApiInput{
Name: aws.String(d.Get("name").(string)),
Description: description,
})
if err != nil {
return fmt.Errorf("Error creating API Gateway: %s", err)
}
d.SetId(*gateway.Id)
return resourceAwsApiGatewayRestApiRefreshResources(d, meta)
}
func resourceAwsApiGatewayRestApiRefreshResources(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
resp, err := conn.GetResources(&apigateway.GetResourcesInput{
RestApiId: aws.String(d.Id()),
})
if err != nil {
return err
}
for _, item := range resp.Items {
if *item.Path == "/" {
d.Set("root_resource_id", item.Id)
break
}
}
return nil
}
func resourceAwsApiGatewayRestApiRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Reading API Gateway %s", d.Id())
api, err := conn.GetRestApi(&apigateway.GetRestApiInput{
RestApiId: aws.String(d.Id()),
})
if err != nil {
return err
}
d.SetId(*api.Id)
d.Set("name", api.Name)
d.Set("description", api.Description)
return nil
}
func resourceAwsApiGatewayRestApiUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation {
operations := make([]*apigateway.PatchOperation, 0)
if d.HasChange("name") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/name"),
Value: aws.String(d.Get("name").(string)),
})
}
if d.HasChange("description") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/description"),
Value: aws.String(d.Get("description").(string)),
})
}
return operations
}
func resourceAwsApiGatewayRestApiUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Updating API Gateway %s", d.Id())
_, err := conn.UpdateRestApi(&apigateway.UpdateRestApiInput{
RestApiId: aws.String(d.Id()),
PatchOperations: resourceAwsApiGatewayRestApiUpdateOperations(d),
})
if err != nil {
return err
}
log.Printf("[DEBUG] Updated API Gateway %s", d.Id())
return resourceAwsApiGatewayRestApiRead(d, meta)
}
func resourceAwsApiGatewayRestApiDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Deleting API Gateway: %s", d.Id())
return resource.Retry(5*time.Minute, func() error {
_, err := conn.DeleteRestApi(&apigateway.DeleteRestApiInput{
RestApiId: aws.String(d.Id()),
})
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,135 @@
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 TestAccAWSAPIGatewayRestApi_basic(t *testing.T) {
var conf apigateway.RestApi
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayRestAPIDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSAPIGatewayRestAPIConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf),
testAccCheckAWSAPIGatewayRestAPINameAttribute(&conf, "bar"),
resource.TestCheckResourceAttr(
"aws_api_gateway_rest_api.test", "name", "bar"),
resource.TestCheckResourceAttr(
"aws_api_gateway_rest_api.test", "description", ""),
),
},
resource.TestStep{
Config: testAccAWSAPIGatewayRestAPIUpdateConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayRestAPIExists("aws_api_gateway_rest_api.test", &conf),
testAccCheckAWSAPIGatewayRestAPINameAttribute(&conf, "test"),
testAccCheckAWSAPIGatewayRestAPIDescriptionAttribute(&conf, "test"),
resource.TestCheckResourceAttr(
"aws_api_gateway_rest_api.test", "name", "test"),
resource.TestCheckResourceAttr(
"aws_api_gateway_rest_api.test", "description", "test"),
),
},
},
})
}
func testAccCheckAWSAPIGatewayRestAPINameAttribute(conf *apigateway.RestApi, name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if *conf.Name != name {
return fmt.Errorf("Wrong Name: %q", *conf.Name)
}
return nil
}
}
func testAccCheckAWSAPIGatewayRestAPIDescriptionAttribute(conf *apigateway.RestApi, description string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if *conf.Description != description {
return fmt.Errorf("Wrong Description: %q", *conf.Description)
}
return nil
}
}
func testAccCheckAWSAPIGatewayRestAPIExists(n string, res *apigateway.RestApi) 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.GetRestApiInput{
RestApiId: aws.String(rs.Primary.ID),
}
describe, err := conn.GetRestApi(req)
if err != nil {
return err
}
if *describe.Id != rs.Primary.ID {
return fmt.Errorf("APIGateway not found")
}
*res = *describe
return nil
}
}
func testAccCheckAWSAPIGatewayRestAPIDestroy(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.GetRestApisInput{}
describe, err := conn.GetRestApis(req)
if err == nil {
if len(describe.Items) != 0 &&
*describe.Items[0].Id == rs.Primary.ID {
return fmt.Errorf("API Gateway still exists")
}
}
return err
}
return nil
}
const testAccAWSAPIGatewayRestAPIConfig = `
resource "aws_api_gateway_rest_api" "test" {
name = "bar"
}
`
const testAccAWSAPIGatewayRestAPIUpdateConfig = `
resource "aws_api_gateway_rest_api" "test" {
name = "test"
description = "test"
}
`

View File

@ -0,0 +1,33 @@
---
layout: "aws"
page_title: "AWS: aws_api_gateway_rest_api"
sidebar_current: "docs-aws-resource-api-gateway-rest-api"
description: |-
Provides an API Gateway REST API.
---
# aws\_api\_gateway\_rest\_api
Provides an API Gateway REST API.
## Example Usage
```
resource "aws_api_gateway_rest_api" "MyDemoAPI" {
name = "MyDemoAPI"
description = "This is my API for demonstration purposes"
}
```
## Argument Reference
The following arguments are supported:
* `name` - (Required) Name of the API Gateway
* `description` - (Optional) The API Gateway description
## Attributes Reference
The following attributes are exported:
* `root_resource_id` - The resource ID of the APIs root

View File

@ -10,6 +10,15 @@
<a href="/docs/providers/aws/index.html">AWS Provider</a>
</li>
<li<%= sidebar_current(/^docs-aws-resource-api-gateway/) %>>
<a href="#">APIGateway Resources</a>
<ul class="nav nav-visible">
<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>
</ul>
</li>
<li<%= sidebar_current(/^docs-aws-resource-cloudformation/) %>>
<a href="#">CloudFormation Resources</a>
<ul class="nav nav-visible">