Skip to content

Commit 18e073d

Browse files
compute: support Forwarding Rules
1 parent 1877e0c commit 18e073d

File tree

7 files changed

+907
-57
lines changed

7 files changed

+907
-57
lines changed

lib/compute/index.js

+184
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,67 @@ 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.target - The full or valid partial URL of the target
330+
* resource to receive the matched traffic. This target must be a global
331+
* `TargetHttpProxy` or `TargetHttpsProxy` resource.
332+
* @param {function} callback - The callback function.
333+
* @param {?error} callback.err - An error returned while making this request.
334+
* @param {module:compute/rule} callback.rule - The created Rule object.
335+
* @param {module:compute/operation} callback.operation - An operation object
336+
* that can be used to check the status of the request.
337+
* @param {object} callback.apiResponse - The full API response.
338+
*
339+
* @example
340+
* var name = 'new-rule-name';
341+
*
342+
* var config = {
343+
* target: '...',
344+
* portRange: '8080',
345+
* IPProtocol: 'TCP'
346+
* };
347+
*
348+
* gce.createRule(name, config, function (err, rule, operation, apiResponse) {
349+
* // `rule` is a Rule object.
350+
*
351+
* // `operation` is an Operation object that can be used to check the status
352+
* // of the request.
353+
* });
354+
*/
355+
Compute.prototype.createRule = function(name, config, callback) {
356+
var self = this;
357+
358+
var body = extend({}, config, {
359+
name: name
360+
});
361+
362+
this.request({
363+
method: 'POST',
364+
uri: '/global/forwardingRules',
365+
json: body
366+
}, function(err, resp) {
367+
if (err) {
368+
callback(err, null, null, resp);
369+
return;
370+
}
371+
372+
var rule = self.rule(name);
373+
374+
var operation = self.operation(resp.name);
375+
operation.metadata = resp;
376+
377+
callback(null, rule, operation, resp);
378+
});
379+
};
380+
314381
/**
315382
* Create a backend service.
316383
*
@@ -1159,6 +1226,109 @@ Compute.prototype.getRegions = function(options, callback) {
11591226
});
11601227
};
11611228

1229+
/**
1230+
* Get a list of forwading rules.
1231+
*
1232+
* @resource [GlobalForwardingRules: list API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/globalForwardingRules/list}
1233+
*
1234+
* @param {object=} options - Rules search options.
1235+
* @param {boolean} options.autoPaginate - Have pagination handled
1236+
* automatically. Default: true.
1237+
* @param {string} options.filter - Search filter in the format of
1238+
* `{name} {comparison} {filterString}`.
1239+
* - **`name`**: the name of the field to compare
1240+
* - **`comparison`**: the comparison operator, `eq` (equal) or `ne`
1241+
* (not equal)
1242+
* - **`filterString`**: the string to filter to. For string fields, this
1243+
* can be a regular expression.
1244+
* @param {number} options.maxResults - Maximum number of rules to return.
1245+
* @param {string} options.pageToken - A previously-returned page token
1246+
* representing part of the larger set of results to view.
1247+
* @param {function} callback - The callback function.
1248+
* @param {?error} callback.err - An error returned while making this request.
1249+
* @param {module:compute/rule} callback.rules - Rule objects from this region.
1250+
* @param {?object} callback.nextQuery - If present, query with this object to
1251+
* check for more results.
1252+
* @param {object} callback.apiResponse - The full API response.
1253+
*
1254+
* @example
1255+
* gce.getRules(function(err, rules) {
1256+
* // `rules` is an array of `Rule` objects.
1257+
* });
1258+
*
1259+
* //-
1260+
* // To control how many API requests are made and page through the results
1261+
* // manually, set `autoPaginate` to `false`.
1262+
* //-
1263+
* function callback(err, rules, nextQuery, apiResponse) {
1264+
* if (nextQuery) {
1265+
* // More results exist.
1266+
* gce.getRules(nextQuery, callback);
1267+
* }
1268+
* }
1269+
*
1270+
* gce.getRules({
1271+
* autoPaginate: false
1272+
* }, callback);
1273+
*
1274+
* //-
1275+
* // Get the rules from your project as a readable object stream.
1276+
* //-
1277+
* gce.getRules()
1278+
* .on('error', console.error)
1279+
* .on('data', function(rule) {
1280+
* // `rule` is a `Rule` object.
1281+
* })
1282+
* .on('end', function() {
1283+
* // All rules retrieved.
1284+
* });
1285+
*
1286+
* //-
1287+
* // If you anticipate many results, you can end a stream early to prevent
1288+
* // unnecessary processing and API requests.
1289+
* //-
1290+
* gce.getRules()
1291+
* .on('data', function(rule) {
1292+
* this.end();
1293+
* });
1294+
*/
1295+
Compute.prototype.getRules = function(options, callback) {
1296+
var self = this;
1297+
1298+
if (is.fn(options)) {
1299+
callback = options;
1300+
options = {};
1301+
}
1302+
1303+
options = options || {};
1304+
1305+
this.request({
1306+
uri: '/global/forwardingRules',
1307+
qs: options
1308+
}, function(err, resp) {
1309+
if (err) {
1310+
callback(err, null, null, resp);
1311+
return;
1312+
}
1313+
1314+
var nextQuery = null;
1315+
1316+
if (resp.nextPageToken) {
1317+
nextQuery = extend({}, options, {
1318+
pageToken: resp.nextPageToken
1319+
});
1320+
}
1321+
1322+
var rules = (resp.items || []).map(function(rule) {
1323+
var ruleInstance = self.rule(rule.name);
1324+
ruleInstance.metadata = rule;
1325+
return ruleInstance;
1326+
});
1327+
1328+
callback(null, rules, nextQuery, resp);
1329+
});
1330+
};
1331+
11621332
/**
11631333
* Get a list of backend services.
11641334
*
@@ -1630,6 +1800,19 @@ Compute.prototype.region = function(name) {
16301800
return new Region(this, name);
16311801
};
16321802

1803+
/**
1804+
* Get a reference to a Google Compute Engine forwading rule.
1805+
*
1806+
* @param {string} name - Name of the rule.
1807+
* @return {module:compute/rule}
1808+
*
1809+
* @example
1810+
* var rule = gce.rule('rule-name');
1811+
*/
1812+
Compute.prototype.rule = function(name) {
1813+
return new Rule(this, name);
1814+
};
1815+
16331816
/**
16341817
* Get a reference to a Google Compute Engine backend service.
16351818
*
@@ -1688,6 +1871,7 @@ streamRouter.extend(Compute, [
16881871
'getNetworks',
16891872
'getOperations',
16901873
'getRegions',
1874+
'getRules',
16911875
'getServices',
16921876
'getSnapshots',
16931877
'getVMs',

0 commit comments

Comments
 (0)