Rule

Rule

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).

Constructor

new Rule(scope, name)

Parameters:
Name Type Description
scope Compute | Region

The parent scope this firewall rule belongs to.

name string

Rule name.

See:
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');

Members

id

scope

Methods

create(config)

Create a forwarding rule.

Parameters:
Name Type Description
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(callbackopt)

Delete the rule.

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

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

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

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.

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

Get the metadata of this rule.

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

apiResponse object

The full API response.

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

Set the target for this forwarding rule.

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

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

apiResponse object

The full API response.

See:
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];
});