github.com/keshavdv/terraform@v0.7.0-rc2.0.20160711232630-d69256dcb425/builtin/providers/aws/resource_aws_api_gateway_method.go (about)

     1  package aws
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"log"
     7  	"time"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/aws/awserr"
    11  	"github.com/aws/aws-sdk-go/service/apigateway"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/helper/schema"
    14  )
    15  
    16  func resourceAwsApiGatewayMethod() *schema.Resource {
    17  	return &schema.Resource{
    18  		Create: resourceAwsApiGatewayMethodCreate,
    19  		Read:   resourceAwsApiGatewayMethodRead,
    20  		Update: resourceAwsApiGatewayMethodUpdate,
    21  		Delete: resourceAwsApiGatewayMethodDelete,
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"rest_api_id": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  				ForceNew: true,
    28  			},
    29  
    30  			"resource_id": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  				ForceNew: true,
    34  			},
    35  
    36  			"http_method": &schema.Schema{
    37  				Type:         schema.TypeString,
    38  				Required:     true,
    39  				ForceNew:     true,
    40  				ValidateFunc: validateHTTPMethod,
    41  			},
    42  
    43  			"authorization": &schema.Schema{
    44  				Type:     schema.TypeString,
    45  				Required: true,
    46  			},
    47  
    48  			"api_key_required": &schema.Schema{
    49  				Type:     schema.TypeBool,
    50  				Optional: true,
    51  				Default:  false,
    52  			},
    53  
    54  			"request_models": &schema.Schema{
    55  				Type:     schema.TypeMap,
    56  				Optional: true,
    57  				Elem:     schema.TypeString,
    58  			},
    59  
    60  			"request_parameters_in_json": &schema.Schema{
    61  				Type:     schema.TypeString,
    62  				Optional: true,
    63  			},
    64  		},
    65  	}
    66  }
    67  
    68  func resourceAwsApiGatewayMethodCreate(d *schema.ResourceData, meta interface{}) error {
    69  	conn := meta.(*AWSClient).apigateway
    70  
    71  	models := make(map[string]string)
    72  	for k, v := range d.Get("request_models").(map[string]interface{}) {
    73  		models[k] = v.(string)
    74  	}
    75  
    76  	parameters := make(map[string]bool)
    77  	if v, ok := d.GetOk("request_parameters_in_json"); ok {
    78  		if err := json.Unmarshal([]byte(v.(string)), &parameters); err != nil {
    79  			return fmt.Errorf("Error unmarshaling request_parameters_in_json: %s", err)
    80  		}
    81  	}
    82  
    83  	_, err := conn.PutMethod(&apigateway.PutMethodInput{
    84  		AuthorizationType: aws.String(d.Get("authorization").(string)),
    85  		HttpMethod:        aws.String(d.Get("http_method").(string)),
    86  		ResourceId:        aws.String(d.Get("resource_id").(string)),
    87  		RestApiId:         aws.String(d.Get("rest_api_id").(string)),
    88  		RequestModels:     aws.StringMap(models),
    89  		// TODO reimplement once [GH-2143](https://github.com/hashicorp/terraform/issues/2143) has been implemented
    90  		RequestParameters: aws.BoolMap(parameters),
    91  		ApiKeyRequired:    aws.Bool(d.Get("api_key_required").(bool)),
    92  	})
    93  	if err != nil {
    94  		return fmt.Errorf("Error creating API Gateway Method: %s", err)
    95  	}
    96  
    97  	d.SetId(fmt.Sprintf("agm-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string)))
    98  	log.Printf("[DEBUG] API Gateway Method ID: %s", d.Id())
    99  
   100  	return nil
   101  }
   102  
   103  func resourceAwsApiGatewayMethodRead(d *schema.ResourceData, meta interface{}) error {
   104  	conn := meta.(*AWSClient).apigateway
   105  
   106  	log.Printf("[DEBUG] Reading API Gateway Method %s", d.Id())
   107  	out, err := conn.GetMethod(&apigateway.GetMethodInput{
   108  		HttpMethod: aws.String(d.Get("http_method").(string)),
   109  		ResourceId: aws.String(d.Get("resource_id").(string)),
   110  		RestApiId:  aws.String(d.Get("rest_api_id").(string)),
   111  	})
   112  	if err != nil {
   113  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
   114  			d.SetId("")
   115  			return nil
   116  		}
   117  		return err
   118  	}
   119  	log.Printf("[DEBUG] Received API Gateway Method: %s", out)
   120  	d.SetId(fmt.Sprintf("agm-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string)))
   121  	d.Set("request_parameters_in_json", aws.BoolValueMap(out.RequestParameters))
   122  
   123  	return nil
   124  }
   125  
   126  func resourceAwsApiGatewayMethodUpdate(d *schema.ResourceData, meta interface{}) error {
   127  	conn := meta.(*AWSClient).apigateway
   128  
   129  	log.Printf("[DEBUG] Reading API Gateway Method %s", d.Id())
   130  	operations := make([]*apigateway.PatchOperation, 0)
   131  	if d.HasChange("resource_id") {
   132  		operations = append(operations, &apigateway.PatchOperation{
   133  			Op:    aws.String("replace"),
   134  			Path:  aws.String("/resourceId"),
   135  			Value: aws.String(d.Get("resource_id").(string)),
   136  		})
   137  	}
   138  
   139  	if d.HasChange("request_models") {
   140  		operations = append(operations, expandApiGatewayRequestResponseModelOperations(d, "request_models", "requestModels")...)
   141  	}
   142  
   143  	if d.HasChange("request_parameters_in_json") {
   144  		ops, err := expandApiGatewayMethodParametersJSONOperations(d, "request_parameters_in_json", "requestParameters")
   145  		if err != nil {
   146  			return err
   147  		}
   148  		operations = append(operations, ops...)
   149  	}
   150  
   151  	method, err := conn.UpdateMethod(&apigateway.UpdateMethodInput{
   152  		HttpMethod:      aws.String(d.Get("http_method").(string)),
   153  		ResourceId:      aws.String(d.Get("resource_id").(string)),
   154  		RestApiId:       aws.String(d.Get("rest_api_id").(string)),
   155  		PatchOperations: operations,
   156  	})
   157  
   158  	if err != nil {
   159  		return err
   160  	}
   161  
   162  	log.Printf("[DEBUG] Received API Gateway Method: %s", method)
   163  
   164  	return resourceAwsApiGatewayMethodRead(d, meta)
   165  }
   166  
   167  func resourceAwsApiGatewayMethodDelete(d *schema.ResourceData, meta interface{}) error {
   168  	conn := meta.(*AWSClient).apigateway
   169  	log.Printf("[DEBUG] Deleting API Gateway Method: %s", d.Id())
   170  
   171  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   172  		_, err := conn.DeleteMethod(&apigateway.DeleteMethodInput{
   173  			HttpMethod: aws.String(d.Get("http_method").(string)),
   174  			ResourceId: aws.String(d.Get("resource_id").(string)),
   175  			RestApiId:  aws.String(d.Get("rest_api_id").(string)),
   176  		})
   177  		if err == nil {
   178  			return nil
   179  		}
   180  
   181  		apigatewayErr, ok := err.(awserr.Error)
   182  		if apigatewayErr.Code() == "NotFoundException" {
   183  			return nil
   184  		}
   185  
   186  		if !ok {
   187  			return resource.NonRetryableError(err)
   188  		}
   189  
   190  		return resource.NonRetryableError(err)
   191  	})
   192  }