Skip to content

Commit

Permalink
Be more forgiving when parsing IDs in service networks_values
Browse files Browse the repository at this point in the history
  • Loading branch information
sk4zuzu committed Oct 18, 2024
1 parent 23f50c7 commit 9fda6a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
13 changes: 13 additions & 0 deletions opennebula/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package opennebula
import (
"fmt"
"reflect"
"strconv"
"strings"

"github.com/OpenNebula/one/src/oca/go/src/goca/errors"
Expand Down Expand Up @@ -143,3 +144,15 @@ func mergeSchemas(schema map[string]*schema.Schema, schemas ...map[string]*schem

return schema
}

func ParseIntFromInterface(i interface{}) (int, error) {
switch v := i.(type) {
case float64:
return int(v), nil
case string:
if r, err := strconv.ParseInt(v, 10, 32); err == nil {
return int(r), nil
}
}
return -1, fmt.Errorf("Does not look like a number")
}
16 changes: 3 additions & 13 deletions opennebula/resource_opennebula_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func resourceOpennebulaService() *schema.Resource {
"roles": {
Type: schema.TypeList,
Computed: true,
Description: "Map with the role dinamically generated information",
Description: "Map with the role dynamically generated information",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cardinality": {
Expand Down Expand Up @@ -335,17 +335,7 @@ func resourceOpennebulaServiceRead(ctx context.Context, d *schema.ResourceData,
})
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)
networkID, err := ParseIntFromInterface(idInterface)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Expand All @@ -354,7 +344,7 @@ func resourceOpennebulaServiceRead(ctx context.Context, d *schema.ResourceData,
})
return diags
}
networks[k] = int(networkID)
networks[k] = networkID
}
}
d.Set("networks", networks)
Expand Down

0 comments on commit 9fda6a8

Please sign in to comment.