Merge pull request #58775 from freehan/url-parse-fix
Automatic merge from submit-queue (batch tested with PRs 58777, 58978, 58977, 58775). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. fix url parsing for staging/dev endpoint ```release-note NONE ```
This commit is contained in:
		@@ -30,10 +30,6 @@ const (
 | 
			
		||||
	betaPrefix  = "https://www.googleapis.com/compute/beta/"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	allPrefixes = []string{gaPrefix, alphaPrefix, betaPrefix}
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// ResourceID identifies a GCE resource as parsed from compute resource URL.
 | 
			
		||||
type ResourceID struct {
 | 
			
		||||
	ProjectID string
 | 
			
		||||
@@ -66,15 +62,10 @@ func (r *ResourceID) Equal(other *ResourceID) bool {
 | 
			
		||||
func ParseResourceURL(url string) (*ResourceID, error) {
 | 
			
		||||
	errNotValid := fmt.Errorf("%q is not a valid resource URL", url)
 | 
			
		||||
 | 
			
		||||
	// Remove the "https://..." prefix if present
 | 
			
		||||
	for _, prefix := range allPrefixes {
 | 
			
		||||
		if strings.HasPrefix(url, prefix) {
 | 
			
		||||
			if len(url) < len(prefix) {
 | 
			
		||||
				return nil, errNotValid
 | 
			
		||||
			}
 | 
			
		||||
			url = url[len(prefix):]
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
	// Remove the prefix up to ...projects/
 | 
			
		||||
	projectsIndex := strings.Index(url, "/projects/")
 | 
			
		||||
	if projectsIndex >= 0 {
 | 
			
		||||
		url = url[projectsIndex+1:]
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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",
 | 
			
		||||
			&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",
 | 
			
		||||
			&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/zones/us-central1-c/res",
 | 
			
		||||
		"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)
 | 
			
		||||
		if err == nil {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user