Constructor

Operation

new Operation(scope, name)

An Operation object allows you to interact with a Google Compute Engine operation.

An operation can be a GlobalOperation, RegionOperation, or ZoneOperation.

Parameter

scope

(Compute, Zone, or Region)

The scope of the operation: a Compute, Zone, or Region object.

name

string

Operation name.

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();

//-
// Reference a global operation.
//-
const operation = compute.operation('operation-id');

//-
// Reference a region operation.
//-
const region = compute.region('us-central1');
const operation = region.operation('operation-id');

//-
// Reference a zone operation.
//-
const zone = compute.zone('us-central1-a');
const operation = zone.operation('operation-id');

//-
// All operations are event emitters. The status of each operation is polled
// continuously, starting only after you register a "complete" listener.
//-
operation.on('complete', function(metadata) {
  // The operation is complete.
});

//-
// You can register a listener to monitor when the operation begins running.
//-
operation.on('running', function(metadata) {
  // The operation is running.
});

//-
// Be sure to register an error handler as well to catch any issues which
// impeded the operation.
//-
operation.on('error', function(err) {
  // An error occurred during the operation.
});

//-
// To force the Operation object to stop polling for updates, simply remove
// any "complete" listeners you've registered.
//
// The easiest way to do this is with `removeAllListeners()`.
//-
operation.removeAllListeners();

Properties

id

string

name

string

Methods

delete

delete(callback)

Delete the operation.

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.

apiResponse

object

The full API response.

See also

GlobalOperations: delete API Documentation

RegionOperations: delete API Documentation

ZoneOperations: delete API Documentation

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const operation = compute.operation('operation-id');

operation.delete(function(err, apiResponse) {});

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

exists

exists(callback)

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

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const operation = compute.operation('operation-id');

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

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

get

get()

Get a Project object.

Example

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

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

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

get

get()

Get an operation if it exists.

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const operation = compute.operation('operation-id');

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

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

getMetadata

getMetadata(callback)

Get the operation's metadata. For a detailed description of metadata see Operation resource.

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 disk's metadata.

apiResponse

object

The full API response.

See also

GlobalOperations: get API Documentation

RegionOperations: get API Documentation

ZoneOperations: get API Documentation

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();

operation.getMetadata(function(err, metadata, apiResponse) {
  // `metadata.error`: Contains errors if the operation failed.
  // `metadata.warnings`: Contains warnings.
});

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

getMetadata

getMetadata(callback)

Get the project'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 machine type's metadata.

apiResponse

object

The full API response.

See also

Projects: get API Documentation

Projects Resource

Example

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

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

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