Skip to content

Commit

Permalink
Merge pull request #1159 from stephenplusplus/spp--1073-backend-services
Browse files Browse the repository at this point in the history
compute: support BackendServices
  • Loading branch information
callmehiphop committed Mar 21, 2016
2 parents 3965972 + e991601 commit 1a2b2da
Show file tree
Hide file tree
Showing 7 changed files with 1,268 additions and 51 deletions.
3 changes: 3 additions & 0 deletions docs/toc.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
}, {
"title": "Region",
"type": "compute/region"
}, {
"title": "Service",
"type": "compute/service"
}, {
"title": "Snapshot",
"type": "compute/snapshot"
Expand Down
2 changes: 1 addition & 1 deletion lib/compute/autoscaler.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var util = require('../common/util.js');

/*! Developer Documentation
*
* @param {module:zone} zone - Zone object this autoscaler belongs to.
* @param {module:compute/zone} zone - Zone object this autoscaler belongs to.
* @param {string} name - Name of the autoscaler.
*/
/**
Expand Down
191 changes: 191 additions & 0 deletions lib/compute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ var Region = require('./region.js');
*/
var Service = require('../common/service.js');

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

/**
* @type {module:compute/snapshot}
* @private
Expand Down Expand Up @@ -305,6 +311,70 @@ Compute.prototype.createNetwork = function(name, config, callback) {
});
};

/**
* Create a backend service.
*
* @resource [Backend Services Overview]{@link https://cloud.google.com/compute/docs/load-balancing/http/backend-service}
* @resource [BackendServices: insert API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/backendServices/insert}
*
* @param {string} name - Name of the backend service.
* @param {object} config - See a
* [BackendService resource](https://cloud.google.com/compute/docs/reference/v1/backendServices#resource).
* @param {function=} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request.
* @param {module:compute/service} callback.service - The created Service
* 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 config = {
* backends: [
* {
* group: 'URL of an Instance Group resource'
* }
* ],
* healthChecks: [
* 'URL of an HTTP/HTTPS health check resource'
* ]
* };
*
* function callback(err, service, operation, apiResponse) {
* // `service` is a Service object.
*
* // `operation` is an Operation object that can be used to check the status
* // of network creation.
* }
*
* gce.createService('new-service', config, callback);
*/
Compute.prototype.createService = function(name, config, callback) {
var self = this;

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

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

var service = self.service(name);

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

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

/**
* Get a reference to a Google Compute Engine firewall.
*
Expand Down Expand Up @@ -1089,6 +1159,111 @@ Compute.prototype.getRegions = function(options, callback) {
});
};

/**
* Get a list of backend services.
*
* @resource [Backend Services Overview]{@link https://cloud.google.com/compute/docs/load-balancing/http/backend-service}
* @resource [BackendServices: list API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/backendServices/list}
*
* @param {object=} options - BackendService 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 snapshots 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/service[]} callback.services - Service objects from
* your project.
* @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.getServices(function(err, services) {
* // `services` is an array of `Service` objects.
* });
*
* //-
* // To control how many API requests are made and page through the results
* // manually, set `autoPaginate` to `false`.
* //-
* function callback(err, services, nextQuery, apiResponse) {
* if (nextQuery) {
* // More results exist.
* gce.getServices(nextQuery, callback);
* }
* }
*
* gce.getServices({
* autoPaginate: false
* }, callback);
*
* //-
* // Get the backend services from your project as a readable object stream.
* //-
* gce.getServices()
* .on('error', console.error)
* .on('data', function(service) {
* // `service` is a `Service` object.
* })
* .on('end', function() {
* // All services retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* //-
* gce.getServices()
* .on('data', function(service) {
* this.end();
* });
*/
Compute.prototype.getServices = function(options, callback) {
var self = this;

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

options = options || {};

this.request({
uri: '/global/backendServices',
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 services = (resp.items || []).map(function(service) {
var serviceInstance = self.service(service.name);
serviceInstance.metadata = service;
return serviceInstance;
});

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

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

/**
* Get a reference to a Google Compute Engine backend service.
*
* @resource [Backend Services Overview]{@link https://cloud.google.com/compute/docs/load-balancing/http/backend-service}
*
* @param {string} name - Name of the existing service.
* @return {module:compute/service}
*
* @example
* var service = gce.service('service-name');
*/
Compute.prototype.service = function(name) {
return new ServiceClass(this, name);
};

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

0 comments on commit 1a2b2da

Please sign in to comment.