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