github.com/tam7t/terraform@v0.7.0-rc2.0.20160705125922-be2469a05c5e/builtin/providers/aws/resource_aws_api_gateway_rest_api.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/apigateway"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsApiGatewayRestApi() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsApiGatewayRestApiCreate,
    18  		Read:   resourceAwsApiGatewayRestApiRead,
    19  		Update: resourceAwsApiGatewayRestApiUpdate,
    20  		Delete: resourceAwsApiGatewayRestApiDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"name": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  			},
    27  
    28  			"description": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Optional: true,
    31  			},
    32  
    33  			"root_resource_id": &schema.Schema{
    34  				Type:     schema.TypeString,
    35  				Computed: true,
    36  			},
    37  		},
    38  	}
    39  }
    40  
    41  func resourceAwsApiGatewayRestApiCreate(d *schema.ResourceData, meta interface{}) error {
    42  	conn := meta.(*AWSClient).apigateway
    43  	log.Printf("[DEBUG] Creating API Gateway")
    44  
    45  	var description *string
    46  	if d.Get("description").(string) != "" {
    47  		description = aws.String(d.Get("description").(string))
    48  	}
    49  	gateway, err := conn.CreateRestApi(&apigateway.CreateRestApiInput{
    50  		Name:        aws.String(d.Get("name").(string)),
    51  		Description: description,
    52  	})
    53  	if err != nil {
    54  		return fmt.Errorf("Error creating API Gateway: %s", err)
    55  	}
    56  
    57  	d.SetId(*gateway.Id)
    58  
    59  	return resourceAwsApiGatewayRestApiRefreshResources(d, meta)
    60  }
    61  
    62  func resourceAwsApiGatewayRestApiRefreshResources(d *schema.ResourceData, meta interface{}) error {
    63  	conn := meta.(*AWSClient).apigateway
    64  
    65  	resp, err := conn.GetResources(&apigateway.GetResourcesInput{
    66  		RestApiId: aws.String(d.Id()),
    67  	})
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	for _, item := range resp.Items {
    73  		if *item.Path == "/" {
    74  			d.Set("root_resource_id", item.Id)
    75  			break
    76  		}
    77  	}
    78  
    79  	return nil
    80  }
    81  
    82  func resourceAwsApiGatewayRestApiRead(d *schema.ResourceData, meta interface{}) error {
    83  	conn := meta.(*AWSClient).apigateway
    84  	log.Printf("[DEBUG] Reading API Gateway %s", d.Id())
    85  
    86  	api, err := conn.GetRestApi(&apigateway.GetRestApiInput{
    87  		RestApiId: aws.String(d.Id()),
    88  	})
    89  	if err != nil {
    90  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
    91  			d.SetId("")
    92  			return nil
    93  		}
    94  		return err
    95  	}
    96  
    97  	d.SetId(*api.Id)
    98  	d.Set("name", api.Name)
    99  	d.Set("description", api.Description)
   100  
   101  	return nil
   102  }
   103  
   104  func resourceAwsApiGatewayRestApiUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation {
   105  	operations := make([]*apigateway.PatchOperation, 0)
   106  
   107  	if d.HasChange("name") {
   108  		operations = append(operations, &apigateway.PatchOperation{
   109  			Op:    aws.String("replace"),
   110  			Path:  aws.String("/name"),
   111  			Value: aws.String(d.Get("name").(string)),
   112  		})
   113  	}
   114  
   115  	if d.HasChange("description") {
   116  		operations = append(operations, &apigateway.PatchOperation{
   117  			Op:    aws.String("replace"),
   118  			Path:  aws.String("/description"),
   119  			Value: aws.String(d.Get("description").(string)),
   120  		})
   121  	}
   122  
   123  	return operations
   124  }
   125  
   126  func resourceAwsApiGatewayRestApiUpdate(d *schema.ResourceData, meta interface{}) error {
   127  	conn := meta.(*AWSClient).apigateway
   128  	log.Printf("[DEBUG] Updating API Gateway %s", d.Id())
   129  
   130  	_, err := conn.UpdateRestApi(&apigateway.UpdateRestApiInput{
   131  		RestApiId:       aws.String(d.Id()),
   132  		PatchOperations: resourceAwsApiGatewayRestApiUpdateOperations(d),
   133  	})
   134  
   135  	if err != nil {
   136  		return err
   137  	}
   138  	log.Printf("[DEBUG] Updated API Gateway %s", d.Id())
   139  
   140  	return resourceAwsApiGatewayRestApiRead(d, meta)
   141  }
   142  
   143  func resourceAwsApiGatewayRestApiDelete(d *schema.ResourceData, meta interface{}) error {
   144  	conn := meta.(*AWSClient).apigateway
   145  	log.Printf("[DEBUG] Deleting API Gateway: %s", d.Id())
   146  
   147  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   148  		_, err := conn.DeleteRestApi(&apigateway.DeleteRestApiInput{
   149  			RestApiId: aws.String(d.Id()),
   150  		})
   151  		if err == nil {
   152  			return nil
   153  		}
   154  
   155  		if apigatewayErr, ok := err.(awserr.Error); ok && apigatewayErr.Code() == "NotFoundException" {
   156  			return nil
   157  		}
   158  
   159  		return resource.NonRetryableError(err)
   160  	})
   161  }