github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/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  	d.Set("path", resource.Path)
    88  
    89  	return nil
    90  }
    91  
    92  func resourceAwsApiGatewayResourceUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation {
    93  	operations := make([]*apigateway.PatchOperation, 0)
    94  	if d.HasChange("path_part") {
    95  		operations = append(operations, &apigateway.PatchOperation{
    96  			Op:    aws.String("replace"),
    97  			Path:  aws.String("/pathPart"),
    98  			Value: aws.String(d.Get("path_part").(string)),
    99  		})
   100  	}
   101  
   102  	if d.HasChange("parent_id") {
   103  		operations = append(operations, &apigateway.PatchOperation{
   104  			Op:    aws.String("replace"),
   105  			Path:  aws.String("/parentId"),
   106  			Value: aws.String(d.Get("parent_id").(string)),
   107  		})
   108  	}
   109  	return operations
   110  }
   111  
   112  func resourceAwsApiGatewayResourceUpdate(d *schema.ResourceData, meta interface{}) error {
   113  	conn := meta.(*AWSClient).apigateway
   114  
   115  	log.Printf("[DEBUG] Updating API Gateway Resource %s", d.Id())
   116  	_, err := conn.UpdateResource(&apigateway.UpdateResourceInput{
   117  		ResourceId:      aws.String(d.Id()),
   118  		RestApiId:       aws.String(d.Get("rest_api_id").(string)),
   119  		PatchOperations: resourceAwsApiGatewayResourceUpdateOperations(d),
   120  	})
   121  
   122  	if err != nil {
   123  		return err
   124  	}
   125  
   126  	return resourceAwsApiGatewayResourceRead(d, meta)
   127  }
   128  
   129  func resourceAwsApiGatewayResourceDelete(d *schema.ResourceData, meta interface{}) error {
   130  	conn := meta.(*AWSClient).apigateway
   131  	log.Printf("[DEBUG] Deleting API Gateway Resource: %s", d.Id())
   132  
   133  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   134  		log.Printf("[DEBUG] schema is %#v", d)
   135  		_, err := conn.DeleteResource(&apigateway.DeleteResourceInput{
   136  			ResourceId: aws.String(d.Id()),
   137  			RestApiId:  aws.String(d.Get("rest_api_id").(string)),
   138  		})
   139  		if err == nil {
   140  			return nil
   141  		}
   142  
   143  		if apigatewayErr, ok := err.(awserr.Error); ok && apigatewayErr.Code() == "NotFoundException" {
   144  			return nil
   145  		}
   146  
   147  		return resource.NonRetryableError(err)
   148  	})
   149  }