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