github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_api_gateway_method_settings.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/apigateway" 10 "github.com/hashicorp/terraform/helper/schema" 11 ) 12 13 func resourceAwsApiGatewayMethodSettings() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceAwsApiGatewayMethodSettingsUpdate, 16 Read: resourceAwsApiGatewayMethodSettingsRead, 17 Update: resourceAwsApiGatewayMethodSettingsUpdate, 18 Delete: resourceAwsApiGatewayMethodSettingsDelete, 19 20 Schema: map[string]*schema.Schema{ 21 "rest_api_id": { 22 Type: schema.TypeString, 23 Required: true, 24 ForceNew: true, 25 }, 26 "stage_name": { 27 Type: schema.TypeString, 28 Required: true, 29 ForceNew: true, 30 }, 31 "method_path": { 32 Type: schema.TypeString, 33 Required: true, 34 ForceNew: true, 35 }, 36 "settings": { 37 Type: schema.TypeList, 38 Required: true, 39 MaxItems: 1, 40 Elem: &schema.Resource{ 41 Schema: map[string]*schema.Schema{ 42 "metrics_enabled": { 43 Type: schema.TypeBool, 44 Optional: true, 45 }, 46 "logging_level": { 47 Type: schema.TypeString, 48 Optional: true, 49 }, 50 "data_trace_enabled": { 51 Type: schema.TypeBool, 52 Optional: true, 53 }, 54 "throttling_burst_limit": { 55 Type: schema.TypeInt, 56 Optional: true, 57 }, 58 "throttling_rate_limit": { 59 Type: schema.TypeFloat, 60 Optional: true, 61 }, 62 "caching_enabled": { 63 Type: schema.TypeBool, 64 Optional: true, 65 }, 66 "cache_ttl_in_seconds": { 67 Type: schema.TypeInt, 68 Optional: true, 69 }, 70 "cache_data_encrypted": { 71 Type: schema.TypeBool, 72 Optional: true, 73 }, 74 "require_authorization_for_cache_control": { 75 Type: schema.TypeBool, 76 Optional: true, 77 }, 78 "unauthorized_cache_control_header_strategy": { 79 Type: schema.TypeString, 80 Optional: true, 81 }, 82 }, 83 }, 84 }, 85 }, 86 } 87 } 88 89 func resourceAwsApiGatewayMethodSettingsRead(d *schema.ResourceData, meta interface{}) error { 90 conn := meta.(*AWSClient).apigateway 91 92 log.Printf("[DEBUG] Reading API Gateway Method Settings %s", d.Id()) 93 input := apigateway.GetStageInput{ 94 RestApiId: aws.String(d.Get("rest_api_id").(string)), 95 StageName: aws.String(d.Get("stage_name").(string)), 96 } 97 stage, err := conn.GetStage(&input) 98 if err != nil { 99 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" { 100 log.Printf("[WARN] API Gateway Stage %s not found, removing method settings", d.Id()) 101 d.SetId("") 102 return nil 103 } 104 return err 105 } 106 log.Printf("[DEBUG] Received API Gateway Stage: %s", stage) 107 108 methodPath := d.Get("method_path").(string) 109 settings, ok := stage.MethodSettings[methodPath] 110 if !ok { 111 log.Printf("[WARN] API Gateway Method Settings for %q not found, removing", methodPath) 112 d.SetId("") 113 return nil 114 } 115 116 d.Set("settings.0.metrics_enabled", settings.MetricsEnabled) 117 d.Set("settings.0.logging_level", settings.LoggingLevel) 118 d.Set("settings.0.data_trace_enabled", settings.DataTraceEnabled) 119 d.Set("settings.0.throttling_burst_limit", settings.ThrottlingBurstLimit) 120 d.Set("settings.0.throttling_rate_limit", settings.ThrottlingRateLimit) 121 d.Set("settings.0.caching_enabled", settings.CachingEnabled) 122 d.Set("settings.0.cache_ttl_in_seconds", settings.CacheTtlInSeconds) 123 d.Set("settings.0.cache_data_encrypted", settings.CacheDataEncrypted) 124 d.Set("settings.0.require_authorization_for_cache_control", settings.RequireAuthorizationForCacheControl) 125 d.Set("settings.0.unauthorized_cache_control_header_strategy", settings.UnauthorizedCacheControlHeaderStrategy) 126 127 return nil 128 } 129 130 func resourceAwsApiGatewayMethodSettingsUpdate(d *schema.ResourceData, meta interface{}) error { 131 conn := meta.(*AWSClient).apigateway 132 133 methodPath := d.Get("method_path").(string) 134 prefix := fmt.Sprintf("/%s/", methodPath) 135 136 ops := make([]*apigateway.PatchOperation, 0) 137 if d.HasChange("settings.0.metrics_enabled") { 138 ops = append(ops, &apigateway.PatchOperation{ 139 Op: aws.String("replace"), 140 Path: aws.String(prefix + "metrics/enabled"), 141 Value: aws.String(fmt.Sprintf("%t", d.Get("settings.0.metrics_enabled").(bool))), 142 }) 143 } 144 if d.HasChange("settings.0.logging_level") { 145 ops = append(ops, &apigateway.PatchOperation{ 146 Op: aws.String("replace"), 147 Path: aws.String(prefix + "logging/loglevel"), 148 Value: aws.String(d.Get("settings.0.logging_level").(string)), 149 }) 150 } 151 if d.HasChange("settings.0.data_trace_enabled") { 152 ops = append(ops, &apigateway.PatchOperation{ 153 Op: aws.String("replace"), 154 Path: aws.String(prefix + "logging/dataTrace"), 155 Value: aws.String(fmt.Sprintf("%t", d.Get("settings.0.data_trace_enabled").(bool))), 156 }) 157 } 158 159 if d.HasChange("settings.0.throttling_burst_limit") { 160 ops = append(ops, &apigateway.PatchOperation{ 161 Op: aws.String("replace"), 162 Path: aws.String(prefix + "throttling/burstLimit"), 163 Value: aws.String(fmt.Sprintf("%d", d.Get("settings.0.throttling_burst_limit").(int))), 164 }) 165 } 166 if d.HasChange("settings.0.throttling_rate_limit") { 167 ops = append(ops, &apigateway.PatchOperation{ 168 Op: aws.String("replace"), 169 Path: aws.String(prefix + "throttling/rateLimit"), 170 Value: aws.String(fmt.Sprintf("%f", d.Get("settings.0.throttling_rate_limit").(float64))), 171 }) 172 } 173 if d.HasChange("settings.0.caching_enabled") { 174 ops = append(ops, &apigateway.PatchOperation{ 175 Op: aws.String("replace"), 176 Path: aws.String(prefix + "caching/enabled"), 177 Value: aws.String(fmt.Sprintf("%t", d.Get("settings.0.caching_enabled").(bool))), 178 }) 179 } 180 if d.HasChange("settings.0.cache_ttl_in_seconds") { 181 ops = append(ops, &apigateway.PatchOperation{ 182 Op: aws.String("replace"), 183 Path: aws.String(prefix + "caching/ttlInSeconds"), 184 Value: aws.String(fmt.Sprintf("%d", d.Get("settings.0.cache_ttl_in_seconds").(int))), 185 }) 186 } 187 if d.HasChange("settings.0.cache_data_encrypted") { 188 ops = append(ops, &apigateway.PatchOperation{ 189 Op: aws.String("replace"), 190 Path: aws.String(prefix + "caching/dataEncrypted"), 191 Value: aws.String(fmt.Sprintf("%d", d.Get("settings.0.cache_data_encrypted").(int))), 192 }) 193 } 194 if d.HasChange("settings.0.require_authorization_for_cache_control") { 195 ops = append(ops, &apigateway.PatchOperation{ 196 Op: aws.String("replace"), 197 Path: aws.String(prefix + "caching/requireAuthorizationForCacheControl"), 198 Value: aws.String(fmt.Sprintf("%t", d.Get("settings.0.require_authorization_for_cache_control").(bool))), 199 }) 200 } 201 if d.HasChange("settings.0.unauthorized_cache_control_header_strategy") { 202 ops = append(ops, &apigateway.PatchOperation{ 203 Op: aws.String("replace"), 204 Path: aws.String(prefix + "caching/unauthorizedCacheControlHeaderStrategy"), 205 Value: aws.String(d.Get("settings.0.unauthorized_cache_control_header_strategy").(string)), 206 }) 207 } 208 209 restApiId := d.Get("rest_api_id").(string) 210 stageName := d.Get("stage_name").(string) 211 input := apigateway.UpdateStageInput{ 212 RestApiId: aws.String(restApiId), 213 StageName: aws.String(stageName), 214 PatchOperations: ops, 215 } 216 log.Printf("[DEBUG] Updating API Gateway Stage: %s", input) 217 _, err := conn.UpdateStage(&input) 218 if err != nil { 219 return fmt.Errorf("Updating API Gateway Stage failed: %s", err) 220 } 221 222 d.SetId(restApiId + "-" + stageName + "-" + methodPath) 223 224 return resourceAwsApiGatewayMethodSettingsRead(d, meta) 225 } 226 227 func resourceAwsApiGatewayMethodSettingsDelete(d *schema.ResourceData, meta interface{}) error { 228 conn := meta.(*AWSClient).apigateway 229 log.Printf("[DEBUG] Deleting API Gateway Method Settings: %s", d.Id()) 230 231 input := apigateway.UpdateStageInput{ 232 RestApiId: aws.String(d.Get("rest_api_id").(string)), 233 StageName: aws.String(d.Get("stage_name").(string)), 234 PatchOperations: []*apigateway.PatchOperation{ 235 { 236 Op: aws.String("remove"), 237 Path: aws.String(fmt.Sprintf("/%s", d.Get("method_path").(string))), 238 }, 239 }, 240 } 241 log.Printf("[DEBUG] Updating API Gateway Stage: %s", input) 242 _, err := conn.UpdateStage(&input) 243 if err != nil { 244 return fmt.Errorf("Updating API Gateway Stage failed: %s", err) 245 } 246 247 return nil 248 }