github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_api_gateway_method_response.go (about)

     1  package aws
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"log"
     7  	"strconv"
     8  	"time"
     9  
    10  	"github.com/aws/aws-sdk-go/aws"
    11  	"github.com/aws/aws-sdk-go/aws/awserr"
    12  	"github.com/aws/aws-sdk-go/service/apigateway"
    13  	"github.com/hashicorp/terraform/helper/resource"
    14  	"github.com/hashicorp/terraform/helper/schema"
    15  )
    16  
    17  func resourceAwsApiGatewayMethodResponse() *schema.Resource {
    18  	return &schema.Resource{
    19  		Create: resourceAwsApiGatewayMethodResponseCreate,
    20  		Read:   resourceAwsApiGatewayMethodResponseRead,
    21  		Update: resourceAwsApiGatewayMethodResponseUpdate,
    22  		Delete: resourceAwsApiGatewayMethodResponseDelete,
    23  
    24  		Schema: map[string]*schema.Schema{
    25  			"rest_api_id": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"resource_id": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  				ForceNew: true,
    35  			},
    36  
    37  			"http_method": &schema.Schema{
    38  				Type:         schema.TypeString,
    39  				Required:     true,
    40  				ForceNew:     true,
    41  				ValidateFunc: validateHTTPMethod,
    42  			},
    43  
    44  			"status_code": &schema.Schema{
    45  				Type:     schema.TypeString,
    46  				Required: true,
    47  			},
    48  
    49  			"response_models": &schema.Schema{
    50  				Type:     schema.TypeMap,
    51  				Optional: true,
    52  				Elem:     schema.TypeString,
    53  			},
    54  
    55  			"response_parameters": &schema.Schema{
    56  				Type:          schema.TypeMap,
    57  				Elem:          schema.TypeBool,
    58  				Optional:      true,
    59  				ConflictsWith: []string{"response_parameters_in_json"},
    60  			},
    61  
    62  			"response_parameters_in_json": &schema.Schema{
    63  				Type:          schema.TypeString,
    64  				Optional:      true,
    65  				ConflictsWith: []string{"response_parameters"},
    66  				Deprecated:    "Use field response_parameters instead",
    67  			},
    68  		},
    69  	}
    70  }
    71  
    72  func resourceAwsApiGatewayMethodResponseCreate(d *schema.ResourceData, meta interface{}) error {
    73  	conn := meta.(*AWSClient).apigateway
    74  
    75  	models := make(map[string]string)
    76  	for k, v := range d.Get("response_models").(map[string]interface{}) {
    77  		models[k] = v.(string)
    78  	}
    79  
    80  	parameters := make(map[string]bool)
    81  	if kv, ok := d.GetOk("response_parameters"); ok {
    82  		for k, v := range kv.(map[string]interface{}) {
    83  			parameters[k], ok = v.(bool)
    84  			if !ok {
    85  				value, _ := strconv.ParseBool(v.(string))
    86  				parameters[k] = value
    87  			}
    88  		}
    89  	}
    90  	if v, ok := d.GetOk("response_parameters_in_json"); ok {
    91  		if err := json.Unmarshal([]byte(v.(string)), &parameters); err != nil {
    92  			return fmt.Errorf("Error unmarshaling request_parameters_in_json: %s", err)
    93  		}
    94  	}
    95  
    96  	_, err := conn.PutMethodResponse(&apigateway.PutMethodResponseInput{
    97  		HttpMethod:         aws.String(d.Get("http_method").(string)),
    98  		ResourceId:         aws.String(d.Get("resource_id").(string)),
    99  		RestApiId:          aws.String(d.Get("rest_api_id").(string)),
   100  		StatusCode:         aws.String(d.Get("status_code").(string)),
   101  		ResponseModels:     aws.StringMap(models),
   102  		ResponseParameters: aws.BoolMap(parameters),
   103  	})
   104  	if err != nil {
   105  		return fmt.Errorf("Error creating API Gateway Method Response: %s", err)
   106  	}
   107  
   108  	d.SetId(fmt.Sprintf("agmr-%s-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string), d.Get("status_code").(string)))
   109  	log.Printf("[DEBUG] API Gateway Method ID: %s", d.Id())
   110  
   111  	return nil
   112  }
   113  
   114  func resourceAwsApiGatewayMethodResponseRead(d *schema.ResourceData, meta interface{}) error {
   115  	conn := meta.(*AWSClient).apigateway
   116  
   117  	log.Printf("[DEBUG] Reading API Gateway Method %s", d.Id())
   118  	methodResponse, err := conn.GetMethodResponse(&apigateway.GetMethodResponseInput{
   119  		HttpMethod: aws.String(d.Get("http_method").(string)),
   120  		ResourceId: aws.String(d.Get("resource_id").(string)),
   121  		RestApiId:  aws.String(d.Get("rest_api_id").(string)),
   122  		StatusCode: aws.String(d.Get("status_code").(string)),
   123  	})
   124  	if err != nil {
   125  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
   126  			d.SetId("")
   127  			return nil
   128  		}
   129  		return err
   130  	}
   131  
   132  	log.Printf("[DEBUG] Received API Gateway Method: %s", methodResponse)
   133  	d.Set("response_models", aws.StringValueMap(methodResponse.ResponseModels))
   134  	d.Set("response_parameters", aws.BoolValueMap(methodResponse.ResponseParameters))
   135  	d.Set("response_parameters_in_json", aws.BoolValueMap(methodResponse.ResponseParameters))
   136  	d.SetId(fmt.Sprintf("agmr-%s-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string), d.Get("status_code").(string)))
   137  
   138  	return nil
   139  }
   140  
   141  func resourceAwsApiGatewayMethodResponseUpdate(d *schema.ResourceData, meta interface{}) error {
   142  	conn := meta.(*AWSClient).apigateway
   143  
   144  	log.Printf("[DEBUG] Updating API Gateway Method Response %s", d.Id())
   145  	operations := make([]*apigateway.PatchOperation, 0)
   146  
   147  	if d.HasChange("response_models") {
   148  		operations = append(operations, expandApiGatewayRequestResponseModelOperations(d, "response_models", "responseModels")...)
   149  	}
   150  
   151  	if d.HasChange("response_parameters_in_json") {
   152  		ops, err := deprecatedExpandApiGatewayMethodParametersJSONOperations(d, "response_parameters_in_json", "responseParameters")
   153  		if err != nil {
   154  			return err
   155  		}
   156  		operations = append(operations, ops...)
   157  	}
   158  
   159  	if d.HasChange("response_parameters") {
   160  		ops, err := expandApiGatewayMethodParametersOperations(d, "response_parameters", "responseParameters")
   161  		if err != nil {
   162  			return err
   163  		}
   164  		operations = append(operations, ops...)
   165  	}
   166  
   167  	out, err := conn.UpdateMethodResponse(&apigateway.UpdateMethodResponseInput{
   168  		HttpMethod:      aws.String(d.Get("http_method").(string)),
   169  		ResourceId:      aws.String(d.Get("resource_id").(string)),
   170  		RestApiId:       aws.String(d.Get("rest_api_id").(string)),
   171  		StatusCode:      aws.String(d.Get("status_code").(string)),
   172  		PatchOperations: operations,
   173  	})
   174  
   175  	if err != nil {
   176  		return err
   177  	}
   178  
   179  	log.Printf("[DEBUG] Received API Gateway Method Response: %s", out)
   180  
   181  	return resourceAwsApiGatewayMethodResponseRead(d, meta)
   182  }
   183  
   184  func resourceAwsApiGatewayMethodResponseDelete(d *schema.ResourceData, meta interface{}) error {
   185  	conn := meta.(*AWSClient).apigateway
   186  	log.Printf("[DEBUG] Deleting API Gateway Method Response: %s", d.Id())
   187  
   188  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   189  		_, err := conn.DeleteMethodResponse(&apigateway.DeleteMethodResponseInput{
   190  			HttpMethod: aws.String(d.Get("http_method").(string)),
   191  			ResourceId: aws.String(d.Get("resource_id").(string)),
   192  			RestApiId:  aws.String(d.Get("rest_api_id").(string)),
   193  			StatusCode: aws.String(d.Get("status_code").(string)),
   194  		})
   195  		if err == nil {
   196  			return nil
   197  		}
   198  
   199  		apigatewayErr, ok := err.(awserr.Error)
   200  		if apigatewayErr.Code() == "NotFoundException" {
   201  			return nil
   202  		}
   203  
   204  		if !ok {
   205  			return resource.NonRetryableError(err)
   206  		}
   207  
   208  		return resource.NonRetryableError(err)
   209  	})
   210  }