diff --git a/drivers/google/compute_util.go b/drivers/google/compute_util.go index e095bab0c0..b7f9f7e018 100644 --- a/drivers/google/compute_util.go +++ b/drivers/google/compute_util.go @@ -28,6 +28,7 @@ type ComputeUtil struct { project string diskTypeURL string address string + networkProject string network string subnetwork string preemptible bool @@ -60,6 +61,19 @@ func newComputeUtil(driver *Driver) (*ComputeUtil, error) { return nil, err } + var networkProject string + if strings.Contains(driver.Network, "/projects/") { + var splittedElements = strings.Split(driver.Network, "/") + for i, element := range splittedElements { + if element == "projects" { + networkProject = splittedElements[i+1] + break + } + } + } else { + networkProject = driver.Project + } + return &ComputeUtil{ zone: driver.Zone, instanceName: driver.MachineName, @@ -67,6 +81,7 @@ func newComputeUtil(driver *Driver) (*ComputeUtil, error) { project: driver.Project, diskTypeURL: driver.DiskType, address: driver.Address, + networkProject: networkProject, network: driver.Network, subnetwork: driver.Subnetwork, preemptible: driver.Preemptible, @@ -137,7 +152,7 @@ func (c *ComputeUtil) region() string { } func (c *ComputeUtil) firewallRule() (*raw.Firewall, error) { - return c.service.Firewalls.Get(c.project, firewallRule).Do() + return c.service.Firewalls.Get(c.networkProject, firewallRule).Do() } func missingOpenedPorts(rule *raw.Firewall, ports []string) map[string][]string { @@ -185,15 +200,24 @@ func (c *ComputeUtil) openFirewallPorts(d *Driver) error { log.Infof("Opening firewall ports") create := false - rule, _ := c.firewallRule() + rule, err := c.firewallRule() + if err != nil { + return err + } if rule == nil { create = true + var net string + if strings.Contains(d.Network, "/networks/") { + net = d.Network + } else { + net = c.globalURL + "/networks/" + d.Network + } rule = &raw.Firewall{ Name: firewallRule, Allowed: []*raw.FirewallAllowed{}, SourceRanges: []string{"0.0.0.0/0"}, TargetTags: []string{firewallTargetTag}, - Network: c.globalURL + "/networks/" + d.Network, + Network: net, } } @@ -215,9 +239,9 @@ func (c *ComputeUtil) openFirewallPorts(d *Driver) error { var op *raw.Operation if create { - op, err = c.service.Firewalls.Insert(c.project, rule).Do() + op, err = c.service.Firewalls.Insert(c.networkProject, rule).Do() } else { - op, err = c.service.Firewalls.Update(c.project, firewallRule, rule).Do() + op, err = c.service.Firewalls.Update(c.networkProject, firewallRule, rule).Do() } if err != nil { @@ -277,7 +301,7 @@ func (c *ComputeUtil) createInstance(d *Driver) error { if strings.Contains(c.subnetwork, "/subnetworks/") { instance.NetworkInterfaces[0].Subnetwork = c.subnetwork } else if c.subnetwork != "" { - instance.NetworkInterfaces[0].Subnetwork = "projects/" + c.project + "/regions/" + c.region() + "/subnetworks/" + c.subnetwork + instance.NetworkInterfaces[0].Subnetwork = "projects/" + c.networkProject + "/regions/" + c.region() + "/subnetworks/" + c.subnetwork } if !c.useInternalIPOnly {