Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws_apigatewayv2_stage: 'data_trace_enabled' and 'logging_level' are only valid for WebSocket APIs #13809

Merged
59 changes: 37 additions & 22 deletions aws/resource_aws_apigatewayv2_stage.go
Original file line number Diff line number Diff line change
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 @@ -158,7 +151,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 @@ -194,8 +187,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 @@ -207,7 +211,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 @@ -216,7 +220,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 @@ -321,8 +325,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 @@ -335,7 +350,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 @@ -344,7 +359,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 @@ -361,7 +376,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 @@ -453,21 +468,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 @@ -494,21 +509,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
Loading