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