Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashley committed Aug 4, 2017
1 parent 3e91324 commit 06a12ab
Show file tree
Hide file tree
Showing 5 changed files with 382 additions and 0 deletions.
102 changes: 102 additions & 0 deletions packages/compute/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ var Network = require('./network.js');
*/
var Operation = require('./operation.js');

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

/**
* @type {module:compute/region}
* @private
Expand Down Expand Up @@ -1734,6 +1740,84 @@ Compute.prototype.getOperations = function(options, callback) {
Compute.prototype.getOperationsStream =
common.paginator.streamify('getOperations');

/**
* Return the regions available to your project.
*
* @resource [Project Overview]{@link https://cloud.google.com/compute/docs/projects}
*
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request.
* @param {module:compute/project} callback.project - Project objects with
* details
* @param {object} callback.apiResponse - The full API response.
*
* @example
* gce.getProject(function(err, project) {
* // `project` is an object with metadata
* });
*
*
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* gce.getProject().then(function(data) {
* var project = data[0];
* });
*/
Compute.prototype.getProject = function(options, callback) {
var self = this;

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

this.request({
uri: '',
qs: options
}, function(err, resp) {

if (err) {
callback(err, null, null, resp);
return;
}

var project = new Project(self);
project.metadata = resp;

callback(null, project, null, resp);
});
};

/**
* Get a list of global {module:compute/project} objects as a readable object
* stream.
*
* @return {stream}
*
* @example
* gce.getProjectStream()
* .on('error', console.error)
* .on('data', function(operation) {
* // `operation` is a `Operation` object.
* })
* .on('end', function() {
* // All operations retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* //-
* gce.getProjectStream()
* .on('data', function(operation) {
* this.end();
* });
*/
Compute.prototype.getProjectStream =
common.paginator.streamify('getProject');

/**
* Return the regions available to your project.
*
Expand Down Expand Up @@ -2632,6 +2716,21 @@ Compute.prototype.operation = function(name) {
return new Operation(this, name);
};

/**
* Get a reference to your Google Compute Engine project.
*
* @resource [Projects Overview]{@link https://cloud.google.com/compute/docs/reference/v1/projects}
*
* @param {string} name - Name of the existing operation.
* @return {module:compute/project}
*
* @example
* var project = gce.project();
*/
Compute.prototype.project = function() {
return new Project(this);
};

/**
* Get a reference to a Google Compute Engine region.
*
Expand Down Expand Up @@ -2746,6 +2845,7 @@ common.paginator.extend(Compute, [
'getMachineTypes',
'getNetworks',
'getOperations',
'getProject',
'getRegions',
'getRules',
'getServices',
Expand All @@ -2771,6 +2871,7 @@ common.util.promisifyAll(Compute, {
'machineType',
'network',
'operation',
'project',
'region',
'rule',
'service',
Expand All @@ -2785,6 +2886,7 @@ Compute.Firewall = Firewall;
Compute.HealthCheck = HealthCheck;
Compute.Network = Network;
Compute.Operation = Operation;
Compute.Project = Project;
Compute.Region = Region;
Compute.Rule = Rule;
Compute.Service = Service;
Expand Down
71 changes: 71 additions & 0 deletions packages/compute/src/project.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*!
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*!
* @module compute/project
*/

'use strict';

var common = require('@google-cloud/common');
var util = require('util');

/*! Developer Documentation
*
* @param {module:compute} compute - Compute object this project belongs to.
*/
/**
* A Projects object allows you to interact with your Google Compute Engine
* project.
*
* @resource [Projects Overview]{@link https://cloud.google.com/compute/docs/projects}
* @resource [Projects Resource]{@link https://cloud.google.com/compute/docs/reference/v1/projects}
*
* @constructor
* @alias module:compute/project
*
* @example
* var gcloud = require('google-cloud')({
* keyFilename: '/path/to/keyfile.json',
* projectId: 'grape-spaceship-123'
* });
*
* var gce = gcloud.compute();
*
* var project = gce.project();
*
*/
function Project(compute) {
common.ServiceObject.call(this, {
parent: compute,
baseUrl: '/',
id: compute.projectId
});


this.name = compute.projectId;
}

util.inherits(Project, common.ServiceObject);

/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
common.util.promisifyAll(Project);

module.exports = Project;
24 changes: 24 additions & 0 deletions packages/compute/system-test/compute.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,30 @@ describe('Compute', function() {
});
});

describe('project', function() {
it('should get the project', function(done) {
compute.getProject(function(err, project) {
assert.ifError(err);
assert(project.metadata);
done();
});
});

it('should get a list of machine types in stream mode', function(done) {
var resultsMatched = 0;

compute.getProjectStream()
.on('error', done)
.on('data', function() {
resultsMatched++;
})
.on('end', function() {
assert(resultsMatched > 0);
done();
});
});
});

describe('regions', function() {
it('should get a list of regions', function(done) {
compute.getRegions(function(err, regions) {
Expand Down
101 changes: 101 additions & 0 deletions packages/compute/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var fakeUtil = extend({}, util, {
'machineType',
'network',
'operation',
'project',
'region',
'rule',
'service',
Expand Down Expand Up @@ -76,6 +77,7 @@ var fakePaginator = {
'getMachineTypes',
'getNetworks',
'getOperations',
'getProject',
'getRegions',
'getRules',
'getServices',
Expand Down Expand Up @@ -106,6 +108,10 @@ function FakeOperation() {
this.calledWith_ = slice.call(arguments);
}

function FakeProject() {
this.calledWith_ = slice.call(arguments);
}

function FakeRegion() {
this.calledWith_ = slice.call(arguments);
this.address = function() { return {}; };
Expand Down Expand Up @@ -155,6 +161,7 @@ describe('Compute', function() {
'./health-check.js': FakeHealthCheck,
'./network.js': FakeNetwork,
'./operation.js': FakeOperation,
'./project.js': FakeProject,
'./region.js': FakeRegion,
'./rule.js': FakeRule,
'./service.js': FakeServiceClass,
Expand Down Expand Up @@ -223,6 +230,7 @@ describe('Compute', function() {
assert.strictEqual(compute.getMachineTypesStream, 'getMachineTypes');
assert.strictEqual(compute.getNetworksStream, 'getNetworks');
assert.strictEqual(compute.getOperationsStream, 'getOperations');
assert.strictEqual(compute.getProjectStream, 'getProject');
assert.strictEqual(compute.getRegionsStream, 'getRegions');
assert.strictEqual(compute.getRulesStream, 'getRules');
assert.strictEqual(compute.getServicesStream, 'getServices');
Expand Down Expand Up @@ -1808,6 +1816,99 @@ describe('Compute', function() {
});
});

describe('getProject', function() {
it('should accept only a callback', function(done) {
compute.request = function(reqOpts) {
assert.deepEqual(reqOpts.qs, {});
done();
};

compute.getProject(assert.ifError);
});

it('should make the correct API request', function(done) {
var options = {};

compute.request = function(reqOpts) {
assert.strictEqual(reqOpts.uri, '');
assert.strictEqual(reqOpts.qs, options);
done();
};

compute.getProject(options, assert.ifError);
});

describe('error', function() {
var error = new Error('Error.');
var apiResponse = { a: 'b', c: 'd' };

beforeEach(function() {
compute.request = function(reqOpts, callback) {
callback(error, apiResponse);
};
});

it('should execute callback with error & API response', function(done) {
compute.getProject({}, function(err, project, nextQuery, resp) {
assert.strictEqual(err, error);
assert.strictEqual(project, null);
assert.strictEqual(nextQuery, null);
assert.strictEqual(resp, apiResponse);

done();
});
});
});

describe('success', function() {
var project = { name: PROJECT_ID };
var apiResponse = project;

beforeEach(function() {
compute.request = function(reqOpts, callback) {
callback(null, apiResponse);
};
});

it('should create Project object from the response', function(done) {
compute.project = function(name) {
assert.strictEqual(name, project.name);
setImmediate(done);
return project;
};

compute.getProject({}, (err) => {
assert.ifError(err);

compute.project(PROJECT_ID);
});
});

it('shouldn\'t build a nextQuery', function(done) {
var apiResponseWithNextPageToken = extend({}, apiResponse, {
nextPageToken: 'next-page-token'
});

var query = { a: 'b', c: 'd' };
var originalQuery = extend({}, query);

compute.request = function(reqOpts, callback) {
callback(null, apiResponseWithNextPageToken);
};

compute.getProject(query, function(err, project, nextQuery) {
assert.ifError(err);

assert.deepEqual(query, originalQuery);

assert.strictEqual(nextQuery, null);

done();
});
});
});
});

describe('getRegions', function() {
it('should work with only a callback', function(done) {
compute.request = function(reqOpts) {
Expand Down
Loading

0 comments on commit 06a12ab

Please sign in to comment.