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