Skip to content

Commit a42326b

Browse files
compute: support Forwarding Rules
1 parent 5199cad commit a42326b

File tree

8 files changed

+1731
-107
lines changed

8 files changed

+1731
-107
lines changed

lib/compute/index.js

+213
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ var Operation = require('./operation.js');
4949
*/
5050
var Region = require('./region.js');
5151

52+
/**
53+
* @type {module:compute/rule}
54+
* @private
55+
*/
56+
var Rule = require('./rule.js');
57+
5258
/**
5359
* @type {module:common/service}
5460
* @private
@@ -311,6 +317,96 @@ Compute.prototype.createNetwork = function(name, config, callback) {
311317
});
312318
};
313319

320+
/**
321+
* Create a global forwarding rule.
322+
*
323+
* @resource [GlobalForwardingRule Resource]{@link https://cloud.google.com/compute/docs/reference/v1/globalForwardingRules#resource}
324+
* @resource [GlobalForwardingRules: insert API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/globalForwardingRules/insert}
325+
*
326+
* @param {string} name - Name of the rule.
327+
* @param {object} config - See a
328+
* [GlobalForwardingRule resource](https://cloud.google.com/compute/docs/reference/v1/globalForwardingRules#resource).
329+
* @param {string=} config.ip - The single IP address this forwarding rule will
330+
* match against. All traffic that matches the IP address, protocol, and
331+
* ports of this forwarding rule will be handled by this rule. If specified,
332+
* the IP address must be a static external IP address. To create a new
333+
* ephemeral external IP address for the forwarding rule, leave this field
334+
* empty. (Alias for `config.IPAddress`)
335+
* @param {string=} config.protocol - The type of protocol that this forwarding
336+
* rule matches. Valid values are `AH`, `ESP`, `SCTP`, `TCP`, `UDP`.
337+
* Default: `TCP`. (Alias for `config.IPProtocol`)
338+
* @param {string=} config.range - A single port or single contiguous port
339+
* range, ranging from low to high for which this forwarding rule matches.
340+
* Packets of the specified protocol sent to these ports will be forwarded
341+
* on to the appropriate target pool or target instance. If this field is
342+
* left empty, then the forwarding matches traffic for all ports for the
343+
* specified protocol. (Alias for `config.portRange`)
344+
* @param {string} config.target - The full or valid partial URL of the target
345+
* resource to receive the matched traffic. This target must be a global
346+
* `TargetHttpProxy` or `TargetHttpsProxy` resource.
347+
* @param {function} callback - The callback function.
348+
* @param {?error} callback.err - An error returned while making this request.
349+
* @param {module:compute/rule} callback.rule - The created Rule object.
350+
* @param {module:compute/operation} callback.operation - An operation object
351+
* that can be used to check the status of the request.
352+
* @param {object} callback.apiResponse - The full API response.
353+
*
354+
* @example
355+
* var name = 'new-rule-name';
356+
*
357+
* var config = {
358+
* target: 'global/targetHttpProxies/my-proxy',
359+
* range: '8080-8089'
360+
* };
361+
*
362+
* gce.createRule(name, config, function (err, rule, operation, apiResponse) {
363+
* // `rule` is a Rule object.
364+
*
365+
* // `operation` is an Operation object that can be used to check the status
366+
* // of the request.
367+
* });
368+
*/
369+
Compute.prototype.createRule = function(name, config, callback) {
370+
var self = this;
371+
372+
var body = extend({}, config, {
373+
name: name
374+
});
375+
376+
if (body.ip) {
377+
body.IPAddress = body.ip;
378+
delete body.ip;
379+
}
380+
381+
if (body.protocol) {
382+
body.IPProtocol = body.protocol;
383+
delete body.protocol;
384+
}
385+
386+
if (body.range) {
387+
body.portRange = body.range;
388+
delete body.range;
389+
}
390+
391+
this.request({
392+
method: 'POST',
393+
uri: '/global/forwardingRules',
394+
json: body
395+
}, function(err, resp) {
396+
if (err) {
397+
callback(err, null, null, resp);
398+
return;
399+
}
400+
401+
var rule = self.rule(name);
402+
403+
var operation = self.operation(resp.name);
404+
operation.metadata = resp;
405+
406+
callback(null, rule, operation, resp);
407+
});
408+
};
409+
314410
/**
315411
* Create a backend service.
316412
*
@@ -1159,6 +1255,109 @@ Compute.prototype.getRegions = function(options, callback) {
11591255
});
11601256
};
11611257

1258+
/**
1259+
* Get a list of forwading rules.
1260+
*
1261+
* @resource [GlobalForwardingRules: list API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/globalForwardingRules/list}
1262+
*
1263+
* @param {object=} options - Rules search options.
1264+
* @param {boolean} options.autoPaginate - Have pagination handled
1265+
* automatically. Default: true.
1266+
* @param {string} options.filter - Search filter in the format of
1267+
* `{name} {comparison} {filterString}`.
1268+
* - **`name`**: the name of the field to compare
1269+
* - **`comparison`**: the comparison operator, `eq` (equal) or `ne`
1270+
* (not equal)
1271+
* - **`filterString`**: the string to filter to. For string fields, this
1272+
* can be a regular expression.
1273+
* @param {number} options.maxResults - Maximum number of rules to return.
1274+
* @param {string} options.pageToken - A previously-returned page token
1275+
* representing part of the larger set of results to view.
1276+
* @param {function} callback - The callback function.
1277+
* @param {?error} callback.err - An error returned while making this request.
1278+
* @param {module:compute/rule} callback.rules - Rule objects from this region.
1279+
* @param {?object} callback.nextQuery - If present, query with this object to
1280+
* check for more results.
1281+
* @param {object} callback.apiResponse - The full API response.
1282+
*
1283+
* @example
1284+
* gce.getRules(function(err, rules) {
1285+
* // `rules` is an array of `Rule` objects.
1286+
* });
1287+
*
1288+
* //-
1289+
* // To control how many API requests are made and page through the results
1290+
* // manually, set `autoPaginate` to `false`.
1291+
* //-
1292+
* function callback(err, rules, nextQuery, apiResponse) {
1293+
* if (nextQuery) {
1294+
* // More results exist.
1295+
* gce.getRules(nextQuery, callback);
1296+
* }
1297+
* }
1298+
*
1299+
* gce.getRules({
1300+
* autoPaginate: false
1301+
* }, callback);
1302+
*
1303+
* //-
1304+
* // Get the rules from your project as a readable object stream.
1305+
* //-
1306+
* gce.getRules()
1307+
* .on('error', console.error)
1308+
* .on('data', function(rule) {
1309+
* // `rule` is a `Rule` object.
1310+
* })
1311+
* .on('end', function() {
1312+
* // All rules retrieved.
1313+
* });
1314+
*
1315+
* //-
1316+
* // If you anticipate many results, you can end a stream early to prevent
1317+
* // unnecessary processing and API requests.
1318+
* //-
1319+
* gce.getRules()
1320+
* .on('data', function(rule) {
1321+
* this.end();
1322+
* });
1323+
*/
1324+
Compute.prototype.getRules = function(options, callback) {
1325+
var self = this;
1326+
1327+
if (is.fn(options)) {
1328+
callback = options;
1329+
options = {};
1330+
}
1331+
1332+
options = options || {};
1333+
1334+
this.request({
1335+
uri: '/global/forwardingRules',
1336+
qs: options
1337+
}, function(err, resp) {
1338+
if (err) {
1339+
callback(err, null, null, resp);
1340+
return;
1341+
}
1342+
1343+
var nextQuery = null;
1344+
1345+
if (resp.nextPageToken) {
1346+
nextQuery = extend({}, options, {
1347+
pageToken: resp.nextPageToken
1348+
});
1349+
}
1350+
1351+
var rules = (resp.items || []).map(function(rule) {
1352+
var ruleInstance = self.rule(rule.name);
1353+
ruleInstance.metadata = rule;
1354+
return ruleInstance;
1355+
});
1356+
1357+
callback(null, rules, nextQuery, resp);
1358+
});
1359+
};
1360+
11621361
/**
11631362
* Get a list of backend services.
11641363
*
@@ -1630,6 +1829,19 @@ Compute.prototype.region = function(name) {
16301829
return new Region(this, name);
16311830
};
16321831

1832+
/**
1833+
* Get a reference to a Google Compute Engine forwading rule.
1834+
*
1835+
* @param {string} name - Name of the rule.
1836+
* @return {module:compute/rule}
1837+
*
1838+
* @example
1839+
* var rule = gce.rule('rule-name');
1840+
*/
1841+
Compute.prototype.rule = function(name) {
1842+
return new Rule(this, name);
1843+
};
1844+
16331845
/**
16341846
* Get a reference to a Google Compute Engine backend service.
16351847
*
@@ -1688,6 +1900,7 @@ streamRouter.extend(Compute, [
16881900
'getNetworks',
16891901
'getOperations',
16901902
'getRegions',
1903+
'getRules',
16911904
'getServices',
16921905
'getSnapshots',
16931906
'getVMs',

0 commit comments

Comments
 (0)