github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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: resourceAwsApiGatewayIntegrationCreate, 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 "content_handling": &schema.Schema{ 86 Type: schema.TypeString, 87 Optional: true, 88 ValidateFunc: validateApiGatewayIntegrationContentHandling, 89 }, 90 91 "passthrough_behavior": &schema.Schema{ 92 Type: schema.TypeString, 93 Optional: true, 94 Computed: true, 95 ValidateFunc: validateApiGatewayIntegrationPassthroughBehavior, 96 }, 97 }, 98 } 99 } 100 101 func resourceAwsApiGatewayIntegrationCreate(d *schema.ResourceData, meta interface{}) error { 102 conn := meta.(*AWSClient).apigateway 103 104 var integrationHttpMethod *string 105 if v, ok := d.GetOk("integration_http_method"); ok { 106 integrationHttpMethod = aws.String(v.(string)) 107 } 108 var uri *string 109 if v, ok := d.GetOk("uri"); ok { 110 uri = aws.String(v.(string)) 111 } 112 templates := make(map[string]string) 113 for k, v := range d.Get("request_templates").(map[string]interface{}) { 114 templates[k] = v.(string) 115 } 116 117 parameters := make(map[string]string) 118 if kv, ok := d.GetOk("request_parameters"); ok { 119 for k, v := range kv.(map[string]interface{}) { 120 parameters[k] = v.(string) 121 } 122 } 123 124 if v, ok := d.GetOk("request_parameters_in_json"); ok { 125 if err := json.Unmarshal([]byte(v.(string)), ¶meters); err != nil { 126 return fmt.Errorf("Error unmarshaling request_parameters_in_json: %s", err) 127 } 128 } 129 130 var passthroughBehavior *string 131 if v, ok := d.GetOk("passthrough_behavior"); ok { 132 passthroughBehavior = aws.String(v.(string)) 133 } 134 135 var credentials *string 136 if val, ok := d.GetOk("credentials"); ok { 137 credentials = aws.String(val.(string)) 138 } 139 140 var contentHandling *string 141 if val, ok := d.GetOk("content_handling"); ok { 142 contentHandling = aws.String(val.(string)) 143 } 144 145 _, err := conn.PutIntegration(&apigateway.PutIntegrationInput{ 146 HttpMethod: aws.String(d.Get("http_method").(string)), 147 ResourceId: aws.String(d.Get("resource_id").(string)), 148 RestApiId: aws.String(d.Get("rest_api_id").(string)), 149 Type: aws.String(d.Get("type").(string)), 150 IntegrationHttpMethod: integrationHttpMethod, 151 Uri: uri, 152 RequestParameters: aws.StringMap(parameters), 153 RequestTemplates: aws.StringMap(templates), 154 Credentials: credentials, 155 CacheNamespace: nil, 156 CacheKeyParameters: nil, 157 PassthroughBehavior: passthroughBehavior, 158 ContentHandling: contentHandling, 159 }) 160 if err != nil { 161 return fmt.Errorf("Error creating API Gateway Integration: %s", err) 162 } 163 164 d.SetId(fmt.Sprintf("agi-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string))) 165 166 return nil 167 } 168 169 func resourceAwsApiGatewayIntegrationRead(d *schema.ResourceData, meta interface{}) error { 170 conn := meta.(*AWSClient).apigateway 171 172 log.Printf("[DEBUG] Reading API Gateway Integration %s", d.Id()) 173 integration, err := conn.GetIntegration(&apigateway.GetIntegrationInput{ 174 HttpMethod: aws.String(d.Get("http_method").(string)), 175 ResourceId: aws.String(d.Get("resource_id").(string)), 176 RestApiId: aws.String(d.Get("rest_api_id").(string)), 177 }) 178 if err != nil { 179 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" { 180 d.SetId("") 181 return nil 182 } 183 return err 184 } 185 log.Printf("[DEBUG] Received API Gateway Integration: %s", integration) 186 d.SetId(fmt.Sprintf("agi-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string))) 187 188 // AWS converts "" to null on their side, convert it back 189 if v, ok := integration.RequestTemplates["application/json"]; ok && v == nil { 190 integration.RequestTemplates["application/json"] = aws.String("") 191 } 192 193 d.Set("request_templates", aws.StringValueMap(integration.RequestTemplates)) 194 d.Set("credentials", integration.Credentials) 195 d.Set("type", integration.Type) 196 d.Set("uri", integration.Uri) 197 d.Set("request_parameters", aws.StringValueMap(integration.RequestParameters)) 198 d.Set("request_parameters_in_json", aws.StringValueMap(integration.RequestParameters)) 199 d.Set("passthrough_behavior", integration.PassthroughBehavior) 200 d.Set("content_handling", integration.ContentHandling) 201 202 return nil 203 } 204 205 func resourceAwsApiGatewayIntegrationDelete(d *schema.ResourceData, meta interface{}) error { 206 conn := meta.(*AWSClient).apigateway 207 log.Printf("[DEBUG] Deleting API Gateway Integration: %s", d.Id()) 208 209 return resource.Retry(5*time.Minute, func() *resource.RetryError { 210 _, err := conn.DeleteIntegration(&apigateway.DeleteIntegrationInput{ 211 HttpMethod: aws.String(d.Get("http_method").(string)), 212 ResourceId: aws.String(d.Get("resource_id").(string)), 213 RestApiId: aws.String(d.Get("rest_api_id").(string)), 214 }) 215 if err == nil { 216 return nil 217 } 218 219 apigatewayErr, ok := err.(awserr.Error) 220 if apigatewayErr.Code() == "NotFoundException" { 221 return nil 222 } 223 224 if !ok { 225 return resource.NonRetryableError(err) 226 } 227 228 return resource.NonRetryableError(err) 229 }) 230 }