2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-10 07:18:30 -04:00
|
|
|
package gcpshared
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
var providerIDRegex = regexp.MustCompile(`^gce://([^/]+)/([^/]+)/([^/]+)$`)
|
|
|
|
|
2022-11-09 08:43:48 -05:00
|
|
|
// SplitProviderID splits a k8s provider ID for GCP instances into its core components.
|
|
|
|
// A provider ID is build after the schema 'gce://<project-id>/<zone>/<instance-name>'
|
2022-06-10 07:18:30 -04:00
|
|
|
func SplitProviderID(providerID string) (project, zone, instance string, err error) {
|
|
|
|
matches := providerIDRegex.FindStringSubmatch(providerID)
|
|
|
|
if len(matches) != 4 {
|
|
|
|
return "", "", "", fmt.Errorf("error splitting providerID: %v", providerID)
|
|
|
|
}
|
|
|
|
return matches[1], matches[2], matches[3], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoinProviderID builds a k8s provider ID for GCP instances.
|
|
|
|
// A providerID is build after the schema 'gce://<project-id>/<zone>/<instance-name>'
|
|
|
|
func JoinProviderID(project, zone, instanceName string) string {
|
|
|
|
return fmt.Sprintf("gce://%v/%v/%v", project, zone, instanceName)
|
|
|
|
}
|