github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_api_gateway_integration.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 resourceAwsApiGatewayIntegration() *schema.Resource {
    17  	return &schema.Resource{
    18  		Create: resourceAwsApiGatewayIntegrationCreate,
    19  		Read:   resourceAwsApiGatewayIntegrationRead,
    20  		Update: resourceAwsApiGatewayIntegrationUpdate,
    21  		Delete: resourceAwsApiGatewayIntegrationDelete,
    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  			"type": &schema.Schema{
    44  				Type:         schema.TypeString,
    45  				Required:     true,
    46  				ValidateFunc: validateApiGatewayIntegrationType,
    47  			},
    48  
    49  			"uri": &schema.Schema{
    50  				Type:     schema.TypeString,
    51  				Optional: true,
    52  			},
    53  
    54  			"credentials": &schema.Schema{
    55  				Type:     schema.TypeString,
    56  				Optional: true,
    57  			},
    58  
    59  			"integration_http_method": &schema.Schema{
    60  				Type:         schema.TypeString,
    61  				Optional:     true,
    62  				ValidateFunc: validateHTTPMethod,
    63  			},
    64  
    65  			"request_templates": &schema.Schema{
    66  				Type:     schema.TypeMap,
    67  				Optional: true,
    68  				Elem:     schema.TypeString,
    69  			},
    70  
    71  			"request_parameters": &schema.Schema{
    72  				Type:          schema.TypeMap,
    73  				Elem:          schema.TypeString,
    74  				Optional:      true,
    75  				ConflictsWith: []string{"request_parameters_in_json"},
    76  			},
    77  
    78  			"request_parameters_in_json": &schema.Schema{
    79  				Type:          schema.TypeString,
    80  				Optional:      true,
    81  				ConflictsWith: []string{"request_parameters"},
    82  				Deprecated:    "Use field request_parameters instead",
    83  			},
    84  
    85  			"passthrough_behavior": &schema.Schema{
    86  				Type:         schema.TypeString,
    87  				Optional:     true,
    88  				Computed:     true,
    89  				ValidateFunc: validateApiGatewayIntegrationPassthroughBehavior,
    90  			},
    91  		},
    92  	}
    93  }
    94  
    95  func resourceAwsApiGatewayIntegrationCreate(d *schema.ResourceData, meta interface{}) error {
    96  	conn := meta.(*AWSClient).apigateway
    97  
    98  	var integrationHttpMethod *string
    99  	if v, ok := d.GetOk("integration_http_method"); ok {
   100  		integrationHttpMethod = aws.String(v.(string))
   101  	}
   102  	var uri *string
   103  	if v, ok := d.GetOk("uri"); ok {
   104  		uri = aws.String(v.(string))
   105  	}
   106  	templates := make(map[string]string)
   107  	for k, v := range d.Get("request_templates").(map[string]interface{}) {
   108  		templates[k] = v.(string)
   109  	}
   110  
   111  	parameters := make(map[string]string)
   112  	if kv, ok := d.GetOk("request_parameters"); ok {
   113  		for k, v := range kv.(map[string]interface{}) {
   114  			parameters[k] = v.(string)
   115  		}
   116  	}
   117  
   118  	if v, ok := d.GetOk("request_parameters_in_json"); ok {
   119  		if err := json.Unmarshal([]byte(v.(string)), &parameters); err != nil {
   120  			return fmt.Errorf("Error unmarshaling request_parameters_in_json: %s", err)
   121  		}
   122  	}
   123  
   124  	var passthroughBehavior *string
   125  	if v, ok := d.GetOk("passthrough_behavior"); ok {
   126  		passthroughBehavior = aws.String(v.(string))
   127  	}
   128  
   129  	var credentials *string
   130  	if val, ok := d.GetOk("credentials"); ok {
   131  		credentials = aws.String(val.(string))
   132  	}
   133  
   134  	_, err := conn.PutIntegration(&apigateway.PutIntegrationInput{
   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  		Type:       aws.String(d.Get("type").(string)),
   139  		IntegrationHttpMethod: integrationHttpMethod,
   140  		Uri:                 uri,
   141  		RequestParameters:   aws.StringMap(parameters),
   142  		RequestTemplates:    aws.StringMap(templates),
   143  		Credentials:         credentials,
   144  		CacheNamespace:      nil,
   145  		CacheKeyParameters:  nil,
   146  		PassthroughBehavior: passthroughBehavior,
   147  	})
   148  	if err != nil {
   149  		return fmt.Errorf("Error creating API Gateway Integration: %s", err)
   150  	}
   151  
   152  	d.SetId(fmt.Sprintf("agi-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string)))
   153  
   154  	return nil
   155  }
   156  
   157  func resourceAwsApiGatewayIntegrationRead(d *schema.ResourceData, meta interface{}) error {
   158  	conn := meta.(*AWSClient).apigateway
   159  
   160  	log.Printf("[DEBUG] Reading API Gateway Integration %s", d.Id())
   161  	integration, err := conn.GetIntegration(&apigateway.GetIntegrationInput{
   162  		HttpMethod: aws.String(d.Get("http_method").(string)),
   163  		ResourceId: aws.String(d.Get("resource_id").(string)),
   164  		RestApiId:  aws.String(d.Get("rest_api_id").(string)),
   165  	})
   166  	if err != nil {
   167  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
   168  			d.SetId("")
   169  			return nil
   170  		}
   171  		return err
   172  	}
   173  	log.Printf("[DEBUG] Received API Gateway Integration: %s", integration)
   174  	d.SetId(fmt.Sprintf("agi-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string)))
   175  
   176  	// AWS converts "" to null on their side, convert it back
   177  	if v, ok := integration.RequestTemplates["application/json"]; ok && v == nil {
   178  		integration.RequestTemplates["application/json"] = aws.String("")
   179  	}
   180  
   181  	d.Set("request_templates", aws.StringValueMap(integration.RequestTemplates))
   182  	d.Set("credentials", integration.Credentials)
   183  	d.Set("type", integration.Type)
   184  	d.Set("uri", integration.Uri)
   185  	d.Set("request_parameters", aws.StringValueMap(integration.RequestParameters))
   186  	d.Set("request_parameters_in_json", aws.StringValueMap(integration.RequestParameters))
   187  	d.Set("passthrough_behavior", integration.PassthroughBehavior)
   188  
   189  	return nil
   190  }
   191  
   192  func resourceAwsApiGatewayIntegrationUpdate(d *schema.ResourceData, meta interface{}) error {
   193  	return resourceAwsApiGatewayIntegrationCreate(d, meta)
   194  }
   195  
   196  func resourceAwsApiGatewayIntegrationDelete(d *schema.ResourceData, meta interface{}) error {
   197  	conn := meta.(*AWSClient).apigateway
   198  	log.Printf("[DEBUG] Deleting API Gateway Integration: %s", d.Id())
   199  
   200  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   201  		_, err := conn.DeleteIntegration(&apigateway.DeleteIntegrationInput{
   202  			HttpMethod: aws.String(d.Get("http_method").(string)),
   203  			ResourceId: aws.String(d.Get("resource_id").(string)),
   204  			RestApiId:  aws.String(d.Get("rest_api_id").(string)),
   205  		})
   206  		if err == nil {
   207  			return nil
   208  		}
   209  
   210  		apigatewayErr, ok := err.(awserr.Error)
   211  		if apigatewayErr.Code() == "NotFoundException" {
   212  			return nil
   213  		}
   214  
   215  		if !ok {
   216  			return resource.NonRetryableError(err)
   217  		}
   218  
   219  		return resource.NonRetryableError(err)
   220  	})
   221  }