github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_api_gateway_integration_response.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 resourceAwsApiGatewayIntegrationResponse() *schema.Resource {
    17  	return &schema.Resource{
    18  		Create: resourceAwsApiGatewayIntegrationResponseCreate,
    19  		Read:   resourceAwsApiGatewayIntegrationResponseRead,
    20  		Update: resourceAwsApiGatewayIntegrationResponseCreate,
    21  		Delete: resourceAwsApiGatewayIntegrationResponseDelete,
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"rest_api_id": {
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  				ForceNew: true,
    28  			},
    29  
    30  			"resource_id": {
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  				ForceNew: true,
    34  			},
    35  
    36  			"http_method": {
    37  				Type:         schema.TypeString,
    38  				Required:     true,
    39  				ForceNew:     true,
    40  				ValidateFunc: validateHTTPMethod,
    41  			},
    42  
    43  			"status_code": {
    44  				Type:     schema.TypeString,
    45  				Required: true,
    46  			},
    47  
    48  			"selection_pattern": {
    49  				Type:     schema.TypeString,
    50  				Optional: true,
    51  			},
    52  
    53  			"response_templates": {
    54  				Type:     schema.TypeMap,
    55  				Optional: true,
    56  				Elem:     schema.TypeString,
    57  			},
    58  
    59  			"response_parameters": {
    60  				Type:          schema.TypeMap,
    61  				Elem:          schema.TypeString,
    62  				Optional:      true,
    63  				ConflictsWith: []string{"response_parameters_in_json"},
    64  			},
    65  
    66  			"response_parameters_in_json": {
    67  				Type:          schema.TypeString,
    68  				Optional:      true,
    69  				ConflictsWith: []string{"response_parameters"},
    70  				Deprecated:    "Use field response_parameters instead",
    71  			},
    72  
    73  			"content_handling": {
    74  				Type:         schema.TypeString,
    75  				Optional:     true,
    76  				ValidateFunc: validateApiGatewayIntegrationContentHandling,
    77  			},
    78  		},
    79  	}
    80  }
    81  
    82  func resourceAwsApiGatewayIntegrationResponseCreate(d *schema.ResourceData, meta interface{}) error {
    83  	conn := meta.(*AWSClient).apigateway
    84  
    85  	templates := make(map[string]string)
    86  	for k, v := range d.Get("response_templates").(map[string]interface{}) {
    87  		templates[k] = v.(string)
    88  	}
    89  
    90  	parameters := make(map[string]string)
    91  	if kv, ok := d.GetOk("response_parameters"); ok {
    92  		for k, v := range kv.(map[string]interface{}) {
    93  			parameters[k] = v.(string)
    94  		}
    95  	}
    96  	if v, ok := d.GetOk("response_parameters_in_json"); ok {
    97  		if err := json.Unmarshal([]byte(v.(string)), &parameters); err != nil {
    98  			return fmt.Errorf("Error unmarshaling response_parameters_in_json: %s", err)
    99  		}
   100  	}
   101  	var contentHandling *string
   102  	if val, ok := d.GetOk("content_handling"); ok {
   103  		contentHandling = aws.String(val.(string))
   104  	}
   105  
   106  	input := apigateway.PutIntegrationResponseInput{
   107  		HttpMethod:         aws.String(d.Get("http_method").(string)),
   108  		ResourceId:         aws.String(d.Get("resource_id").(string)),
   109  		RestApiId:          aws.String(d.Get("rest_api_id").(string)),
   110  		StatusCode:         aws.String(d.Get("status_code").(string)),
   111  		ResponseTemplates:  aws.StringMap(templates),
   112  		ResponseParameters: aws.StringMap(parameters),
   113  		ContentHandling:    contentHandling,
   114  	}
   115  	if v, ok := d.GetOk("selection_pattern"); ok {
   116  		input.SelectionPattern = aws.String(v.(string))
   117  	}
   118  
   119  	_, err := conn.PutIntegrationResponse(&input)
   120  	if err != nil {
   121  		return fmt.Errorf("Error creating API Gateway Integration Response: %s", err)
   122  	}
   123  
   124  	d.SetId(fmt.Sprintf("agir-%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)))
   125  	log.Printf("[DEBUG] API Gateway Integration Response ID: %s", d.Id())
   126  
   127  	return resourceAwsApiGatewayIntegrationResponseRead(d, meta)
   128  }
   129  
   130  func resourceAwsApiGatewayIntegrationResponseRead(d *schema.ResourceData, meta interface{}) error {
   131  	conn := meta.(*AWSClient).apigateway
   132  
   133  	log.Printf("[DEBUG] Reading API Gateway Integration Response %s", d.Id())
   134  	integrationResponse, err := conn.GetIntegrationResponse(&apigateway.GetIntegrationResponseInput{
   135  		HttpMethod: aws.String(d.Get("http_method").(string)),
   136  		ResourceId: aws.String(d.Get("resource_id").(string)),
   137  		RestApiId:  aws.String(d.Get("rest_api_id").(string)),
   138  		StatusCode: aws.String(d.Get("status_code").(string)),
   139  	})
   140  	if err != nil {
   141  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
   142  			d.SetId("")
   143  			return nil
   144  		}
   145  		return err
   146  	}
   147  
   148  	log.Printf("[DEBUG] Received API Gateway Integration Response: %s", integrationResponse)
   149  
   150  	d.SetId(fmt.Sprintf("agir-%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)))
   151  	d.Set("response_templates", integrationResponse.ResponseTemplates)
   152  	d.Set("selection_pattern", integrationResponse.SelectionPattern)
   153  	d.Set("response_parameters", aws.StringValueMap(integrationResponse.ResponseParameters))
   154  	d.Set("response_parameters_in_json", aws.StringValueMap(integrationResponse.ResponseParameters))
   155  	return nil
   156  }
   157  
   158  func resourceAwsApiGatewayIntegrationResponseDelete(d *schema.ResourceData, meta interface{}) error {
   159  	conn := meta.(*AWSClient).apigateway
   160  	log.Printf("[DEBUG] Deleting API Gateway Integration Response: %s", d.Id())
   161  
   162  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   163  		_, err := conn.DeleteIntegrationResponse(&apigateway.DeleteIntegrationResponseInput{
   164  			HttpMethod: aws.String(d.Get("http_method").(string)),
   165  			ResourceId: aws.String(d.Get("resource_id").(string)),
   166  			RestApiId:  aws.String(d.Get("rest_api_id").(string)),
   167  			StatusCode: aws.String(d.Get("status_code").(string)),
   168  		})
   169  		if err == nil {
   170  			return nil
   171  		}
   172  
   173  		apigatewayErr, ok := err.(awserr.Error)
   174  		if apigatewayErr.Code() == "NotFoundException" {
   175  			return nil
   176  		}
   177  
   178  		if !ok {
   179  			return resource.NonRetryableError(err)
   180  		}
   181  
   182  		return resource.NonRetryableError(err)
   183  	})
   184  }