From 23f8a0acc07c4250a067e5197647ba463d41db0b Mon Sep 17 00:00:00 2001 From: shurkys Date: Mon, 26 Feb 2024 23:27:38 +0000 Subject: [PATCH] Fixed type assertion panic in network ID retrieval Refactored network ID retrieval logic to include checks for nil values and to ensure that the 'id' field is of type float64 before conversion. This prevents a panic caused by improper type assertion and ensures robust error handling in case of missing or invalid network IDs. --- opennebula/resource_opennebula_service.go | 29 ++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/opennebula/resource_opennebula_service.go b/opennebula/resource_opennebula_service.go index d49525e70..04e0a0b1b 100644 --- a/opennebula/resource_opennebula_service.go +++ b/opennebula/resource_opennebula_service.go @@ -314,7 +314,33 @@ func resourceOpennebulaServiceRead(ctx context.Context, d *schema.ResourceData, var networks = make(map[string]int) for _, val := range sv.Template.Body.NetworksVals { for k, v := range val { - s := v.(map[string]interface{})["id"].(string) + if v == nil { + diags = append(diags, diag.Diagnostic{ + Severity: diag.Error, + Summary: "Network ID is nil", + Detail: fmt.Sprintf("service (ID: %s): Network ID is nil", d.Id()), + }) + return diags + } + idInterface, ok := v.(map[string]interface{})["id"] + if !ok { + diags = append(diags, diag.Diagnostic{ + Severity: diag.Error, + Summary: "Network ID is missing", + Detail: fmt.Sprintf("service (ID: %s): Network ID is missing", d.Id()), + }) + return diags + } + idFloat, ok := idInterface.(float64) + if !ok { + diags = append(diags, diag.Diagnostic{ + Severity: diag.Error, + Summary: "Failed to convert network ID to float64", + Detail: fmt.Sprintf("service (ID: %s): Failed to convert network ID to float64", d.Id()), + }) + return diags + } + s := strconv.Itoa(int(idFloat)) networkID, err := strconv.ParseInt(s, 10, 0) if err != nil { diags = append(diags, diag.Diagnostic{ @@ -329,6 +355,7 @@ func resourceOpennebulaServiceRead(ctx context.Context, d *schema.ResourceData, } d.Set("networks", networks) + // Retrieve roles var roles []map[string]interface{} for _, role := range sv.Template.Body.Roles {