Network

Network

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

Constructor

new Network(compute, name)

Parameters:
Name Type Description
compute Compute
name strign
See:
Example
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const network = compute.network('network-name');

Members

compute :Compute

The parent Compute instance of this Network instance.

formattedName :string

id :string

name :string

Methods

create(config)

Create a network.

Parameters:
Name Type Description
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(name, config, callback)

Create a firewall for this network.

Parameters:
Name Type Description
name string

Name of the firewall.

config object

See a Firewall resource.

Properties
Name Type Description
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.<string>

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

callback function

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

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:
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(name, config, callback)

Create a subnetwork in this network.

Parameters:
Name Type Description
name string

Name of the subnetwork.

config object

See a Subnetwork resource.

Properties
Name Type Description
region Region | 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.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

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:
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(callbackopt)

Delete the network.

Parameters:
Name Type Attributes Description
callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

operation Operation

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

apiResponse object

The full API response.

See:
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(callback)

Check if the network exists.

Parameters:
Name Type Description
callback function

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

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(name)

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

Parameters:
Name Type Description
name string

Name of the firewall.

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

get(optionsopt)

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.

Parameters:
Name Type Attributes Description
options options <optional>

Configuration object.

Properties
Name Type Description
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(optionsopt, callback)

Get a list of firewalls for this network.

Parameters:
Name Type Attributes Description
options object <optional>

Firewall search options.

Properties
Name Type Description
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.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

firewalls Array.<Firewall>

Firewall objects from this network.

apiResponse object

The full API response.

See:
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(optionsopt) → {stream}

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

Parameters:
Name Type Attributes Description
options object <optional>

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

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(callbackopt)

Get the network's metadata.

Parameters:
Name Type Attributes Description
callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

metadata object

The network's metadata.

apiResponse object

The full API response.

See:
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(optionsopt, callback)

Get a list of subnetworks in this network.

Parameters:
Name Type Attributes Description
options object <optional>

Subnetwork search options.

Properties
Name Type Description
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.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

subnetworks Array.<Subnetwork>

Subnetwork objects from this network.

apiResponse object

The full API response.

See:
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(optionsopt) → {stream}

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

Parameters:
Name Type Attributes Description
options object <optional>

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

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();
  });