Constructor

InstanceGroup

new InstanceGroup(zone, name)

You can create and manage groups of virtual machine instances so that you don't have to individually control each instance in your project.

Parameter

zone

Zone

name

string

See also

Creating Groups of Instances

Unmanaged Instance Groups

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('us-central1-a');
const instanceGroup = zone.instanceGroup('web-servers');

Properties

getVMsStream

Get a list of VM instances in this instance group as a readable object stream.

Parameter

options

Optional

object

Configuration object. See InstanceGroup#getVMs for a complete list of options.

Returns

stream 

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('us-central1-a');
const instanceGroup = zone.instanceGroup('web-servers');

instanceGroup.getVMsStream()
  .on('error', console.error)
  .on('data', function(vm) {
    // `vm` is a `VM` object.
  })
  .on('end', function() {
    // All instances retrieved.
  });

//-
// If you anticipate many results, you can end a stream early to prevent
// unnecessary processing and API requests.
//-
instanceGroup.getVMsStream()
  .on('data', function(vm) {
    this.end();
  });

id

string

name

string

zone

Zone

The parent Zone instance of this InstanceGroup instance.

Methods

add

add(vms, callback)

Add one or more VMs to this instance group.

Parameter

vms

(VM or Array of VM)

VM instances to add to this instance group.

callback

function()

The callback function.

Values in callback have the following properties:

Parameter

err

error

An error returned while making this request.

Value may be null.

operation

Operation

An operation object that can be used to check the status of the request.

apiResponse

object

The full API response.

See also

InstanceGroups: addInstances API Documentation

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('us-central1-a');
const instanceGroup = zone.instanceGroup('web-servers');

const vms = [
  gce.zone('us-central1-a').vm('http-server'),
  gce.zone('us-central1-a').vm('https-server')
];

instanceGroup.add(vms, function(err, operation, apiResponse) {
  // `operation` is an Operation object that can be used to check the status
  // of the request.
});

//-
// If the callback is omitted, we'll return a Promise.
//-
instanceGroup.add(vms).then(function(data) {
  const operation = data[0];
  const apiResponse = data[1];
});

create

create(options)

Create an instance group.

Parameter

options

Optional

object

See Zone#createInstanceGroup.

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('us-central1-a');
const instanceGroup = zone.instanceGroup('web-servers');

function onCreated(err, instanceGroup, operation, apiResponse) {
  // `instanceGroup` is an InstanceGroup object.

  // `operation` is an Operation object that can be used to check the
  // status of the request.
}

instanceGroup.create(onCreated);

//-
// If the callback is omitted, we'll return a Promise.
//-
instanceGroup.create().then(function(data) {
  const instanceGroup = data[0];
  const operation = data[1];
  const apiResponse = data[2];
});

delete

delete(callback)

Delete the instance group.

Parameter

callback

Optional

function()

The callback function.

Values in callback have the following properties:

Parameter

err

error

An error returned while making this request.

Value may be null.

operation

Operation

An operation object that can be used to check the status of the request.

apiResponse

object

The full API response.

See also

InstanceGroups: delete API Documentation

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('us-central1-a');
const instanceGroup = zone.instanceGroup('web-servers');

instanceGroup.delete(function(err, operation, apiResponse) {
  // `operation` is an Operation object that can be used to check the status
  // of the request.
});

//-
// If the callback is omitted, we'll return a Promise.
//-
instanceGroup.delete().then(function(data) {
  const operation = data[0];
  const apiResponse = data[1];
});

exists

exists(callback)

Check if the instance group exists.

Parameter

callback

function()

The callback function.

Values in callback have the following properties:

Parameter

err

error

An error returned while making this request.

Value may be null.

exists

boolean

Whether the instance group exists or not.

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('us-central1-a');
const instanceGroup = zone.instanceGroup('web-servers');

instanceGroup.exists(function(err, exists) {});

//-
// If the callback is omitted, we'll return a Promise.
//-
instanceGroup.exists().then(function(data) {
  const exists = data[0];
});

get

get(options)

Get an instance group if it exists.

You may optionally use this to "get or create" an object by providing an object with autoCreate set to true. Any extra configuration that is normally required for the create method must be contained within this object as well.

Parameter

options

Optional

options

Configuration object.

Values in options have the following properties:

Parameter

autoCreate

boolean

Automatically create the object if it does not exist. Default: false

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('us-central1-a');
const instanceGroup = zone.instanceGroup('web-servers');

instanceGroup.get(function(err, instanceGroup, apiResponse) {
  // `instanceGroup` is an InstanceGroup object.
});

//-
// If the callback is omitted, we'll return a Promise.
//-
instanceGroup.get().then(function(data) {
  const instanceGroup = data[0];
  const apiResponse = data[1];
});

getMetadata

getMetadata(callback)

Get the instance group's metadata.

Parameter

callback

Optional

function()

The callback function.

Values in callback have the following properties:

Parameter

err

error

An error returned while making this request.

Value may be null.

metadata

object

The instance group's metadata.

apiResponse

object

The full API response.

See also

InstanceGroups: get API Documentation

InstanceGroups Resource

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('us-central1-a');
const instanceGroup = zone.instanceGroup('web-servers');

instanceGroup.getMetadata(function(err, metadata, apiResponse) {});

//-
// If the callback is omitted, we'll return a Promise.
//-
instanceGroup.getMetadata().then(function(data) {
  const metadata = data[0];
  const apiResponse = data[1];
});

getVMs

getVMs(options, callback)

Get a list of VM instances in this instance group.

Parameter

options

Optional

object

Instance search options.

Values in options have the following properties:

Parameter

autoPaginate

boolean

Have pagination handled automatically. Default: true.

filter

string

Search filter in the format of {name} {comparison} {filterString}.

- **`name`**: the name of the field to compare
- **`comparison`**: the comparison operator, `eq` (equal) or `ne`
  (not equal)
- **`filterString`**: the string to filter to. For string fields, this
  can be a regular expression.

maxApiCalls

number

Maximum number of API calls to make.

maxResults

number

Maximum number of VMs to return.

pageToken

string

A previously-returned page token representing part of the larger set of results to view.

running

boolean

Only return instances which are running.

callback

function()

The callback function.

Values in callback have the following properties:

Parameter

err

error

An error returned while making this request.

Value may be null.

vms

Array of VM

VM objects from this instance group.

apiResponse

object

The full API response.

See also

InstaceGroups: listInstances API Documentation

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('us-central1-a');
const instanceGroup = zone.instanceGroup('web-servers');

instanceGroup.getVMs(function(err, vms) {
  // `vms` is an array of `VM` objects.
});

//-
// To control how many API requests are made and page through the results
// manually, set `autoPaginate` to `false`.
//-
function callback(err, vms, nextQuery, apiResponse) {
  if (nextQuery) {
    // More results exist.
    instanceGroup.getVMs(nextQuery, callback);
  }
}

instanceGroup.getVMs({
  autoPaginate: false
}, callback);

//-
// If the callback is omitted, we'll return a Promise.
//-
instanceGroup.getVMs().then(function(data) {
  const vms = data[0];
});

remove

remove(vms, callback)

Remove one or more VMs from this instance group.

Parameter

vms

(VM or Array of VM)

VM instances to remove from this instance group.

callback

function()

The callback function.

Values in callback have the following properties:

Parameter

err

error

An error returned while making this request.

Value may be null.

operation

Operation

An operation object that can be used to check the status of the request.

apiResponse

object

The full API response.

See also

InstanceGroups: removeInstances API Documentation

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('us-central1-a');
const instanceGroup = zone.instanceGroup('web-servers');

const vms = [
  gce.zone('us-central1-a').vm('http-server'),
  gce.zone('us-central1-a').vm('https-server')
];

instanceGroup.remove(vms, function(err, operation, apiResponse) {
  // `operation` is an Operation object that can be used to check the status
  // of the request.
});

//-
// If the callback is omitted, we'll return a Promise.
//-
instanceGroup.remove(vms).then(function(data) {
  const operation = data[0];
  const apiResponse = data[1];
});

setPorts

setPorts(ports, callback)

Set the named ports for this instance group.

Parameter

ports

object

A map of names to ports. The key should be the name, and the value the port number.

callback

Optional

function()

The callback function.

Values in callback have the following properties:

Parameter

err

error

An error returned while making this request.

Value may be null.

operation

Operation

An operation object that can be used to check the status of the request.

apiResponse

object

The full API response.

See also

InstanceGroups: setNamedPorts API Documentation

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('us-central1-a');
const instanceGroup = zone.instanceGroup('web-servers');

const ports = {
  http: 80,
  https: 443
};

instanceGroup.setPorts(ports, function(err, operation, apiResponse) {
  // `operation` is an Operation object that can be used to check the status
  // of the request.
});

//-
// If the callback is omitted, we'll return a Promise.
//-
instanceGroup.setPorts(ports).then(function(data) {
  const operation = data[0];
  const apiResponse = data[1];
});