Skip to content

Commit 06a12ab

Browse files
author
Ashley
committed
1 parent 3e91324 commit 06a12ab

File tree

5 files changed

+382
-0
lines changed

5 files changed

+382
-0
lines changed

packages/compute/src/index.js

+102
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ var Network = require('./network.js');
5050
*/
5151
var Operation = require('./operation.js');
5252

53+
/**
54+
* @type {module: compute/project}
55+
* @private
56+
*/
57+
var Project = require('./project.js');
58+
5359
/**
5460
* @type {module:compute/region}
5561
* @private
@@ -1734,6 +1740,84 @@ Compute.prototype.getOperations = function(options, callback) {
17341740
Compute.prototype.getOperationsStream =
17351741
common.paginator.streamify('getOperations');
17361742

1743+
/**
1744+
* Return the regions available to your project.
1745+
*
1746+
* @resource [Project Overview]{@link https://cloud.google.com/compute/docs/projects}
1747+
*
1748+
* @param {function} callback - The callback function.
1749+
* @param {?error} callback.err - An error returned while making this request.
1750+
* @param {module:compute/project} callback.project - Project objects with
1751+
* details
1752+
* @param {object} callback.apiResponse - The full API response.
1753+
*
1754+
* @example
1755+
* gce.getProject(function(err, project) {
1756+
* // `project` is an object with metadata
1757+
* });
1758+
*
1759+
*
1760+
*
1761+
* //-
1762+
* // If the callback is omitted, we'll return a Promise.
1763+
* //-
1764+
* gce.getProject().then(function(data) {
1765+
* var project = data[0];
1766+
* });
1767+
*/
1768+
Compute.prototype.getProject = function(options, callback) {
1769+
var self = this;
1770+
1771+
if (is.fn(options)) {
1772+
callback = options;
1773+
options = {};
1774+
}
1775+
1776+
this.request({
1777+
uri: '',
1778+
qs: options
1779+
}, function(err, resp) {
1780+
1781+
if (err) {
1782+
callback(err, null, null, resp);
1783+
return;
1784+
}
1785+
1786+
var project = new Project(self);
1787+
project.metadata = resp;
1788+
1789+
callback(null, project, null, resp);
1790+
});
1791+
};
1792+
1793+
/**
1794+
* Get a list of global {module:compute/project} objects as a readable object
1795+
* stream.
1796+
*
1797+
* @return {stream}
1798+
*
1799+
* @example
1800+
* gce.getProjectStream()
1801+
* .on('error', console.error)
1802+
* .on('data', function(operation) {
1803+
* // `operation` is a `Operation` object.
1804+
* })
1805+
* .on('end', function() {
1806+
* // All operations retrieved.
1807+
* });
1808+
*
1809+
* //-
1810+
* // If you anticipate many results, you can end a stream early to prevent
1811+
* // unnecessary processing and API requests.
1812+
* //-
1813+
* gce.getProjectStream()
1814+
* .on('data', function(operation) {
1815+
* this.end();
1816+
* });
1817+
*/
1818+
Compute.prototype.getProjectStream =
1819+
common.paginator.streamify('getProject');
1820+
17371821
/**
17381822
* Return the regions available to your project.
17391823
*
@@ -2632,6 +2716,21 @@ Compute.prototype.operation = function(name) {
26322716
return new Operation(this, name);
26332717
};
26342718

2719+
/**
2720+
* Get a reference to your Google Compute Engine project.
2721+
*
2722+
* @resource [Projects Overview]{@link https://cloud.google.com/compute/docs/reference/v1/projects}
2723+
*
2724+
* @param {string} name - Name of the existing operation.
2725+
* @return {module:compute/project}
2726+
*
2727+
* @example
2728+
* var project = gce.project();
2729+
*/
2730+
Compute.prototype.project = function() {
2731+
return new Project(this);
2732+
};
2733+
26352734
/**
26362735
* Get a reference to a Google Compute Engine region.
26372736
*
@@ -2746,6 +2845,7 @@ common.paginator.extend(Compute, [
27462845
'getMachineTypes',
27472846
'getNetworks',
27482847
'getOperations',
2848+
'getProject',
27492849
'getRegions',
27502850
'getRules',
27512851
'getServices',
@@ -2771,6 +2871,7 @@ common.util.promisifyAll(Compute, {
27712871
'machineType',
27722872
'network',
27732873
'operation',
2874+
'project',
27742875
'region',
27752876
'rule',
27762877
'service',
@@ -2785,6 +2886,7 @@ Compute.Firewall = Firewall;
27852886
Compute.HealthCheck = HealthCheck;
27862887
Compute.Network = Network;
27872888
Compute.Operation = Operation;
2889+
Compute.Project = Project;
27882890
Compute.Region = Region;
27892891
Compute.Rule = Rule;
27902892
Compute.Service = Service;

packages/compute/src/project.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*!
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*!
18+
* @module compute/project
19+
*/
20+
21+
'use strict';
22+
23+
var common = require('@google-cloud/common');
24+
var util = require('util');
25+
26+
/*! Developer Documentation
27+
*
28+
* @param {module:compute} compute - Compute object this project belongs to.
29+
*/
30+
/**
31+
* A Projects object allows you to interact with your Google Compute Engine
32+
* project.
33+
*
34+
* @resource [Projects Overview]{@link https://cloud.google.com/compute/docs/projects}
35+
* @resource [Projects Resource]{@link https://cloud.google.com/compute/docs/reference/v1/projects}
36+
*
37+
* @constructor
38+
* @alias module:compute/project
39+
*
40+
* @example
41+
* var gcloud = require('google-cloud')({
42+
* keyFilename: '/path/to/keyfile.json',
43+
* projectId: 'grape-spaceship-123'
44+
* });
45+
*
46+
* var gce = gcloud.compute();
47+
*
48+
* var project = gce.project();
49+
*
50+
*/
51+
function Project(compute) {
52+
common.ServiceObject.call(this, {
53+
parent: compute,
54+
baseUrl: '/',
55+
id: compute.projectId
56+
});
57+
58+
59+
this.name = compute.projectId;
60+
}
61+
62+
util.inherits(Project, common.ServiceObject);
63+
64+
/*! Developer Documentation
65+
*
66+
* All async methods (except for streams) will return a Promise in the event
67+
* that a callback is omitted.
68+
*/
69+
common.util.promisifyAll(Project);
70+
71+
module.exports = Project;

packages/compute/system-test/compute.js

+24
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,30 @@ describe('Compute', function() {
739739
});
740740
});
741741

742+
describe('project', function() {
743+
it('should get the project', function(done) {
744+
compute.getProject(function(err, project) {
745+
assert.ifError(err);
746+
assert(project.metadata);
747+
done();
748+
});
749+
});
750+
751+
it('should get a list of machine types in stream mode', function(done) {
752+
var resultsMatched = 0;
753+
754+
compute.getProjectStream()
755+
.on('error', done)
756+
.on('data', function() {
757+
resultsMatched++;
758+
})
759+
.on('end', function() {
760+
assert(resultsMatched > 0);
761+
done();
762+
});
763+
});
764+
});
765+
742766
describe('regions', function() {
743767
it('should get a list of regions', function(done) {
744768
compute.getRegions(function(err, regions) {

packages/compute/test/index.js

+101
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var fakeUtil = extend({}, util, {
4646
'machineType',
4747
'network',
4848
'operation',
49+
'project',
4950
'region',
5051
'rule',
5152
'service',
@@ -76,6 +77,7 @@ var fakePaginator = {
7677
'getMachineTypes',
7778
'getNetworks',
7879
'getOperations',
80+
'getProject',
7981
'getRegions',
8082
'getRules',
8183
'getServices',
@@ -106,6 +108,10 @@ function FakeOperation() {
106108
this.calledWith_ = slice.call(arguments);
107109
}
108110

111+
function FakeProject() {
112+
this.calledWith_ = slice.call(arguments);
113+
}
114+
109115
function FakeRegion() {
110116
this.calledWith_ = slice.call(arguments);
111117
this.address = function() { return {}; };
@@ -155,6 +161,7 @@ describe('Compute', function() {
155161
'./health-check.js': FakeHealthCheck,
156162
'./network.js': FakeNetwork,
157163
'./operation.js': FakeOperation,
164+
'./project.js': FakeProject,
158165
'./region.js': FakeRegion,
159166
'./rule.js': FakeRule,
160167
'./service.js': FakeServiceClass,
@@ -223,6 +230,7 @@ describe('Compute', function() {
223230
assert.strictEqual(compute.getMachineTypesStream, 'getMachineTypes');
224231
assert.strictEqual(compute.getNetworksStream, 'getNetworks');
225232
assert.strictEqual(compute.getOperationsStream, 'getOperations');
233+
assert.strictEqual(compute.getProjectStream, 'getProject');
226234
assert.strictEqual(compute.getRegionsStream, 'getRegions');
227235
assert.strictEqual(compute.getRulesStream, 'getRules');
228236
assert.strictEqual(compute.getServicesStream, 'getServices');
@@ -1808,6 +1816,99 @@ describe('Compute', function() {
18081816
});
18091817
});
18101818

1819+
describe('getProject', function() {
1820+
it('should accept only a callback', function(done) {
1821+
compute.request = function(reqOpts) {
1822+
assert.deepEqual(reqOpts.qs, {});
1823+
done();
1824+
};
1825+
1826+
compute.getProject(assert.ifError);
1827+
});
1828+
1829+
it('should make the correct API request', function(done) {
1830+
var options = {};
1831+
1832+
compute.request = function(reqOpts) {
1833+
assert.strictEqual(reqOpts.uri, '');
1834+
assert.strictEqual(reqOpts.qs, options);
1835+
done();
1836+
};
1837+
1838+
compute.getProject(options, assert.ifError);
1839+
});
1840+
1841+
describe('error', function() {
1842+
var error = new Error('Error.');
1843+
var apiResponse = { a: 'b', c: 'd' };
1844+
1845+
beforeEach(function() {
1846+
compute.request = function(reqOpts, callback) {
1847+
callback(error, apiResponse);
1848+
};
1849+
});
1850+
1851+
it('should execute callback with error & API response', function(done) {
1852+
compute.getProject({}, function(err, project, nextQuery, resp) {
1853+
assert.strictEqual(err, error);
1854+
assert.strictEqual(project, null);
1855+
assert.strictEqual(nextQuery, null);
1856+
assert.strictEqual(resp, apiResponse);
1857+
1858+
done();
1859+
});
1860+
});
1861+
});
1862+
1863+
describe('success', function() {
1864+
var project = { name: PROJECT_ID };
1865+
var apiResponse = project;
1866+
1867+
beforeEach(function() {
1868+
compute.request = function(reqOpts, callback) {
1869+
callback(null, apiResponse);
1870+
};
1871+
});
1872+
1873+
it('should create Project object from the response', function(done) {
1874+
compute.project = function(name) {
1875+
assert.strictEqual(name, project.name);
1876+
setImmediate(done);
1877+
return project;
1878+
};
1879+
1880+
compute.getProject({}, (err) => {
1881+
assert.ifError(err);
1882+
1883+
compute.project(PROJECT_ID);
1884+
});
1885+
});
1886+
1887+
it('shouldn\'t build a nextQuery', function(done) {
1888+
var apiResponseWithNextPageToken = extend({}, apiResponse, {
1889+
nextPageToken: 'next-page-token'
1890+
});
1891+
1892+
var query = { a: 'b', c: 'd' };
1893+
var originalQuery = extend({}, query);
1894+
1895+
compute.request = function(reqOpts, callback) {
1896+
callback(null, apiResponseWithNextPageToken);
1897+
};
1898+
1899+
compute.getProject(query, function(err, project, nextQuery) {
1900+
assert.ifError(err);
1901+
1902+
assert.deepEqual(query, originalQuery);
1903+
1904+
assert.strictEqual(nextQuery, null);
1905+
1906+
done();
1907+
});
1908+
});
1909+
});
1910+
});
1911+
18111912
describe('getRegions', function() {
18121913
it('should work with only a callback', function(done) {
18131914
compute.request = function(reqOpts) {

0 commit comments

Comments
 (0)