diff --git a/opennebula/helpers.go b/opennebula/helpers.go index 5d87fc9f5..5b1ca76f3 100644 --- a/opennebula/helpers.go +++ b/opennebula/helpers.go @@ -3,6 +3,7 @@ package opennebula import ( "fmt" "reflect" + "strconv" "strings" "github.com/OpenNebula/one/src/oca/go/src/goca/errors" @@ -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") +} diff --git a/opennebula/resource_opennebula_service.go b/opennebula/resource_opennebula_service.go index 76523de7f..decc5968c 100644 --- a/opennebula/resource_opennebula_service.go +++ b/opennebula/resource_opennebula_service.go @@ -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": { @@ -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, @@ -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)