-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Firewall network field now supports self_link in addition of name (#477)
- Loading branch information
Showing
5 changed files
with
116 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
const networkLinkTemplate = "projects/%s/global/networks/%s" | ||
|
||
var networkLinkRegex = regexp.MustCompile("projects/(.+)/global/networks/(.+)") | ||
|
||
type NetworkFieldValue struct { | ||
Project string | ||
Name string | ||
} | ||
|
||
// Parses a `network` supporting 4 different formats: | ||
// - https://www.googleapis.com/compute/{version}/projects/myproject/global/networks/my-network | ||
// - projects/myproject/global/networks/my-network | ||
// - global/networks/my-network (default project is used) | ||
// - my-network (default project is used) | ||
func ParseNetworkFieldValue(network string, config *Config) *NetworkFieldValue { | ||
if networkLinkRegex.MatchString(network) { | ||
parts := networkLinkRegex.FindStringSubmatch(network) | ||
|
||
return &NetworkFieldValue{ | ||
Project: parts[1], | ||
Name: parts[2], | ||
} | ||
} | ||
|
||
return &NetworkFieldValue{ | ||
Project: config.Project, | ||
Name: GetResourceNameFromSelfLink(network), | ||
} | ||
} | ||
|
||
func (f NetworkFieldValue) RelativeLink() string { | ||
return fmt.Sprintf(networkLinkTemplate, f.Project, f.Name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package google | ||
|
||
import "testing" | ||
|
||
func TestParseNetworkFieldValue(t *testing.T) { | ||
cases := map[string]struct { | ||
Network string | ||
ExpectedRelativeLink string | ||
Config *Config | ||
}{ | ||
"network is a full self link": { | ||
Network: "https://www.googleapis.com/compute/v1/projects/myproject/global/networks/my-network", | ||
ExpectedRelativeLink: "projects/myproject/global/networks/my-network", | ||
}, | ||
"network is a relative self link": { | ||
Network: "projects/myproject/global/networks/my-network", | ||
ExpectedRelativeLink: "projects/myproject/global/networks/my-network", | ||
}, | ||
"network is a partial relative self link": { | ||
Network: "global/networks/my-network", | ||
ExpectedRelativeLink: "projects/default-project/global/networks/my-network", | ||
Config: &Config{Project: "default-project"}, | ||
}, | ||
"network is the name only": { | ||
Network: "my-network", | ||
ExpectedRelativeLink: "projects/default-project/global/networks/my-network", | ||
Config: &Config{Project: "default-project"}, | ||
}, | ||
} | ||
|
||
for tn, tc := range cases { | ||
if fieldValue := ParseNetworkFieldValue(tc.Network, tc.Config); fieldValue.RelativeLink() != tc.ExpectedRelativeLink { | ||
t.Fatalf("bad: %s, expected relative link to be '%s' but got '%s'", tn, tc.ExpectedRelativeLink, fieldValue.RelativeLink()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters