Skip to content

Commit

Permalink
Fixed type assertion panic in network ID retrieval
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shurkys committed Feb 26, 2024
1 parent 9ac3407 commit 23f8a0a
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion opennebula/resource_opennebula_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 {
Expand Down

0 comments on commit 23f8a0a

Please sign in to comment.