Skip to content

Commit 0d95674

Browse files
Merge pull request #223 from briantkennedy/location
Add location method to Key.
2 parents 1b37761 + a40d5e4 commit 0d95674

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

pkg/cloud/meta/key.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ func (k *Key) Type() KeyType {
7272
}
7373
}
7474

75+
// Location returns the location for the key.
76+
func (k *Key) Location() string {
77+
switch {
78+
case k.Zone != "":
79+
return k.Zone
80+
case k.Region != "":
81+
return k.Region
82+
default:
83+
return "global"
84+
}
85+
}
86+
7587
// String returns a string representation of the key.
7688
func (k Key) String() string {
7789
switch k.Type() {

pkg/cloud/meta/key_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ func TestKeyType(t *testing.T) {
3737
}
3838
}
3939

40+
func TestKeyLocation(t *testing.T) {
41+
for _, tc := range []struct {
42+
key *Key
43+
want string
44+
}{
45+
{GlobalKey("abc"), "global"},
46+
{ZonalKey("abc", "us-central1-b"), "us-central1-b"},
47+
{RegionalKey("abc", "us-central1"), "us-central1"},
48+
} {
49+
if tc.key.Location() != tc.want {
50+
t.Errorf("key.Location() == %v, want %v", tc.key.Type(), tc.want)
51+
}
52+
}
53+
}
54+
4055
func TestKeyString(t *testing.T) {
4156
t.Parallel()
4257

0 commit comments

Comments
 (0)