Skip to content

Commit 72e11d5

Browse files
compute: support Forwarding Rules
1 parent 1877e0c commit 72e11d5

File tree

5 files changed

+876
-50
lines changed

5 files changed

+876
-50
lines changed

lib/compute/index.js

+176
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,59 @@ 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+
* compute.createRule('new-rule', function (err, rule, operation, apiResponse) {
341+
* // `rule` is a Rule object.
342+
*
343+
* // `operation` is an Operation object that can be used to check the status
344+
* // of the request.
345+
* });
346+
*/
347+
Compute.prototype.createRule = function(name, config, callback) {
348+
var self = this;
349+
350+
var body = extend({}, config, {
351+
name: name
352+
});
353+
354+
this.request({
355+
method: 'POST',
356+
uri: '/global/forwardingRules',
357+
json: body
358+
}, function(err, resp) {
359+
if (err) {
360+
callback(err, null, null, resp);
361+
return;
362+
}
363+
364+
var rule = self.rule(name);
365+
366+
var operation = self.operation(resp.name);
367+
operation.metadata = resp;
368+
369+
callback(null, rule, operation, resp);
370+
});
371+
};
372+
314373
/**
315374
* Create a backend service.
316375
*
@@ -1159,6 +1218,109 @@ Compute.prototype.getRegions = function(options, callback) {
11591218
});
11601219
};
11611220

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

1795+
/**
1796+
* Get a reference to a Google Compute Engine forwading rule.
1797+
*
1798+
* @param {string} name - Name of the rule.
1799+
* @return {module:compute/rule}
1800+
*
1801+
* @example
1802+
* var rule = compute.rule('rule-name');
1803+
*/
1804+
Compute.prototype.rule = function(name) {
1805+
return new Rule(this, name);
1806+
};
1807+
16331808
/**
16341809
* Get a reference to a Google Compute Engine backend service.
16351810
*
@@ -1688,6 +1863,7 @@ streamRouter.extend(Compute, [
16881863
'getNetworks',
16891864
'getOperations',
16901865
'getRegions',
1866+
'getRules',
16911867
'getServices',
16921868
'getSnapshots',
16931869
'getVMs',

0 commit comments

Comments
 (0)