-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: utility to handle string IDs (#884)
All current resources use string IDs, but they are ints in hcloud-go. This util helps convert between them. Will be used by #817
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package resourceutil | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"strconv" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func ParseID(value types.String) (int, diag.Diagnostics) { | ||
var diagnostics diag.Diagnostics | ||
id, err := strconv.ParseInt(value.ValueString(), 10, 64) | ||
if err != nil { | ||
diagnostics.AddAttributeError( | ||
path.Root("id"), | ||
"Could not parse resource ID", | ||
err.Error(), | ||
) | ||
return 0, diagnostics | ||
} | ||
if id > math.MaxInt32 && strconv.IntSize == 32 { | ||
diagnostics.AddAttributeError( | ||
path.Root("id"), | ||
"ID is larger than your system supports.", | ||
"The current version of the provider has problems with IDs > 32bit on 32 bit systems. If possible, switch to a 64 bit system for now. See https://github.com/hetznercloud/hcloud-go/issues/263") | ||
} | ||
return int(id), diagnostics | ||
} | ||
|
||
func IDStringValue(value int) types.String { | ||
return types.StringValue(fmt.Sprintf("%d", value)) | ||
} |