Skip to content

Commit

Permalink
Fixed type assertion panic in network ID retrieval (#532)
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 authored Oct 17, 2024
1 parent 5d906d1 commit 23f50c7
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion opennebula/resource_opennebula_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,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 Down

0 comments on commit 23f50c7

Please sign in to comment.