From 8aa5e0771a59bcffc6ff577600b89c58ce782038 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Sun, 19 Jul 2015 16:30:15 +0200 Subject: [PATCH] Support static IP by name on GCE Signed-off-by: David Gageot --- drivers/google/compute_util.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/drivers/google/compute_util.go b/drivers/google/compute_util.go index 1e4d7771bc..159788c56b 100644 --- a/drivers/google/compute_util.go +++ b/drivers/google/compute_util.go @@ -4,6 +4,7 @@ import ( "fmt" "io/ioutil" "net/url" + "regexp" "strings" "time" @@ -88,6 +89,31 @@ func (c *ComputeUtil) deleteDisk() error { return c.waitForRegionalOp(op.Name) } +// staticAddress returns the external static IP address. +func (c *ComputeUtil) staticAddress() (string, error) { + // is the address a name? + isName, err := regexp.MatchString("[a-z]([-a-z0-9]*[a-z0-9])?", c.address) + if err != nil { + return "", err + } + + if (!isName) { + return c.address, nil + } + + // resolve the address by name + externalAddress, err := c.service.Addresses.Get(c.project, c.region(), c.address).Do() + if err != nil { + return "", err + } + + return externalAddress.Address, nil +} + +func (c *ComputeUtil) region() (string) { + return c.zone[:len(c.zone)-2] +} + func (c *ComputeUtil) firewallRule() (*raw.Firewall, error) { return c.service.Firewalls.Get(c.project, firewallRule).Do() } @@ -188,7 +214,12 @@ func (c *ComputeUtil) createInstance(d *Driver) error { } if c.address != "" { - instance.NetworkInterfaces[0].AccessConfigs[0].NatIP = c.address + staticAddress, err := c.staticAddress() + if err != nil { + return err + } + + instance.NetworkInterfaces[0].AccessConfigs[0].NatIP = staticAddress } disk, err := c.disk()