fix url parsing for staging/dev endpoint
This commit is contained in:
@@ -30,10 +30,6 @@ const (
|
|||||||
betaPrefix = "https://www.googleapis.com/compute/beta/"
|
betaPrefix = "https://www.googleapis.com/compute/beta/"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
allPrefixes = []string{gaPrefix, alphaPrefix, betaPrefix}
|
|
||||||
)
|
|
||||||
|
|
||||||
// ResourceID identifies a GCE resource as parsed from compute resource URL.
|
// ResourceID identifies a GCE resource as parsed from compute resource URL.
|
||||||
type ResourceID struct {
|
type ResourceID struct {
|
||||||
ProjectID string
|
ProjectID string
|
||||||
@@ -66,15 +62,10 @@ func (r *ResourceID) Equal(other *ResourceID) bool {
|
|||||||
func ParseResourceURL(url string) (*ResourceID, error) {
|
func ParseResourceURL(url string) (*ResourceID, error) {
|
||||||
errNotValid := fmt.Errorf("%q is not a valid resource URL", url)
|
errNotValid := fmt.Errorf("%q is not a valid resource URL", url)
|
||||||
|
|
||||||
// Remove the "https://..." prefix if present
|
// Remove the prefix up to ...projects/
|
||||||
for _, prefix := range allPrefixes {
|
projectsIndex := strings.Index(url, "/projects/")
|
||||||
if strings.HasPrefix(url, prefix) {
|
if projectsIndex >= 0 {
|
||||||
if len(url) < len(prefix) {
|
url = url[projectsIndex+1:]
|
||||||
return nil, errNotValid
|
|
||||||
}
|
|
||||||
url = url[len(prefix):]
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
parts := strings.Split(url, "/")
|
parts := strings.Split(url, "/")
|
||||||
|
@@ -54,6 +54,18 @@ func TestParseResourceURL(t *testing.T) {
|
|||||||
"https://www.googleapis.com/compute/v1/projects/some-gce-project/zones/us-central1-c/instances/instance-1",
|
"https://www.googleapis.com/compute/v1/projects/some-gce-project/zones/us-central1-c/instances/instance-1",
|
||||||
&ResourceID{"some-gce-project", "instances", meta.ZonalKey("instance-1", "us-central1-c")},
|
&ResourceID{"some-gce-project", "instances", meta.ZonalKey("instance-1", "us-central1-c")},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"http://localhost:3990/compute/beta/projects/some-gce-project/global/operations/operation-1513289952196-56054460af5a0-b1dae0c3-9bbf9dbf",
|
||||||
|
&ResourceID{"some-gce-project", "operations", meta.GlobalKey("operation-1513289952196-56054460af5a0-b1dae0c3-9bbf9dbf")},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"http://localhost:3990/compute/alpha/projects/some-gce-project/regions/dev-central1/addresses/my-address",
|
||||||
|
&ResourceID{"some-gce-project", "addresses", meta.RegionalKey("my-address", "dev-central1")},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"http://localhost:3990/compute/v1/projects/some-gce-project/zones/dev-central1-std/instances/instance-1",
|
||||||
|
&ResourceID{"some-gce-project", "instances", meta.ZonalKey("instance-1", "dev-central1-std")},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"projects/some-gce-project",
|
"projects/some-gce-project",
|
||||||
&ResourceID{"some-gce-project", "projects", nil},
|
&ResourceID{"some-gce-project", "projects", nil},
|
||||||
@@ -103,7 +115,6 @@ func TestParseResourceURL(t *testing.T) {
|
|||||||
"projects/some-gce-project/global/foo/bar/baz",
|
"projects/some-gce-project/global/foo/bar/baz",
|
||||||
"projects/some-gce-project/zones/us-central1-c/res",
|
"projects/some-gce-project/zones/us-central1-c/res",
|
||||||
"projects/some-gce-project/zones/us-central1-c/res/name/extra",
|
"projects/some-gce-project/zones/us-central1-c/res/name/extra",
|
||||||
"https://www.googleapis.com/compute/gamma/projects/some-gce-project/global/addresses/name",
|
|
||||||
} {
|
} {
|
||||||
r, err := ParseResourceURL(tc)
|
r, err := ParseResourceURL(tc)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
Reference in New Issue
Block a user