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