Skip to content

Commit 7ddf557

Browse files
author
Ashley
committed
1 parent 3e91324 commit 7ddf557

File tree

5 files changed

+286
-0
lines changed

5 files changed

+286
-0
lines changed

packages/compute/src/index.js

+101
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
*
@@ -2771,6 +2870,7 @@ common.util.promisifyAll(Compute, {
27712870
'machineType',
27722871
'network',
27732872
'operation',
2873+
'project',
27742874
'region',
27752875
'rule',
27762876
'service',
@@ -2785,6 +2885,7 @@ Compute.Firewall = Firewall;
27852885
Compute.HealthCheck = HealthCheck;
27862886
Compute.Network = Network;
27872887
Compute.Operation = Operation;
2888+
Compute.Project = Project;
27882889
Compute.Region = Region;
27892890
Compute.Rule = Rule;
27902891
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

+6
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',
@@ -106,6 +107,10 @@ function FakeOperation() {
106107
this.calledWith_ = slice.call(arguments);
107108
}
108109

110+
function FakeProject() {
111+
this.calledWith_ = slice.call(arguments);
112+
}
113+
109114
function FakeRegion() {
110115
this.calledWith_ = slice.call(arguments);
111116
this.address = function() { return {}; };
@@ -155,6 +160,7 @@ describe('Compute', function() {
155160
'./health-check.js': FakeHealthCheck,
156161
'./network.js': FakeNetwork,
157162
'./operation.js': FakeOperation,
163+
'./project.js': FakeProject,
158164
'./region.js': FakeRegion,
159165
'./rule.js': FakeRule,
160166
'./service.js': FakeServiceClass,

packages/compute/test/project.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
'use strict';
18+
19+
var assert = require('assert');
20+
var extend = require('extend');
21+
var nodeutil = require('util');
22+
var proxyquire = require('proxyquire');
23+
var ServiceObject = require('@google-cloud/common').ServiceObject;
24+
var util = require('@google-cloud/common').util;
25+
26+
var promisified = false;
27+
var fakeUtil = extend({}, util, {
28+
promisifyAll: function(Class) {
29+
if (Class.name === 'Project') {
30+
promisified = true;
31+
}
32+
}
33+
});
34+
35+
function FakeServiceObject() {
36+
this.calledWith_ = arguments;
37+
ServiceObject.apply(this, arguments);
38+
}
39+
40+
nodeutil.inherits(FakeServiceObject, ServiceObject);
41+
42+
describe('Project', function() {
43+
var Project;
44+
var project;
45+
46+
var PROJECT_ID = 'project-1';
47+
var COMPUTE = {
48+
projectId: PROJECT_ID,
49+
authConfig: { a: 'b', c: 'd' }
50+
};
51+
52+
before(function() {
53+
Project = proxyquire('../src/project.js', {
54+
'@google-cloud/common': {
55+
ServiceObject: FakeServiceObject,
56+
util: fakeUtil
57+
}
58+
});
59+
});
60+
61+
beforeEach(function() {
62+
project = new Project(COMPUTE);
63+
});
64+
65+
describe('instantiation', function() {
66+
it('should promisify all the things', function() {
67+
assert(promisified);
68+
});
69+
70+
it('should localize the name', function() {
71+
assert.strictEqual(project.name, PROJECT_ID);
72+
});
73+
74+
it('should inherit from ServiceObject', function() {
75+
assert(project instanceof ServiceObject);
76+
77+
var calledWith = project.calledWith_[0];
78+
79+
assert.strictEqual(calledWith.parent, COMPUTE);
80+
assert.strictEqual(calledWith.baseUrl, '/');
81+
assert.strictEqual(calledWith.id, PROJECT_ID);
82+
});
83+
});
84+
});

0 commit comments

Comments
 (0)