Constructor

Network

new Network(compute, name)

A Network object allows you to interact with a Google Compute Engine network.

Parameter

compute

Compute

name

strign

See also

Networks Overview

Network Resource

Example

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

Properties

compute

Compute

The parent Compute instance of this Network instance.

formattedName

string

id

string

name

string

Methods

create

create(config)

Create a network.

Parameter

config

object

See Compute#createNetwork.

Example

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

const config = {
  // ...
};

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

  // `operation` is an Operation object that can be used to check the
  // status of network creation.
});


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

createFirewall

createFirewall(name, config, callback)

Create a firewall for this network.

Parameter

name

string

Name of the firewall.

config

object

See a Firewall resource.

Values in config have the following properties:

Parameter

protocols

object

A map of protocol to port range. The keys of the object refer to a protocol (e.g. tcp, udp) and the value for the key are the ports/port-ranges that are allowed to make a connection.

ranges

Array of string

The IP address blocks that this rule applies to, expressed in CIDR format.

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.

firewall

Firewall

The created Firewall object.

operation

Operation

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

apiResponse

object

The full API response.

See also

Firewalls Overview

Firewalls: insert API Documentation

Example

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

const config = {
  protocols: {
    tcp: [3000],
    udp: [] // An empty array means all ports are allowed.
  },

  ranges: ['0.0.0.0/0']
};

function callback(err, firewall, operation, apiResponse) {
  // `firewall` is a Firewall object.

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

network.createFirewall('new-firewall-name', config, callback);

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

createSubnetwork

createSubnetwork(name, config, callback)

Create a subnetwork in this network.

Parameter

name

string

Name of the subnetwork.

config

object

See a Subnetwork resource.

Values in config have the following properties:

Parameter

region

(Region or string)

The region where the Subnetwork resides.

range

string

The range of internal addresses that are owned by this subnetwork. CIDR range of addresses that are legal on this network. (Alias for config.ipCidrRange)

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.

subnetwork

Subnetwork

The created Subnetwork object.

operation

Operation

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

apiResponse

object

The full API response.

See also

Subnetwork Resource

Subnetwork: insert API Documentation

Example

const Compute = require('@google-cloud/compute');
const compute = new Compute();
const network = compute.network('network-name');
const region = compute.region('us-east1');

const config = {
  region: region,
  range: '10.0.1.0/24'
};

function callback(err, subnetwork, operation, apiResponse) {
  // `subnetwork` is a Subnetwork object.

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

network.createSubnetwork('new-subnetwork-name', config, callback);

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

delete

delete(callback)

Delete the network.

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

Networks: delete API Documentation

Example

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

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

exists

exists(callback)

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

Example

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

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

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

firewall

firewall(name)

Get a reference to a Google Compute Engine firewall in this network.

Parameter

name

string

Name of the firewall.

See also

Firewalls Overview

Example

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

get

get(options)

Get a network 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 network = compute.network('network-name');

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

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

getFirewalls

getFirewalls(options, callback)

Get a list of firewalls for this network.

Parameter

options

Optional

object

Firewall search options.

Values in options have the following properties:

Parameter

autoPaginate

boolean

Have pagination handled automatically. Default: true.

maxApiCalls

number

Maximum number of API calls to make.

maxResults

number

Maximum number of firewalls to return.

pageToken

string

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

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.

firewalls

Array of Firewall

Firewall objects from this network.

apiResponse

object

The full API response.

See also

Firewalls Overview

Firewalls: list API Documentation

Example

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

network.getFirewalls(function(err, firewalls) {
  // `firewalls` is an array of `Firewall` objects.
});

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

network.getFirewalls({
  autoPaginate: false
}, callback);

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

getFirewallsStream

getFirewallsStream(options) returns stream

Get a list of Firewall objects for this network as a readable object stream.

Parameter

options

Optional

object

Configuration object. See Network#getFirewalls for a complete list of options.

Returns

stream 

Example

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

network.getFirewallsStream()
  .on('error', console.error)
  .on('data', function(firewall) {
    // `firewall` is a `Firewall` object.
  })
  .on('end', function() {
    // All firewalls retrieved.
  });

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

getMetadata

getMetadata(callback)

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

apiResponse

object

The full API response.

See also

Network Resource

Networks: get API Documentation

Example

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

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

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

getSubnetworks

getSubnetworks(options, callback)

Get a list of subnetworks in this network.

Parameter

options

Optional

object

Subnetwork 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 subnetworks to return.

pageToken

string

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

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.

subnetworks

Array of Subnetwork

Subnetwork objects from this network.

apiResponse

object

The full API response.

See also

Subnetworks Overview

Subnetworks: list API Documentation

Example

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

network.getSubnetworks(function(err, subnetworks) {
  // `subnetworks` is an array of `Subnetworks` objects.
});

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

network.getSubnetworks({
  autoPaginate: false
}, callback);

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

getSubnetworksStream

getSubnetworksStream(options) returns stream

Get a Subnetwork list within this network as a readable object stream.

Parameter

options

Optional

object

Configuration object. See Network#getSubnetworks for a complete list of options.

Returns

stream 

Example

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

network.getSubnetworksStream()
  .on('error', console.error)
  .on('data', function(subnetwork) {
    // `subnetwork` is a `Subnetwork` object.
  })
  .on('end', function() {
    // All subnetworks retrieved.
  });

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