2016-03-05 23:16:52 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2016-05-05 22:14:51 +02:00
|
|
|
"encoding/json"
|
2016-03-05 23:16:52 +01:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/apigateway"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsApiGatewayIntegration() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsApiGatewayIntegrationCreate,
|
|
|
|
Read: resourceAwsApiGatewayIntegrationRead,
|
|
|
|
Update: resourceAwsApiGatewayIntegrationUpdate,
|
|
|
|
Delete: resourceAwsApiGatewayIntegrationDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"rest_api_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"resource_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"http_method": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
ValidateFunc: validateHTTPMethod,
|
|
|
|
},
|
|
|
|
|
|
|
|
"type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(string)
|
|
|
|
if value != "MOCK" && value != "AWS" && value != "HTTP" {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q must be one of 'AWS', 'MOCK', 'HTTP'", k))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"uri": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"credentials": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"integration_http_method": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ValidateFunc: validateHTTPMethod,
|
|
|
|
},
|
|
|
|
|
|
|
|
"request_templates": &schema.Schema{
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
Optional: true,
|
|
|
|
Elem: schema.TypeString,
|
|
|
|
},
|
2016-05-05 22:14:51 +02:00
|
|
|
|
|
|
|
"request_parameters_in_json": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2016-07-26 10:38:51 +02:00
|
|
|
|
|
|
|
"passthrough_behavior": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ValidateFunc: validateApiGatewayIntegrationPassthroughBehavior,
|
|
|
|
},
|
2016-03-05 23:16:52 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsApiGatewayIntegrationCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).apigateway
|
|
|
|
|
|
|
|
var integrationHttpMethod *string
|
|
|
|
if v, ok := d.GetOk("integration_http_method"); ok {
|
|
|
|
integrationHttpMethod = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
var uri *string
|
|
|
|
if v, ok := d.GetOk("uri"); ok {
|
|
|
|
uri = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
templates := make(map[string]string)
|
|
|
|
for k, v := range d.Get("request_templates").(map[string]interface{}) {
|
|
|
|
templates[k] = v.(string)
|
|
|
|
}
|
|
|
|
|
2016-05-05 22:14:51 +02:00
|
|
|
parameters := make(map[string]string)
|
|
|
|
if v, ok := d.GetOk("request_parameters_in_json"); ok {
|
|
|
|
if err := json.Unmarshal([]byte(v.(string)), ¶meters); err != nil {
|
|
|
|
return fmt.Errorf("Error unmarshaling request_parameters_in_json: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 10:38:51 +02:00
|
|
|
var passthroughBehavior *string
|
|
|
|
if v, ok := d.GetOk("passthrough_behavior"); ok {
|
|
|
|
passthroughBehavior = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
|
2016-03-05 23:16:52 +01:00
|
|
|
var credentials *string
|
|
|
|
if val, ok := d.GetOk("credentials"); ok {
|
|
|
|
credentials = aws.String(val.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conn.PutIntegration(&apigateway.PutIntegrationInput{
|
|
|
|
HttpMethod: aws.String(d.Get("http_method").(string)),
|
|
|
|
ResourceId: aws.String(d.Get("resource_id").(string)),
|
|
|
|
RestApiId: aws.String(d.Get("rest_api_id").(string)),
|
|
|
|
Type: aws.String(d.Get("type").(string)),
|
|
|
|
IntegrationHttpMethod: integrationHttpMethod,
|
|
|
|
Uri: uri,
|
2016-05-05 22:14:51 +02:00
|
|
|
// TODO reimplement once [GH-2143](https://github.com/hashicorp/terraform/issues/2143) has been implemented
|
2016-07-26 10:38:51 +02:00
|
|
|
RequestParameters: aws.StringMap(parameters),
|
|
|
|
RequestTemplates: aws.StringMap(templates),
|
|
|
|
Credentials: credentials,
|
|
|
|
CacheNamespace: nil,
|
|
|
|
CacheKeyParameters: nil,
|
|
|
|
PassthroughBehavior: passthroughBehavior,
|
2016-03-05 23:16:52 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating API Gateway Integration: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(fmt.Sprintf("agi-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string)))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsApiGatewayIntegrationRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).apigateway
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Reading API Gateway Integration %s", d.Id())
|
|
|
|
integration, err := conn.GetIntegration(&apigateway.GetIntegrationInput{
|
|
|
|
HttpMethod: aws.String(d.Get("http_method").(string)),
|
|
|
|
ResourceId: aws.String(d.Get("resource_id").(string)),
|
|
|
|
RestApiId: aws.String(d.Get("rest_api_id").(string)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] Received API Gateway Integration: %s", integration)
|
|
|
|
d.SetId(fmt.Sprintf("agi-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string)))
|
|
|
|
|
|
|
|
// AWS converts "" to null on their side, convert it back
|
|
|
|
if v, ok := integration.RequestTemplates["application/json"]; ok && v == nil {
|
|
|
|
integration.RequestTemplates["application/json"] = aws.String("")
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("request_templates", aws.StringValueMap(integration.RequestTemplates))
|
|
|
|
d.Set("credentials", integration.Credentials)
|
|
|
|
d.Set("type", integration.Type)
|
|
|
|
d.Set("uri", integration.Uri)
|
2016-05-05 22:14:51 +02:00
|
|
|
d.Set("request_parameters_in_json", aws.StringValueMap(integration.RequestParameters))
|
2016-07-26 10:38:51 +02:00
|
|
|
d.Set("passthrough_behavior", integration.PassthroughBehavior)
|
2016-03-05 23:16:52 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsApiGatewayIntegrationUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
return resourceAwsApiGatewayIntegrationCreate(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsApiGatewayIntegrationDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).apigateway
|
|
|
|
log.Printf("[DEBUG] Deleting API Gateway Integration: %s", d.Id())
|
|
|
|
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.Retry(5*time.Minute, func() *resource.RetryError {
|
2016-03-05 23:16:52 +01:00
|
|
|
_, err := conn.DeleteIntegration(&apigateway.DeleteIntegrationInput{
|
2016-03-06 10:20:13 +01:00
|
|
|
HttpMethod: aws.String(d.Get("http_method").(string)),
|
|
|
|
ResourceId: aws.String(d.Get("resource_id").(string)),
|
|
|
|
RestApiId: aws.String(d.Get("rest_api_id").(string)),
|
2016-03-05 23:16:52 +01:00
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
apigatewayErr, ok := err.(awserr.Error)
|
|
|
|
if apigatewayErr.Code() == "NotFoundException" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.NonRetryableError(err)
|
2016-03-05 23:16:52 +01:00
|
|
|
}
|
|
|
|
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.NonRetryableError(err)
|
2016-03-05 23:16:52 +01:00
|
|
|
})
|
|
|
|
}
|