Constructor

Rule

new Rule(scope, name)

Forwarding rules work in conjunction with target pools and target instances to support load balancing and protocol forwarding features. To use load balancing and protocol forwarding, you must create a forwarding rule that directs traffic to specific target pools (for load balancing) or target instances (for protocol forwarding).

Parameter

scope

(Compute or Region)

The parent scope this firewall rule belongs to.

name

string

Rule name.

See also

Forwarding rules

Example

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

//-
// Reference a global rule.
//-
const rule = compute.rule('rule-name');

//-
// Reference a region rule.
//-
const region = compute.region('us-central1');
const rule = region.rule('rule-name');

Properties

id

string

scope

(Compute or Region)

Methods

create

create(config)

Create a forwarding rule.

Parameter

config

object

See Compute#createRule or Region#createRule if accessing this object through a Region.

Example

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

const config = {
  // `target` will be different depending of this is a Regional or Global
  // forwarding rule
  target: 'global/targetHttpProxies/my-proxy',
  portRange: '8080-8089'
};

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

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

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

delete

delete(callback)

Delete the rule.

Parameter

callback

Optional

function()

The callback function.

See also

GlobalForwardingRules: delete API Documentation

ForwardingRules: delete API Documentation

Example

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

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

exists

exists(callback)

Check if the forwarding rule exists.

Parameter

callback

function()

The callback function.

Example

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

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

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

get

get(options)

Get a forwarding rule 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.

Example

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

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

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

getMetadata

getMetadata(callback)

Get the metadata of this rule.

Parameter

callback

Optional

function()

The callback function.

See also

GlobalForwardingRule Resource

ForwardingRule Resource

GlobalForwardingRules: get API Documentation

ForwardingRules: get API Documentation

Example

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

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

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

setTarget

setTarget(target, callback)

Set the target for this forwarding rule.

Parameter

target

string

The full or valid partial URL of the target resource to receive the matched traffic. For regional forwarding rules, this target must live in the same region as the forwarding rule. For global forwarding rules, this target must be a global TargetHttpProxy resource.

callback

function()

The callback function.

See also

GlobalForwardingRules: setTarget API Documentation

ForwardingRules: setTarget API Documentation

Example

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

rule.setTarget('new-target', 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.
//-
rule.setTarget('new-target').then(function(data) {
  const operation = data[0];
  const apiResponse = data[1];
});