Add aws_api_gateway_model resource

This commit is contained in:
Raphael Randschau 2016-03-05 23:17:55 +01:00
parent 4da8b3d03a
commit b4c99f1009
5 changed files with 350 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_model": resourceAwsApiGatewayModel(),
"aws_api_gateway_resource": resourceAwsApiGatewayResource(),
"aws_api_gateway_method": resourceAwsApiGatewayMethod(),
"aws_api_gateway_method_response": resourceAwsApiGatewayMethodResponse(),

View File

@ -0,0 +1,168 @@
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 resourceAwsApiGatewayModel() *schema.Resource {
return &schema.Resource{
Create: resourceAwsApiGatewayModelCreate,
Read: resourceAwsApiGatewayModelRead,
Update: resourceAwsApiGatewayModelUpdate,
Delete: resourceAwsApiGatewayModelDelete,
Schema: map[string]*schema.Schema{
"rest_api_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"schema": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"content_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAwsApiGatewayModelCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Creating API Gateway Model")
var description *string
if v, ok := d.GetOk("description"); ok {
description = aws.String(v.(string))
}
var schema *string
if v, ok := d.GetOk("schema"); ok {
schema = aws.String(v.(string))
}
var err error
model, err := conn.CreateModel(&apigateway.CreateModelInput{
Name: aws.String(d.Get("name").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
ContentType: aws.String(d.Get("content_type").(string)),
Description: description,
Schema: schema,
})
if err != nil {
return fmt.Errorf("Error creating API Gateway Model: %s", err)
}
d.SetId(*model.Id)
return nil
}
func resourceAwsApiGatewayModelRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Reading API Gateway Model %s", d.Id())
out, err := conn.GetModel(&apigateway.GetModelInput{
ModelName: aws.String(d.Get("name").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
d.SetId("")
return nil
}
return err
}
log.Printf("[DEBUG] Received API Gateway Model: %s", out)
d.SetId(*out.Id)
d.Set("description", out.Description)
d.Set("schema", out.Schema)
d.Set("content_type", out.ContentType)
return nil
}
func resourceAwsApiGatewayModelUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Reading API Gateway Model %s", d.Id())
operations := make([]*apigateway.PatchOperation, 0)
if d.HasChange("description") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/description"),
Value: aws.String(d.Get("description").(string)),
})
}
if d.HasChange("schema") {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("replace"),
Path: aws.String("/schema"),
Value: aws.String(d.Get("schema").(string)),
})
}
out, err := conn.UpdateModel(&apigateway.UpdateModelInput{
ModelName: aws.String(d.Get("name").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
PatchOperations: operations,
})
if err != nil {
return err
}
log.Printf("[DEBUG] Received API Gateway Model: %s", out)
return resourceAwsApiGatewayModelRead(d, meta)
}
func resourceAwsApiGatewayModelDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
log.Printf("[DEBUG] Deleting API Gateway Model: %s", d.Id())
return resource.Retry(5*time.Minute, func() error {
log.Printf("[DEBUG] schema is %#v", d)
_, err := conn.DeleteModel(&apigateway.DeleteModelInput{
ModelName: aws.String(d.Get("name").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
})
if err == nil {
return nil
}
apigatewayErr, ok := err.(awserr.Error)
if apigatewayErr.Code() == "NotFoundException" {
return nil
}
if !ok {
return resource.RetryError{Err: err}
}
return resource.RetryError{Err: err}
})
}

View File

@ -0,0 +1,136 @@
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 TestAccAWSAPIGatewayModel_basic(t *testing.T) {
var conf apigateway.Model
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayModelDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSAPIGatewayModelConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayModelExists("aws_api_gateway_model.test", &conf),
testAccCheckAWSAPIGatewayModelAttributes(&conf),
resource.TestCheckResourceAttr(
"aws_api_gateway_model.test", "name", "test"),
resource.TestCheckResourceAttr(
"aws_api_gateway_model.test", "description", "a test schema"),
resource.TestCheckResourceAttr(
"aws_api_gateway_model.test", "content_type", "application/json"),
),
},
},
})
}
func testAccCheckAWSAPIGatewayModelAttributes(conf *apigateway.Model) resource.TestCheckFunc {
return func(s *terraform.State) error {
if *conf.Name != "test" {
return fmt.Errorf("Wrong Name: %q", *conf.Name)
}
if *conf.Description != "a test schema" {
return fmt.Errorf("Wrong Description: %q", *conf.Description)
}
if *conf.ContentType != "application/json" {
return fmt.Errorf("Wrong ContentType: %q", *conf.ContentType)
}
return nil
}
}
func testAccCheckAWSAPIGatewayModelExists(n string, res *apigateway.Model) 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 Model ID is set")
}
conn := testAccProvider.Meta().(*AWSClient).apigateway
req := &apigateway.GetModelInput{
ModelName: aws.String("test"),
RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
}
describe, err := conn.GetModel(req)
if err != nil {
return err
}
if *describe.Id != rs.Primary.ID {
return fmt.Errorf("APIGateway Model not found")
}
*res = *describe
return nil
}
}
func testAccCheckAWSAPIGatewayModelDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).apigateway
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_api_gateway_model" {
continue
}
req := &apigateway.GetModelsInput{
RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
}
describe, err := conn.GetModels(req)
if err == nil {
if len(describe.Items) != 0 &&
*describe.Items[0].Id == rs.Primary.ID {
return fmt.Errorf("API Gateway Model still exists")
}
}
aws2err, ok := err.(awserr.Error)
if !ok {
return err
}
if aws2err.Code() != "NotFoundException" {
return err
}
return nil
}
return nil
}
const testAccAWSAPIGatewayModelConfig = `
resource "aws_api_gateway_rest_api" "test" {
name = "test"
}
resource "aws_api_gateway_model" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
name = "test"
description = "a test schema"
content_type = "application/json"
schema = <<EOF
{
"type": "object"
}
EOF
}
`

View File

@ -0,0 +1,42 @@
---
layout: "aws"
page_title: "AWS: aws_api_gateway_model"
sidebar_current: "docs-aws-resource-api-gateway-model"
description: |-
Provides a Model for a API Gateway.
---
# aws\_api\_gateway\_model
Provides a Model for a API Gateway.
## Example Usage
```
resource "aws_api_gateway_rest_api" "MyDemoAPI" {
name = "MyDemoAPI"
description = "This is my API for demonstration purposes"
}
resource "aws_api_gateway_model" "MyDemoModel" {
rest_api_id = "${aws_api_gateway_rest_api.MyDemoAPI.id}"
name = "user"
description = "a JSON schema"
content_type = "application/json"
schema = <<EOF
{
"type": "object"
}
EOF
}
```
## Argument Reference
The following arguments are supported:
* `rest_api_id` - (Required) API Gateway ID
* `name` - (Required) Name of the model
* `description` - (Optional) Model description
* `content_type` - (Required) Model content type
* `schema` - (Required) Model schema

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-model") %>>
<a href="/docs/providers/aws/r/api_gateway_model.html">aws_api_gateway_model</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>