github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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: resourceAwsApiGatewayIntegrationResponseUpdate,
    21  		Delete: resourceAwsApiGatewayIntegrationResponseDelete,
    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  			"status_code": &schema.Schema{
    44  				Type:     schema.TypeString,
    45  				Required: true,
    46  			},
    47  
    48  			"selection_pattern": &schema.Schema{
    49  				Type:     schema.TypeString,
    50  				Optional: true,
    51  			},
    52  
    53  			"response_templates": &schema.Schema{
    54  				Type:     schema.TypeMap,
    55  				Optional: true,
    56  				Elem:     schema.TypeString,
    57  			},
    58  
    59  			"response_parameters": &schema.Schema{
    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": &schema.Schema{
    67  				Type:          schema.TypeString,
    68  				Optional:      true,
    69  				ConflictsWith: []string{"response_parameters"},
    70  				Deprecated:    "Use field response_parameters instead",
    71  			},
    72  		},
    73  	}
    74  }
    75  
    76  func resourceAwsApiGatewayIntegrationResponseCreate(d *schema.ResourceData, meta interface{}) error {
    77  	conn := meta.(*AWSClient).apigateway
    78  
    79  	templates := make(map[string]string)
    80  	for k, v := range d.Get("response_templates").(map[string]interface{}) {
    81  		templates[k] = v.(string)
    82  	}
    83  
    84  	parameters := make(map[string]string)
    85  	if kv, ok := d.GetOk("response_parameters"); ok {
    86  		for k, v := range kv.(map[string]interface{}) {
    87  			parameters[k] = v.(string)
    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 response_parameters_in_json: %s", err)
    93  		}
    94  	}
    95  
    96  	input := apigateway.PutIntegrationResponseInput{
    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  		ResponseTemplates:  aws.StringMap(templates),
   102  		ResponseParameters: aws.StringMap(parameters),
   103  	}
   104  	if v, ok := d.GetOk("selection_pattern"); ok {
   105  		input.SelectionPattern = aws.String(v.(string))
   106  	}
   107  	_, err := conn.PutIntegrationResponse(&input)
   108  	if err != nil {
   109  		return fmt.Errorf("Error creating API Gateway Integration Response: %s", err)
   110  	}
   111  
   112  	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)))
   113  	log.Printf("[DEBUG] API Gateway Integration Response ID: %s", d.Id())
   114  
   115  	return resourceAwsApiGatewayIntegrationResponseRead(d, meta)
   116  }
   117  
   118  func resourceAwsApiGatewayIntegrationResponseRead(d *schema.ResourceData, meta interface{}) error {
   119  	conn := meta.(*AWSClient).apigateway
   120  
   121  	log.Printf("[DEBUG] Reading API Gateway Integration Response %s", d.Id())
   122  	integrationResponse, err := conn.GetIntegrationResponse(&apigateway.GetIntegrationResponseInput{
   123  		HttpMethod: aws.String(d.Get("http_method").(string)),
   124  		ResourceId: aws.String(d.Get("resource_id").(string)),
   125  		RestApiId:  aws.String(d.Get("rest_api_id").(string)),
   126  		StatusCode: aws.String(d.Get("status_code").(string)),
   127  	})
   128  	if err != nil {
   129  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
   130  			d.SetId("")
   131  			return nil
   132  		}
   133  		return err
   134  	}
   135  
   136  	log.Printf("[DEBUG] Received API Gateway Integration Response: %s", integrationResponse)
   137  
   138  	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)))
   139  	d.Set("response_templates", integrationResponse.ResponseTemplates)
   140  	d.Set("selection_pattern", integrationResponse.SelectionPattern)
   141  	d.Set("response_parameters", aws.StringValueMap(integrationResponse.ResponseParameters))
   142  	d.Set("response_parameters_in_json", aws.StringValueMap(integrationResponse.ResponseParameters))
   143  	return nil
   144  }
   145  
   146  func resourceAwsApiGatewayIntegrationResponseUpdate(d *schema.ResourceData, meta interface{}) error {
   147  	return resourceAwsApiGatewayIntegrationResponseCreate(d, meta)
   148  }
   149  
   150  func resourceAwsApiGatewayIntegrationResponseDelete(d *schema.ResourceData, meta interface{}) error {
   151  	conn := meta.(*AWSClient).apigateway
   152  	log.Printf("[DEBUG] Deleting API Gateway Integration Response: %s", d.Id())
   153  
   154  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   155  		_, err := conn.DeleteIntegrationResponse(&apigateway.DeleteIntegrationResponseInput{
   156  			HttpMethod: aws.String(d.Get("http_method").(string)),
   157  			ResourceId: aws.String(d.Get("resource_id").(string)),
   158  			RestApiId:  aws.String(d.Get("rest_api_id").(string)),
   159  			StatusCode: aws.String(d.Get("status_code").(string)),
   160  		})
   161  		if err == nil {
   162  			return nil
   163  		}
   164  
   165  		apigatewayErr, ok := err.(awserr.Error)
   166  		if apigatewayErr.Code() == "NotFoundException" {
   167  			return nil
   168  		}
   169  
   170  		if !ok {
   171  			return resource.NonRetryableError(err)
   172  		}
   173  
   174  		return resource.NonRetryableError(err)
   175  	})
   176  }