-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: duplicate utility to get server ID from ProviderID (#532)
This utility function was duplicated with nearly the exact same functionality. This commit cleans it up by extracting to a new package (to avoid cyclic imports). These two methods are about to get more complicated with #523, better to clean it up now than to make changes to both locations in the future. --------- Co-authored-by: Jonas L. <[email protected]>
- Loading branch information
Showing
4 changed files
with
41 additions
and
80 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
This file was deleted.
Oops, something went wrong.
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
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,36 @@ | ||
package providerid | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const ( | ||
// providerPrefix is the prefix for all provider IDs. It MUST not be changed, | ||
// otherwise existing nodes will not be recognized anymore. | ||
providerPrefix = "hcloud://" | ||
) | ||
|
||
// ToServerID converts a ProviderID to a server ID. | ||
func ToServerID(providerID string) (int64, error) { | ||
if !strings.HasPrefix(providerID, providerPrefix) { | ||
return 0, fmt.Errorf("providerID does not have the expected prefix %s: %s", providerPrefix, providerID) | ||
} | ||
|
||
idString := strings.ReplaceAll(providerID, providerPrefix, "") | ||
if idString == "" { | ||
return 0, fmt.Errorf("providerID is missing a serverID: %s", providerID) | ||
} | ||
|
||
id, err := strconv.ParseInt(idString, 10, 64) | ||
if err != nil { | ||
return 0, fmt.Errorf("unable to parse server id: %s", providerID) | ||
} | ||
return id, nil | ||
} | ||
|
||
// FromServerID converts a server ID to a ProviderID. | ||
func FromServerID(serverID int64) string { | ||
return fmt.Sprintf("%s%d", providerPrefix, serverID) | ||
} |