diff --git a/builder/googlecompute/config.go b/builder/googlecompute/config.go index 2072bd112..bd08f1b2e 100644 --- a/builder/googlecompute/config.go +++ b/builder/googlecompute/config.go @@ -37,6 +37,7 @@ type Config struct { MachineType string `mapstructure:"machine_type"` Metadata map[string]string `mapstructure:"metadata"` Network string `mapstructure:"network"` + OmitExternalIP bool `mapstructure:"omit_external_ip"` Preemptible bool `mapstructure:"preemptible"` RawStateTimeout string `mapstructure:"state_timeout"` Region string `mapstructure:"region"` @@ -169,6 +170,14 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { } } + if c.OmitExternalIP && c.Address != "" { + errs = packer.MultiErrorAppend(fmt.Errorf("you can not specify an external address when 'omit_external_ip' is true")) + } + + if c.OmitExternalIP && !c.UseInternalIP { + errs = packer.MultiErrorAppend(fmt.Errorf("'use_internal_ip' must be true if 'omit_external_ip' is true")) + } + // Check for any errors. if errs != nil && len(errs.Errors) > 0 { return nil, nil, errs diff --git a/builder/googlecompute/driver.go b/builder/googlecompute/driver.go index f1c6c931e..8c0e53aef 100644 --- a/builder/googlecompute/driver.go +++ b/builder/googlecompute/driver.go @@ -53,6 +53,7 @@ type InstanceConfig struct { Metadata map[string]string Name string Network string + OmitExternalIP bool Preemptible bool Region string ServiceAccountEmail string diff --git a/builder/googlecompute/driver_gce.go b/builder/googlecompute/driver_gce.go index 3b262d59e..67b4c7fe8 100644 --- a/builder/googlecompute/driver_gce.go +++ b/builder/googlecompute/driver_gce.go @@ -256,21 +256,24 @@ func (d *driverGCE) RunInstance(c *InstanceConfig) (<-chan error, error) { subnetworkSelfLink = subnetwork.SelfLink } - // If given a regional ip, get it - accessconfig := compute.AccessConfig{ - Name: "AccessConfig created by Packer", - Type: "ONE_TO_ONE_NAT", - } + var accessconfig *compute.AccessConfig + // Use external IP if OmitExternalIP isn't set + if !c.OmitExternalIP { + accessconfig = &compute.AccessConfig{ + Name: "AccessConfig created by Packer", + Type: "ONE_TO_ONE_NAT", + } - if c.Address != "" { - d.ui.Message(fmt.Sprintf("Looking up address: %s", c.Address)) - region_url := strings.Split(zone.Region, "/") - region := region_url[len(region_url)-1] - address, err := d.service.Addresses.Get(d.projectId, region, c.Address).Do() - if err != nil { - return nil, err + // If given a static IP, use it + if c.Address != "" { + region_url := strings.Split(zone.Region, "/") + region := region_url[len(region_url)-1] + address, err := d.service.Addresses.Get(d.projectId, region, c.Address).Do() + if err != nil { + return nil, err + } + accessconfig.NatIP = address.Address } - accessconfig.NatIP = address.Address } // Build up the metadata @@ -307,11 +310,9 @@ func (d *driverGCE) RunInstance(c *InstanceConfig) (<-chan error, error) { Name: c.Name, NetworkInterfaces: []*compute.NetworkInterface{ &compute.NetworkInterface{ - AccessConfigs: []*compute.AccessConfig{ - &accessconfig, - }, - Network: network.SelfLink, - Subnetwork: subnetworkSelfLink, + AccessConfigs: []*compute.AccessConfig{accessconfig}, + Network: network.SelfLink, + Subnetwork: subnetworkSelfLink, }, }, Scheduling: &compute.Scheduling{ diff --git a/builder/googlecompute/step_create_instance.go b/builder/googlecompute/step_create_instance.go index cf997a920..114cc7b9d 100644 --- a/builder/googlecompute/step_create_instance.go +++ b/builder/googlecompute/step_create_instance.go @@ -25,7 +25,7 @@ func (config *Config) getImage() Image { func (config *Config) getInstanceMetadata(sshPublicKey string) (map[string]string, error) { instanceMetadata := make(map[string]string) - var err error + var err error // Copy metadata from config. for k, v := range config.Metadata { @@ -77,6 +77,7 @@ func (s *StepCreateInstance) Run(state multistep.StateBag) multistep.StepAction Metadata: metadata, Name: name, Network: config.Network, + OmitExternalIP: config.OmitExternalIP, Preemptible: config.Preemptible, Region: config.Region, ServiceAccountEmail: config.account.ClientEmail, diff --git a/website/source/docs/builders/googlecompute.html.md b/website/source/docs/builders/googlecompute.html.md index c944e9d4c..9a651187e 100644 --- a/website/source/docs/builders/googlecompute.html.md +++ b/website/source/docs/builders/googlecompute.html.md @@ -146,6 +146,9 @@ builder. - `network` (string) - The Google Compute network to use for the launched instance. Defaults to `"default"`. +- `omit_external_ip` (boolean) - If true, the instance will not have an external IP. + `use_internal_ip` must be true if this property is true. + - `preemptible` (boolean) - If true, launch a preembtible instance. - `region` (string) - The region in which to launch the instance. Defaults to