Properties

new Rule(scope, name)

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

Parameters

Name Type Optional Description

scope

 

 

The parent scope this firewall rule belongs to.

name

 

 

Rule name.

See also

Forwarding rules

Properties

id  string

scope  (Compute or Region)

Methods

create(config)

Create a forwarding rule.

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

Parameter

Name Type Optional Description

config

object

 

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

delete([callback])

Delete the rule.

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

Parameters

Name Type Optional Description

callback

function()

Yes

The callback function.

Values in callback have the following properties:

Name Type Optional Description

err

error

 

An error returned while making this request.

Value can 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

GlobalForwardingRules: delete API Documentation

ForwardingRules: delete API Documentation

exists(callback)

Check if the forwarding rule exists.

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

Parameters

Name Type Optional Description

callback

function()

 

The callback function.

Values in callback have the following properties:

Name Type Optional Description

err

error

 

An error returned while making this request.

Value can be null.

exists

boolean

 

Whether the rule exists or not.

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.

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

Parameters

Name Type Optional Description

options

options

Yes

Configuration object.

Values in options have the following properties:

Name Type Optional Description

autoCreate

boolean

 

Automatically create the object if it does not exist. Default: false

getMetadata([callback])

Get the metadata of this rule.

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

Parameters

Name Type Optional Description

callback

function()

Yes

The callback function.

Values in callback have the following properties:

Name Type Optional Description

err

error

 

An error returned while making this request.

Value can be null.

metadata

object

 

The rule's metadata.

apiResponse

object

 

The full API response.

See also

GlobalForwardingRule Resource

ForwardingRule Resource

GlobalForwardingRules: get API Documentation

ForwardingRules: get API Documentation

setTarget(target, callback)

Set the target for this forwarding rule.

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

Parameters

Name Type Optional 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.

Values in callback have the following properties:

Name Type Optional Description

err

error

 

An error returned while making this request.

Value can be null.

apiResponse

object

 

The full API response.

See also

GlobalForwardingRules: setTarget API Documentation

ForwardingRules: setTarget API Documentation