GCE: Fix Valid() to check for proper region/zone names
This commit is contained in:
		@@ -18,6 +18,7 @@ package meta
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"regexp"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Key for a GCP resource.
 | 
					// Key for a GCP resource.
 | 
				
			||||||
@@ -39,6 +40,11 @@ const (
 | 
				
			|||||||
	Global = "global"
 | 
						Global = "global"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						// locationRegexp is the format of regions/zone names in GCE.
 | 
				
			||||||
 | 
						locationRegexp = regexp.MustCompile("^[a-z](?:[-a-z0-9]+)?$")
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// ZonalKey returns the key for a zonal resource.
 | 
					// ZonalKey returns the key for a zonal resource.
 | 
				
			||||||
func ZonalKey(name, zone string) *Key {
 | 
					func ZonalKey(name, zone string) *Key {
 | 
				
			||||||
	return &Key{name, zone, ""}
 | 
						return &Key{name, zone, ""}
 | 
				
			||||||
@@ -79,10 +85,16 @@ func (k Key) String() string {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Valid is true if the key is valid.
 | 
					// Valid is true if the key is valid.
 | 
				
			||||||
func (k *Key) Valid(typeName string) bool {
 | 
					func (k *Key) Valid() bool {
 | 
				
			||||||
	if k.Zone != "" && k.Region != "" {
 | 
						if k.Zone != "" && k.Region != "" {
 | 
				
			||||||
		return false
 | 
							return false
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						switch {
 | 
				
			||||||
 | 
						case k.Region != "":
 | 
				
			||||||
 | 
							return locationRegexp.Match([]byte(k.Region))
 | 
				
			||||||
 | 
						case k.Zone != "":
 | 
				
			||||||
 | 
							return locationRegexp.Match([]byte(k.Zone))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	return true
 | 
						return true
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -58,18 +58,19 @@ func TestKeyValid(t *testing.T) {
 | 
				
			|||||||
	zone := "us-central1-b"
 | 
						zone := "us-central1-b"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, tc := range []struct {
 | 
						for _, tc := range []struct {
 | 
				
			||||||
		key      *Key
 | 
							key  *Key
 | 
				
			||||||
		typeName string
 | 
							want bool
 | 
				
			||||||
		want     bool
 | 
					 | 
				
			||||||
	}{
 | 
						}{
 | 
				
			||||||
		// Note: these test cases need to be synchronized with the
 | 
							{GlobalKey("abc"), true},
 | 
				
			||||||
		// actual settings for each type.
 | 
							{RegionalKey("abc", region), true},
 | 
				
			||||||
		{GlobalKey("abc"), "UrlMap", true},
 | 
							{ZonalKey("abc", zone), true},
 | 
				
			||||||
		{&Key{"abc", zone, region}, "UrlMap", false},
 | 
							{RegionalKey("abc", "/invalid/"), false},
 | 
				
			||||||
 | 
							{ZonalKey("abc", "/invalid/"), false},
 | 
				
			||||||
 | 
							{&Key{"abc", zone, region}, false},
 | 
				
			||||||
	} {
 | 
						} {
 | 
				
			||||||
		valid := tc.key.Valid(tc.typeName)
 | 
							got := tc.key.Valid()
 | 
				
			||||||
		if valid != tc.want {
 | 
							if got != tc.want {
 | 
				
			||||||
			t.Errorf("key %+v, type %v; key.Valid() = %v, want %v", tc.key, tc.typeName, valid, tc.want)
 | 
								t.Errorf("key %+v; key.Valid() = %v, want %v", tc.key, got, tc.want)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user