Skip to content

Commit

Permalink
compute: support Forwarding Rules
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Apr 15, 2016
1 parent 5199cad commit 4a5dd15
Show file tree
Hide file tree
Showing 7 changed files with 1,166 additions and 106 deletions.
184 changes: 184 additions & 0 deletions lib/compute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ var Operation = require('./operation.js');
*/
var Region = require('./region.js');

/**
* @type {module:compute/rule}
* @private
*/
var Rule = require('./rule.js');

/**
* @type {module:common/service}
* @private
Expand Down Expand Up @@ -311,6 +317,67 @@ Compute.prototype.createNetwork = function(name, config, callback) {
});
};

/**
* Create a global forwarding rule.
*
* @resource [GlobalForwardingRule Resource]{@link https://cloud.google.com/compute/docs/reference/v1/globalForwardingRules#resource}
* @resource [GlobalForwardingRules: insert API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/globalForwardingRules/insert}
*
* @param {string} name - Name of the rule.
* @param {object} config - See a
* [GlobalForwardingRule resource](https://cloud.google.com/compute/docs/reference/v1/globalForwardingRules#resource).
* @param {string} config.target - The full or valid partial URL of the target
* resource to receive the matched traffic. This target must be a global
* `TargetHttpProxy` or `TargetHttpsProxy` resource.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request.
* @param {module:compute/rule} callback.rule - The created Rule object.
* @param {module:compute/operation} callback.operation - An operation object
* that can be used to check the status of the request.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* var name = 'new-rule-name';
*
* var config = {
* target: '...',
* portRange: '8080',
* IPProtocol: 'TCP'
* };
*
* gce.createRule(name, config, function (err, rule, operation, apiResponse) {
* // `rule` is a Rule object.
*
* // `operation` is an Operation object that can be used to check the status
* // of the request.
* });
*/
Compute.prototype.createRule = function(name, config, callback) {
var self = this;

var body = extend({}, config, {
name: name
});

this.request({
method: 'POST',
uri: '/global/forwardingRules',
json: body
}, function(err, resp) {
if (err) {
callback(err, null, null, resp);
return;
}

var rule = self.rule(name);

var operation = self.operation(resp.name);
operation.metadata = resp;

callback(null, rule, operation, resp);
});
};

/**
* Create a backend service.
*
Expand Down Expand Up @@ -1159,6 +1226,109 @@ Compute.prototype.getRegions = function(options, callback) {
});
};

/**
* Get a list of forwading rules.
*
* @resource [GlobalForwardingRules: list API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/globalForwardingRules/list}
*
* @param {object=} options - Rules search options.
* @param {boolean} options.autoPaginate - Have pagination handled
* automatically. Default: true.
* @param {string} options.filter - Search filter in the format of
* `{name} {comparison} {filterString}`.
* - **`name`**: the name of the field to compare
* - **`comparison`**: the comparison operator, `eq` (equal) or `ne`
* (not equal)
* - **`filterString`**: the string to filter to. For string fields, this
* can be a regular expression.
* @param {number} options.maxResults - Maximum number of rules to return.
* @param {string} options.pageToken - A previously-returned page token
* representing part of the larger set of results to view.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request.
* @param {module:compute/rule} callback.rules - Rule objects from this region.
* @param {?object} callback.nextQuery - If present, query with this object to
* check for more results.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* gce.getRules(function(err, rules) {
* // `rules` is an array of `Rule` objects.
* });
*
* //-
* // To control how many API requests are made and page through the results
* // manually, set `autoPaginate` to `false`.
* //-
* function callback(err, rules, nextQuery, apiResponse) {
* if (nextQuery) {
* // More results exist.
* gce.getRules(nextQuery, callback);
* }
* }
*
* gce.getRules({
* autoPaginate: false
* }, callback);
*
* //-
* // Get the rules from your project as a readable object stream.
* //-
* gce.getRules()
* .on('error', console.error)
* .on('data', function(rule) {
* // `rule` is a `Rule` object.
* })
* .on('end', function() {
* // All rules retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* //-
* gce.getRules()
* .on('data', function(rule) {
* this.end();
* });
*/
Compute.prototype.getRules = function(options, callback) {
var self = this;

if (is.fn(options)) {
callback = options;
options = {};
}

options = options || {};

this.request({
uri: '/global/forwardingRules',
qs: options
}, function(err, resp) {
if (err) {
callback(err, null, null, resp);
return;
}

var nextQuery = null;

if (resp.nextPageToken) {
nextQuery = extend({}, options, {
pageToken: resp.nextPageToken
});
}

var rules = (resp.items || []).map(function(rule) {
var ruleInstance = self.rule(rule.name);
ruleInstance.metadata = rule;
return ruleInstance;
});

callback(null, rules, nextQuery, resp);
});
};

/**
* Get a list of backend services.
*
Expand Down Expand Up @@ -1630,6 +1800,19 @@ Compute.prototype.region = function(name) {
return new Region(this, name);
};

/**
* Get a reference to a Google Compute Engine forwading rule.
*
* @param {string} name - Name of the rule.
* @return {module:compute/rule}
*
* @example
* var rule = gce.rule('rule-name');
*/
Compute.prototype.rule = function(name) {
return new Rule(this, name);
};

/**
* Get a reference to a Google Compute Engine backend service.
*
Expand Down Expand Up @@ -1688,6 +1871,7 @@ streamRouter.extend(Compute, [
'getNetworks',
'getOperations',
'getRegions',
'getRules',
'getServices',
'getSnapshots',
'getVMs',
Expand Down
Loading

0 comments on commit 4a5dd15

Please sign in to comment.