github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_api_gateway_resource.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 resourceAwsApiGatewayResource() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsApiGatewayResourceCreate,
    18  		Read:   resourceAwsApiGatewayResourceRead,
    19  		Update: resourceAwsApiGatewayResourceUpdate,
    20  		Delete: resourceAwsApiGatewayResourceDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"rest_api_id": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  
    29  			"parent_id": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Required: true,
    32  			},
    33  
    34  			"path_part": &schema.Schema{
    35  				Type:     schema.TypeString,
    36  				Required: true,
    37  			},
    38  
    39  			"path": &schema.Schema{
    40  				Type:     schema.TypeString,
    41  				Computed: true,
    42  			},
    43  		},
    44  	}
    45  }
    46  
    47  func resourceAwsApiGatewayResourceCreate(d *schema.ResourceData, meta interface{}) error {
    48  	conn := meta.(*AWSClient).apigateway
    49  	log.Printf("[DEBUG] Creating API Gateway Resource for API %s", d.Get("rest_api_id").(string))
    50  
    51  	var err error
    52  	resource, err := conn.CreateResource(&apigateway.CreateResourceInput{
    53  		ParentId:  aws.String(d.Get("parent_id").(string)),
    54  		PathPart:  aws.String(d.Get("path_part").(string)),
    55  		RestApiId: aws.String(d.Get("rest_api_id").(string)),
    56  	})
    57  
    58  	if err != nil {
    59  		return fmt.Errorf("Error creating API Gateway Resource: %s", err)
    60  	}
    61  
    62  	d.SetId(*resource.Id)
    63  	d.Set("path", resource.Path)
    64  
    65  	return nil
    66  }
    67  
    68  func resourceAwsApiGatewayResourceRead(d *schema.ResourceData, meta interface{}) error {
    69  	conn := meta.(*AWSClient).apigateway
    70  
    71  	log.Printf("[DEBUG] Reading API Gateway Resource %s", d.Id())
    72  	resource, err := conn.GetResource(&apigateway.GetResourceInput{
    73  		ResourceId: aws.String(d.Id()),
    74  		RestApiId:  aws.String(d.Get("rest_api_id").(string)),
    75  	})
    76  
    77  	if err != nil {
    78  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
    79  			d.SetId("")
    80  			return nil
    81  		}
    82  		return err
    83  	}
    84  
    85  	d.Set("parent_id", resource.ParentId)
    86  	d.Set("path_part", resource.PathPart)
    87  
    88  	return nil
    89  }
    90  
    91  func resourceAwsApiGatewayResourceUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation {
    92  	operations := make([]*apigateway.PatchOperation, 0)
    93  	if d.HasChange("path_part") {
    94  		operations = append(operations, &apigateway.PatchOperation{
    95  			Op:    aws.String("replace"),
    96  			Path:  aws.String("/pathPart"),
    97  			Value: aws.String(d.Get("path_part").(string)),
    98  		})
    99  	}
   100  
   101  	if d.HasChange("parent_id") {
   102  		operations = append(operations, &apigateway.PatchOperation{
   103  			Op:    aws.String("replace"),
   104  			Path:  aws.String("/parentId"),
   105  			Value: aws.String(d.Get("parent_id").(string)),
   106  		})
   107  	}
   108  	return operations
   109  }
   110  
   111  func resourceAwsApiGatewayResourceUpdate(d *schema.ResourceData, meta interface{}) error {
   112  	conn := meta.(*AWSClient).apigateway
   113  
   114  	log.Printf("[DEBUG] Updating API Gateway Resource %s", d.Id())
   115  	_, err := conn.UpdateResource(&apigateway.UpdateResourceInput{
   116  		ResourceId:      aws.String(d.Id()),
   117  		RestApiId:       aws.String(d.Get("rest_api_id").(string)),
   118  		PatchOperations: resourceAwsApiGatewayResourceUpdateOperations(d),
   119  	})
   120  
   121  	if err != nil {
   122  		return err
   123  	}
   124  
   125  	return resourceAwsApiGatewayResourceRead(d, meta)
   126  }
   127  
   128  func resourceAwsApiGatewayResourceDelete(d *schema.ResourceData, meta interface{}) error {
   129  	conn := meta.(*AWSClient).apigateway
   130  	log.Printf("[DEBUG] Deleting API Gateway Resource: %s", d.Id())
   131  
   132  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   133  		log.Printf("[DEBUG] schema is %#v", d)
   134  		_, err := conn.DeleteResource(&apigateway.DeleteResourceInput{
   135  			ResourceId: aws.String(d.Id()),
   136  			RestApiId:  aws.String(d.Get("rest_api_id").(string)),
   137  		})
   138  		if err == nil {
   139  			return nil
   140  		}
   141  
   142  		if apigatewayErr, ok := err.(awserr.Error); ok && apigatewayErr.Code() == "NotFoundException" {
   143  			return nil
   144  		}
   145  
   146  		return resource.NonRetryableError(err)
   147  	})
   148  }