Skip to content

Commit

Permalink
Merge pull request #13809 from ewbankkit/b-aws_apigatewayv2_stage-htt…
Browse files Browse the repository at this point in the history
…p-route-settings

r/aws_apigatewayv2_stage: 'data_trace_enabled' and 'logging_level' are only valid for WebSocket APIs
  • Loading branch information
breathingdust committed Jul 24, 2020
2 parents cb7677a + 659a511 commit e8bd28e
Show file tree
Hide file tree
Showing 3 changed files with 436 additions and 50 deletions.
59 changes: 37 additions & 22 deletions aws/resource_aws_apigatewayv2_stage.go
Expand Up @@ -91,19 +91,12 @@ func resourceAwsApiGatewayV2Stage() *schema.Resource {
"logging_level": {
Type: schema.TypeString,
Optional: true,
Default: apigatewayv2.LoggingLevelOff,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
apigatewayv2.LoggingLevelError,
apigatewayv2.LoggingLevelInfo,
apigatewayv2.LoggingLevelOff,
}, false),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
// Not set for HTTP APIs.
if old == "" && new == apigatewayv2.LoggingLevelOff {
return true
}
return false
},
},
"throttling_burst_limit": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -159,7 +152,7 @@ func resourceAwsApiGatewayV2Stage() *schema.Resource {
"logging_level": {
Type: schema.TypeString,
Optional: true,
Default: apigatewayv2.LoggingLevelOff,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
apigatewayv2.LoggingLevelError,
apigatewayv2.LoggingLevelInfo,
Expand Down Expand Up @@ -195,8 +188,19 @@ func resourceAwsApiGatewayV2Stage() *schema.Resource {
func resourceAwsApiGatewayV2StageCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigatewayv2conn

apiId := d.Get("api_id").(string)

apiOutput, err := conn.GetApi(&apigatewayv2.GetApiInput{
ApiId: aws.String(apiId),
})
if err != nil {
return fmt.Errorf("error reading API Gateway v2 API (%s): %s", apiId, err)
}

protocolType := aws.StringValue(apiOutput.ProtocolType)

req := &apigatewayv2.CreateStageInput{
ApiId: aws.String(d.Get("api_id").(string)),
ApiId: aws.String(apiId),
AutoDeploy: aws.Bool(d.Get("auto_deploy").(bool)),
StageName: aws.String(d.Get("name").(string)),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().Apigatewayv2Tags(),
Expand All @@ -208,7 +212,7 @@ func resourceAwsApiGatewayV2StageCreate(d *schema.ResourceData, meta interface{}
req.ClientCertificateId = aws.String(v.(string))
}
if v, ok := d.GetOk("default_route_settings"); ok {
req.DefaultRouteSettings = expandApiGatewayV2DefaultRouteSettings(v.([]interface{}))
req.DefaultRouteSettings = expandApiGatewayV2DefaultRouteSettings(v.([]interface{}), protocolType)
}
if v, ok := d.GetOk("deployment_id"); ok {
req.DeploymentId = aws.String(v.(string))
Expand All @@ -217,7 +221,7 @@ func resourceAwsApiGatewayV2StageCreate(d *schema.ResourceData, meta interface{}
req.Description = aws.String(v.(string))
}
if v, ok := d.GetOk("route_settings"); ok {
req.RouteSettings = expandApiGatewayV2RouteSettings(v.(*schema.Set))
req.RouteSettings = expandApiGatewayV2RouteSettings(v.(*schema.Set), protocolType)
}
if v, ok := d.GetOk("stage_variables"); ok {
req.StageVariables = stringMapToPointers(v.(map[string]interface{}))
Expand Down Expand Up @@ -322,8 +326,19 @@ func resourceAwsApiGatewayV2StageUpdate(d *schema.ResourceData, meta interface{}
if d.HasChanges("access_log_settings", "auto_deploy", "client_certificate_id",
"default_route_settings", "deployment_id", "description",
"route_settings", "stage_variables") {
apiId := d.Get("api_id").(string)

apiOutput, err := conn.GetApi(&apigatewayv2.GetApiInput{
ApiId: aws.String(apiId),
})
if err != nil {
return fmt.Errorf("error reading API Gateway v2 API (%s): %s", apiId, err)
}

protocolType := aws.StringValue(apiOutput.ProtocolType)

req := &apigatewayv2.UpdateStageInput{
ApiId: aws.String(d.Get("api_id").(string)),
ApiId: aws.String(apiId),
StageName: aws.String(d.Id()),
}
if d.HasChange("access_log_settings") {
Expand All @@ -336,7 +351,7 @@ func resourceAwsApiGatewayV2StageUpdate(d *schema.ResourceData, meta interface{}
req.ClientCertificateId = aws.String(d.Get("client_certificate_id").(string))
}
if d.HasChange("default_route_settings") {
req.DefaultRouteSettings = expandApiGatewayV2DefaultRouteSettings(d.Get("default_route_settings").([]interface{}))
req.DefaultRouteSettings = expandApiGatewayV2DefaultRouteSettings(d.Get("default_route_settings").([]interface{}), protocolType)
}
if d.HasChange("deployment_id") {
req.DeploymentId = aws.String(d.Get("deployment_id").(string))
Expand All @@ -345,7 +360,7 @@ func resourceAwsApiGatewayV2StageUpdate(d *schema.ResourceData, meta interface{}
req.Description = aws.String(d.Get("description").(string))
}
if d.HasChange("route_settings") {
req.RouteSettings = expandApiGatewayV2RouteSettings(d.Get("route_settings").(*schema.Set))
req.RouteSettings = expandApiGatewayV2RouteSettings(d.Get("route_settings").(*schema.Set), protocolType)
}
if d.HasChange("stage_variables") {
o, n := d.GetChange("stage_variables")
Expand All @@ -362,7 +377,7 @@ func resourceAwsApiGatewayV2StageUpdate(d *schema.ResourceData, meta interface{}
}

log.Printf("[DEBUG] Updating API Gateway v2 stage: %s", req)
_, err := conn.UpdateStage(req)
_, err = conn.UpdateStage(req)
if err != nil {
return fmt.Errorf("error updating API Gateway v2 stage (%s): %s", d.Id(), err)
}
Expand Down Expand Up @@ -454,21 +469,21 @@ func flattenApiGatewayV2AccessLogSettings(settings *apigatewayv2.AccessLogSettin
}}
}

func expandApiGatewayV2DefaultRouteSettings(vSettings []interface{}) *apigatewayv2.RouteSettings {
func expandApiGatewayV2DefaultRouteSettings(vSettings []interface{}, protocolType string) *apigatewayv2.RouteSettings {
routeSettings := &apigatewayv2.RouteSettings{}

if len(vSettings) == 0 || vSettings[0] == nil {
return routeSettings
}
mSettings := vSettings[0].(map[string]interface{})

if vDataTraceEnabled, ok := mSettings["data_trace_enabled"].(bool); ok {
if vDataTraceEnabled, ok := mSettings["data_trace_enabled"].(bool); ok && protocolType == apigatewayv2.ProtocolTypeWebsocket {
routeSettings.DataTraceEnabled = aws.Bool(vDataTraceEnabled)
}
if vDetailedMetricsEnabled, ok := mSettings["detailed_metrics_enabled"].(bool); ok {
routeSettings.DetailedMetricsEnabled = aws.Bool(vDetailedMetricsEnabled)
}
if vLoggingLevel, ok := mSettings["logging_level"].(string); ok && vLoggingLevel != "" {
if vLoggingLevel, ok := mSettings["logging_level"].(string); ok && vLoggingLevel != "" && protocolType == apigatewayv2.ProtocolTypeWebsocket {
routeSettings.LoggingLevel = aws.String(vLoggingLevel)
}
if vThrottlingBurstLimit, ok := mSettings["throttling_burst_limit"].(int); ok {
Expand All @@ -495,21 +510,21 @@ func flattenApiGatewayV2DefaultRouteSettings(routeSettings *apigatewayv2.RouteSe
}}
}

func expandApiGatewayV2RouteSettings(vSettings *schema.Set) map[string]*apigatewayv2.RouteSettings {
func expandApiGatewayV2RouteSettings(vSettings *schema.Set, protocolType string) map[string]*apigatewayv2.RouteSettings {
settings := map[string]*apigatewayv2.RouteSettings{}

for _, v := range vSettings.List() {
routeSettings := &apigatewayv2.RouteSettings{}

mSettings := v.(map[string]interface{})

if v, ok := mSettings["data_trace_enabled"].(bool); ok {
if v, ok := mSettings["data_trace_enabled"].(bool); ok && protocolType == apigatewayv2.ProtocolTypeWebsocket {
routeSettings.DataTraceEnabled = aws.Bool(v)
}
if v, ok := mSettings["detailed_metrics_enabled"].(bool); ok {
routeSettings.DetailedMetricsEnabled = aws.Bool(v)
}
if v, ok := mSettings["logging_level"].(string); ok {
if v, ok := mSettings["logging_level"].(string); ok && v != "" && protocolType == apigatewayv2.ProtocolTypeWebsocket {
routeSettings.LoggingLevel = aws.String(v)
}
if v, ok := mSettings["throttling_burst_limit"].(int); ok {
Expand Down

0 comments on commit e8bd28e

Please sign in to comment.