2016-03-05 23:13:38 +01:00
|
|
|
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{
|
2016-10-23 15:08:41 +02:00
|
|
|
"name": {
|
2016-03-05 23:13:38 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
2016-10-23 15:08:41 +02:00
|
|
|
"description": {
|
2016-03-05 23:13:38 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2016-12-16 03:14:29 +01:00
|
|
|
"binary_media_types": {
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
|
|
|
|
2016-10-23 15:08:41 +02:00
|
|
|
"root_resource_id": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"created_date": {
|
2016-03-05 23:13:38 +01:00
|
|
|
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))
|
|
|
|
}
|
2016-12-16 03:14:29 +01:00
|
|
|
|
|
|
|
params := &apigateway.CreateRestApiInput{
|
2016-03-05 23:13:38 +01:00
|
|
|
Name: aws.String(d.Get("name").(string)),
|
|
|
|
Description: description,
|
2016-12-16 03:14:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
binaryMediaTypes, binaryMediaTypesOk := d.GetOk("binary_media_types")
|
|
|
|
if binaryMediaTypesOk {
|
|
|
|
params.BinaryMediaTypes = expandStringList(binaryMediaTypes.([]interface{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
gateway, err := conn.CreateRestApi(params)
|
2016-03-05 23:13:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating API Gateway: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(*gateway.Id)
|
|
|
|
|
2016-10-23 15:08:41 +02:00
|
|
|
if err = resourceAwsApiGatewayRestApiRefreshResources(d, meta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceAwsApiGatewayRestApiRead(d, meta)
|
2016-03-05 23:13:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2016-03-06 10:13:06 +01:00
|
|
|
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-05 23:13:38 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("name", api.Name)
|
|
|
|
d.Set("description", api.Description)
|
2016-12-16 03:14:29 +01:00
|
|
|
d.Set("binary_media_types", api.BinaryMediaTypes)
|
2016-03-05 23:13:38 +01:00
|
|
|
|
2016-10-23 15:08:41 +02:00
|
|
|
if err := d.Set("created_date", api.CreatedDate.Format(time.RFC3339)); err != nil {
|
|
|
|
log.Printf("[DEBUG] Error setting created_date: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-03-05 23:13:38 +01:00
|
|
|
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())
|
|
|
|
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.Retry(5*time.Minute, func() *resource.RetryError {
|
2016-03-05 23:13:38 +01:00
|
|
|
_, 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
|
|
|
|
}
|
|
|
|
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.NonRetryableError(err)
|
2016-03-05 23:13:38 +01:00
|
|
|
})
|
|
|
|
}
|