github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/aws/resource_aws_api_gateway_integration_response.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/apigateway"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsApiGatewayIntegrationResponse() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsApiGatewayIntegrationResponseCreate,
    18  		Read:   resourceAwsApiGatewayIntegrationResponseRead,
    19  		Update: resourceAwsApiGatewayIntegrationResponseUpdate,
    20  		Delete: resourceAwsApiGatewayIntegrationResponseDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"rest_api_id": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  
    29  			"resource_id": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Required: true,
    32  				ForceNew: true,
    33  			},
    34  
    35  			"http_method": &schema.Schema{
    36  				Type:         schema.TypeString,
    37  				Required:     true,
    38  				ForceNew:     true,
    39  				ValidateFunc: validateHTTPMethod,
    40  			},
    41  
    42  			"status_code": &schema.Schema{
    43  				Type:     schema.TypeString,
    44  				Required: true,
    45  			},
    46  
    47  			"selection_pattern": &schema.Schema{
    48  				Type:     schema.TypeString,
    49  				Optional: true,
    50  			},
    51  
    52  			"response_templates": &schema.Schema{
    53  				Type:     schema.TypeMap,
    54  				Optional: true,
    55  				Elem:     schema.TypeString,
    56  			},
    57  		},
    58  	}
    59  }
    60  
    61  func resourceAwsApiGatewayIntegrationResponseCreate(d *schema.ResourceData, meta interface{}) error {
    62  	conn := meta.(*AWSClient).apigateway
    63  
    64  	templates := make(map[string]string)
    65  	for k, v := range d.Get("response_templates").(map[string]interface{}) {
    66  		templates[k] = v.(string)
    67  	}
    68  
    69  	_, err := conn.PutIntegrationResponse(&apigateway.PutIntegrationResponseInput{
    70  		HttpMethod:        aws.String(d.Get("http_method").(string)),
    71  		ResourceId:        aws.String(d.Get("resource_id").(string)),
    72  		RestApiId:         aws.String(d.Get("rest_api_id").(string)),
    73  		StatusCode:        aws.String(d.Get("status_code").(string)),
    74  		ResponseTemplates: aws.StringMap(templates),
    75  		// TODO implement once [GH-2143](https://github.com/hashicorp/terraform/issues/2143) has been implemented
    76  		ResponseParameters: nil,
    77  	})
    78  	if err != nil {
    79  		return fmt.Errorf("Error creating API Gateway Integration Response: %s", err)
    80  	}
    81  
    82  	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)))
    83  	log.Printf("[DEBUG] API Gateway Integration Response ID: %s", d.Id())
    84  
    85  	return nil
    86  }
    87  
    88  func resourceAwsApiGatewayIntegrationResponseRead(d *schema.ResourceData, meta interface{}) error {
    89  	conn := meta.(*AWSClient).apigateway
    90  
    91  	log.Printf("[DEBUG] Reading API Gateway Integration Response %s", d.Id())
    92  	integrationResponse, err := conn.GetIntegrationResponse(&apigateway.GetIntegrationResponseInput{
    93  		HttpMethod: aws.String(d.Get("http_method").(string)),
    94  		ResourceId: aws.String(d.Get("resource_id").(string)),
    95  		RestApiId:  aws.String(d.Get("rest_api_id").(string)),
    96  		StatusCode: aws.String(d.Get("status_code").(string)),
    97  	})
    98  	if err != nil {
    99  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
   100  			d.SetId("")
   101  			return nil
   102  		}
   103  		return err
   104  	}
   105  	log.Printf("[DEBUG] Received API Gateway Integration Response: %s", integrationResponse)
   106  	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)))
   107  
   108  	return nil
   109  }
   110  
   111  func resourceAwsApiGatewayIntegrationResponseUpdate(d *schema.ResourceData, meta interface{}) error {
   112  	return resourceAwsApiGatewayIntegrationResponseCreate(d, meta)
   113  }
   114  
   115  func resourceAwsApiGatewayIntegrationResponseDelete(d *schema.ResourceData, meta interface{}) error {
   116  	conn := meta.(*AWSClient).apigateway
   117  	log.Printf("[DEBUG] Deleting API Gateway Integration Response: %s", d.Id())
   118  
   119  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   120  		_, err := conn.DeleteIntegrationResponse(&apigateway.DeleteIntegrationResponseInput{
   121  			HttpMethod: aws.String(d.Get("http_method").(string)),
   122  			ResourceId: aws.String(d.Get("resource_id").(string)),
   123  			RestApiId:  aws.String(d.Get("rest_api_id").(string)),
   124  			StatusCode: aws.String(d.Get("status_code").(string)),
   125  		})
   126  		if err == nil {
   127  			return nil
   128  		}
   129  
   130  		apigatewayErr, ok := err.(awserr.Error)
   131  		if apigatewayErr.Code() == "NotFoundException" {
   132  			return nil
   133  		}
   134  
   135  		if !ok {
   136  			return resource.NonRetryableError(err)
   137  		}
   138  
   139  		return resource.NonRetryableError(err)
   140  	})
   141  }