Constructor

VM

new VM(zone, name)

An Instance object allows you to interact with a Google Compute Engine instance.

Parameter

zone

Zone

Zone object this instance belongs to.

name

string

Name of the instance.

See also

Instances and Networks

Instance Resource

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');

Properties

id

string

name

string

zone

Zone

The parent Zone instance of this VM instance.

Methods

attachDisk

attachDisk(disk, options, callback)

Attach a disk to the instance.

Parameter

disk

module:compute/disk

The disk to attach.

options

Optional

object

See the Instances: attachDisk request body.

Values in options have the following properties:

Parameter

readOnly

boolean

Attach the disk in read-only mode. (Alias for options.mode = READ_ONLY)

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

Disks Overview

Disk Resource

Instance: attachDisk API Documentation

Throws

Error 

if a {module:compute/disk} is not provided.

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');

const disk = zone.disk('my-disk');

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

vm.attachDisk(disk, callback);

//-
// Provide an options object to customize the request.
//-
const options = {
  autoDelete: true,
  readOnly: true
};

vm.attachDisk(disk, options, callback);

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

create

create(config)

Create a virtual machine.

Parameter

config

object

See {Zone#createVM}.

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');

const config = {
  // ...
};

vm.create(config, function(err, vm, operation, apiResponse) {
  // `vm` is a VM object.

  // `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.
//-
vm.create(config).then(function(data) {
  const vm = data[0];
  const operation = data[1];
  const apiResponse = data[2];
});

delete

delete(callback)

Delete the instance.

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

Instance: delete API Documentation

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');

vm.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.
//-
vm.delete().then(function(data) {
  const operation = data[0];
  const apiResponse = data[1];
});

detachDisk

detachDisk(deviceName, callback)

Detach a disk from the instance.

Parameter

deviceName

(module:compute/disk or string)

The device name of the disk to detach. If a Disk object is provided, we try to find the device name automatically by searching through the attached disks on the instance.

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

Instance: detachDisk API Documentation

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');

const disk = zone.disk('my-disk');

vm.detachDisk(disk, 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.
//-
vm.detachDisk(disk).then(function(data) {
  const operation = data[0];
  const apiResponse = data[1];
});

exists

exists(callback)

Check if the vm 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 vm exists or not.

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');

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

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

get

get(options)

Get a virtual machine 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('zone-name');
const vm = zone.vm('vm-name');

vm.get(function(err, vm, apiResponse) {
  // `vm` is a VM object.
});

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

getMetadata

getMetadata(callback)

Get the instance'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's metadata.

apiResponse

object

The full API response.

See also

Instance Resource

Instance: get API Documentation

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');

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

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

getSerialPortOutput

getSerialPortOutput(port, callback)

Returns the serial port output for the instance.

Parameter

port

Optional

number

The port from which the output is retrieved (1-4). Default: 1.

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.

output

object

The output from the port.

apiResponse

object

The full API response.

See also

Instances: getSerialPortOutput API Documentation

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = compute.zone('zone-name');
const vm = zone.vm('vm-name');

vm.getSerialPortOutput(function(err, output, apiResponse) {});

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

getTags

getTags(callback)

Get the instance's tags and their fingerprint.

This method wraps {module:compute/vm#getMetadata}, returning only the tags property.

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.

tags

Array of object

Tag objects from this VM.

fingerprint

string

The current tag fingerprint.

apiResponse