github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_api_gateway_method.go (about) 1 package aws 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "log" 7 "strconv" 8 "time" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/aws/awserr" 12 "github.com/aws/aws-sdk-go/service/apigateway" 13 "github.com/hashicorp/terraform/helper/resource" 14 "github.com/hashicorp/terraform/helper/schema" 15 ) 16 17 func resourceAwsApiGatewayMethod() *schema.Resource { 18 return &schema.Resource{ 19 Create: resourceAwsApiGatewayMethodCreate, 20 Read: resourceAwsApiGatewayMethodRead, 21 Update: resourceAwsApiGatewayMethodUpdate, 22 Delete: resourceAwsApiGatewayMethodDelete, 23 24 Schema: map[string]*schema.Schema{ 25 "rest_api_id": &schema.Schema{ 26 Type: schema.TypeString, 27 Required: true, 28 ForceNew: true, 29 }, 30 31 "resource_id": &schema.Schema{ 32 Type: schema.TypeString, 33 Required: true, 34 ForceNew: true, 35 }, 36 37 "http_method": &schema.Schema{ 38 Type: schema.TypeString, 39 Required: true, 40 ForceNew: true, 41 ValidateFunc: validateHTTPMethod, 42 }, 43 44 "authorization": &schema.Schema{ 45 Type: schema.TypeString, 46 Required: true, 47 }, 48 49 "authorizer_id": &schema.Schema{ 50 Type: schema.TypeString, 51 Optional: true, 52 }, 53 54 "api_key_required": &schema.Schema{ 55 Type: schema.TypeBool, 56 Optional: true, 57 Default: false, 58 }, 59 60 "request_models": &schema.Schema{ 61 Type: schema.TypeMap, 62 Optional: true, 63 Elem: schema.TypeString, 64 }, 65 66 "request_parameters": &schema.Schema{ 67 Type: schema.TypeMap, 68 Elem: schema.TypeBool, 69 Optional: true, 70 ConflictsWith: []string{"request_parameters_in_json"}, 71 }, 72 73 "request_parameters_in_json": &schema.Schema{ 74 Type: schema.TypeString, 75 Optional: true, 76 ConflictsWith: []string{"request_parameters"}, 77 Deprecated: "Use field request_parameters instead", 78 }, 79 }, 80 } 81 } 82 83 func resourceAwsApiGatewayMethodCreate(d *schema.ResourceData, meta interface{}) error { 84 conn := meta.(*AWSClient).apigateway 85 86 input := apigateway.PutMethodInput{ 87 AuthorizationType: aws.String(d.Get("authorization").(string)), 88 HttpMethod: aws.String(d.Get("http_method").(string)), 89 ResourceId: aws.String(d.Get("resource_id").(string)), 90 RestApiId: aws.String(d.Get("rest_api_id").(string)), 91 ApiKeyRequired: aws.Bool(d.Get("api_key_required").(bool)), 92 } 93 94 models := make(map[string]string) 95 for k, v := range d.Get("request_models").(map[string]interface{}) { 96 models[k] = v.(string) 97 } 98 if len(models) > 0 { 99 input.RequestModels = aws.StringMap(models) 100 } 101 102 parameters := make(map[string]bool) 103 if kv, ok := d.GetOk("request_parameters"); ok { 104 for k, v := range kv.(map[string]interface{}) { 105 parameters[k], ok = v.(bool) 106 if !ok { 107 value, _ := strconv.ParseBool(v.(string)) 108 parameters[k] = value 109 } 110 } 111 input.RequestParameters = aws.BoolMap(parameters) 112 } 113 if v, ok := d.GetOk("request_parameters_in_json"); ok { 114 if err := json.Unmarshal([]byte(v.(string)), ¶meters); err != nil { 115 return fmt.Errorf("Error unmarshaling request_parameters_in_json: %s", err) 116 } 117 input.RequestParameters = aws.BoolMap(parameters) 118 } 119 120 if v, ok := d.GetOk("authorizer_id"); ok { 121 input.AuthorizerId = aws.String(v.(string)) 122 } 123 124 _, err := conn.PutMethod(&input) 125 if err != nil { 126 return fmt.Errorf("Error creating API Gateway Method: %s", err) 127 } 128 129 d.SetId(fmt.Sprintf("agm-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string))) 130 log.Printf("[DEBUG] API Gateway Method ID: %s", d.Id()) 131 132 return nil 133 } 134 135 func resourceAwsApiGatewayMethodRead(d *schema.ResourceData, meta interface{}) error { 136 conn := meta.(*AWSClient).apigateway 137 138 log.Printf("[DEBUG] Reading API Gateway Method %s", d.Id()) 139 out, err := conn.GetMethod(&apigateway.GetMethodInput{ 140 HttpMethod: aws.String(d.Get("http_method").(string)), 141 ResourceId: aws.String(d.Get("resource_id").(string)), 142 RestApiId: aws.String(d.Get("rest_api_id").(string)), 143 }) 144 if err != nil { 145 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" { 146 d.SetId("") 147 return nil 148 } 149 return err 150 } 151 log.Printf("[DEBUG] Received API Gateway Method: %s", out) 152 d.SetId(fmt.Sprintf("agm-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string))) 153 d.Set("request_parameters", aws.BoolValueMap(out.RequestParameters)) 154 d.Set("request_parameters_in_json", aws.BoolValueMap(out.RequestParameters)) 155 d.Set("api_key_required", out.ApiKeyRequired) 156 d.Set("authorization_type", out.AuthorizationType) 157 d.Set("authorizer_id", out.AuthorizerId) 158 d.Set("request_models", aws.StringValueMap(out.RequestModels)) 159 160 return nil 161 } 162 163 func resourceAwsApiGatewayMethodUpdate(d *schema.ResourceData, meta interface{}) error { 164 conn := meta.(*AWSClient).apigateway 165 166 log.Printf("[DEBUG] Reading API Gateway Method %s", d.Id()) 167 operations := make([]*apigateway.PatchOperation, 0) 168 if d.HasChange("resource_id") { 169 operations = append(operations, &apigateway.PatchOperation{ 170 Op: aws.String("replace"), 171 Path: aws.String("/resourceId"), 172 Value: aws.String(d.Get("resource_id").(string)), 173 }) 174 } 175 176 if d.HasChange("request_models") { 177 operations = append(operations, expandApiGatewayRequestResponseModelOperations(d, "request_models", "requestModels")...) 178 } 179 180 if d.HasChange("request_parameters_in_json") { 181 ops, err := deprecatedExpandApiGatewayMethodParametersJSONOperations(d, "request_parameters_in_json", "requestParameters") 182 if err != nil { 183 return err 184 } 185 operations = append(operations, ops...) 186 } 187 188 if d.HasChange("request_parameters") { 189 parameters := make(map[string]bool) 190 var ok bool 191 for k, v := range d.Get("request_parameters").(map[string]interface{}) { 192 parameters[k], ok = v.(bool) 193 if !ok { 194 value, _ := strconv.ParseBool(v.(string)) 195 parameters[k] = value 196 } 197 } 198 ops, err := expandApiGatewayMethodParametersOperations(d, "request_parameters", "requestParameters") 199 if err != nil { 200 return err 201 } 202 operations = append(operations, ops...) 203 } 204 205 if d.HasChange("authorization") { 206 operations = append(operations, &apigateway.PatchOperation{ 207 Op: aws.String("replace"), 208 Path: aws.String("/authorizationType"), 209 Value: aws.String(d.Get("authorization").(string)), 210 }) 211 } 212 213 if d.HasChange("authorizer_id") { 214 operations = append(operations, &apigateway.PatchOperation{ 215 Op: aws.String("replace"), 216 Path: aws.String("/authorizerId"), 217 Value: aws.String(d.Get("authorizer_id").(string)), 218 }) 219 } 220 221 if d.HasChange("api_key_required") { 222 operations = append(operations, &apigateway.PatchOperation{ 223 Op: aws.String("replace"), 224 Path: aws.String("/apiKeyRequired"), 225 Value: aws.String(fmt.Sprintf("%t", d.Get("api_key_required").(bool))), 226 }) 227 } 228 229 method, err := conn.UpdateMethod(&apigateway.UpdateMethodInput{ 230 HttpMethod: aws.String(d.Get("http_method").(string)), 231 ResourceId: aws.String(d.Get("resource_id").(string)), 232 RestApiId: aws.String(d.Get("rest_api_id").(string)), 233 PatchOperations: operations, 234 }) 235 236 if err != nil { 237 return err 238 } 239 240 log.Printf("[DEBUG] Received API Gateway Method: %s", method) 241 242 return resourceAwsApiGatewayMethodRead(d, meta) 243 } 244 245 func resourceAwsApiGatewayMethodDelete(d *schema.ResourceData, meta interface{}) error { 246 conn := meta.(*AWSClient).apigateway 247 log.Printf("[DEBUG] Deleting API Gateway Method: %s", d.Id()) 248 249 return resource.Retry(5*time.Minute, func() *resource.RetryError { 250 _, err := conn.DeleteMethod(&apigateway.DeleteMethodInput{ 251 HttpMethod: aws.String(d.Get("http_method").(string)), 252 ResourceId: aws.String(d.Get("resource_id").(string)), 253 RestApiId: aws.String(d.Get("rest_api_id").(string)), 254 }) 255 if err == nil { 256 return nil 257 } 258 259 apigatewayErr, ok := err.(awserr.Error) 260 if apigatewayErr.Code() == "NotFoundException" { 261 return nil 262 } 263 264 if !ok { 265 return resource.NonRetryableError(err) 266 } 267 268 return resource.NonRetryableError(err) 269 }) 270 }