Skip to content

Commit f01d8b7

Browse files
author
Ashley
committed
1 parent 3e91324 commit f01d8b7

File tree

4 files changed

+226
-0
lines changed

4 files changed

+226
-0
lines changed

packages/compute/src/index.js

+23
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
@@ -2632,6 +2638,21 @@ Compute.prototype.operation = function(name) {
26322638
return new Operation(this, name);
26332639
};
26342640

2641+
/**
2642+
* Get a reference to your Google Compute Engine project.
2643+
*
2644+
* @resource [Projects Overview]{@link https://cloud.google.com/compute/docs/reference/v1/projects}
2645+
*
2646+
* @param {string} name - Name of the existing operation.
2647+
* @return {module:compute/project}
2648+
*
2649+
* @example
2650+
* var project = gce.project();
2651+
*/
2652+
Compute.prototype.project = function(name) {
2653+
return new Project(this);
2654+
};
2655+
26352656
/**
26362657
* Get a reference to a Google Compute Engine region.
26372658
*
@@ -2771,6 +2792,7 @@ common.util.promisifyAll(Compute, {
27712792
'machineType',
27722793
'network',
27732794
'operation',
2795+
'project',
27742796
'region',
27752797
'rule',
27762798
'service',
@@ -2785,6 +2807,7 @@ Compute.Firewall = Firewall;
27852807
Compute.HealthCheck = HealthCheck;
27862808
Compute.Network = Network;
27872809
Compute.Operation = Operation;
2810+
Compute.Project = Project;
27882811
Compute.Region = Region;
27892812
Compute.Rule = Rule;
27902813
Compute.Service = Service;

packages/compute/src/project.js

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
var methods = {
53+
/**
54+
* Get project
55+
*
56+
* @example
57+
* project.get(function(err, project, apiResponse) {
58+
* // `project` is a Project object.
59+
* });
60+
*/
61+
get: true,
62+
63+
/**
64+
* Get the project's metadata.
65+
*
66+
* @resource [Project Resource]{@link https://cloud.google.com/compute/docs/reference/v1/projects}
67+
* @resource [Projects: get API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/projects/get}
68+
*
69+
* @param {function=} callback - The callback function.
70+
* @param {?error} callback.err - An error returned while making this
71+
* request.
72+
* @param {object} callback.metadata - The project's metadata.
73+
* @param {object} callback.apiResponse - The full API response.
74+
*
75+
* @example
76+
* project.getMetadata(function(err, metadata, apiResponse) {});
77+
*
78+
* //-
79+
* // If the callback is omitted, we'll return a Promise.
80+
* //-
81+
* project.getMetadata().then(function(data) {
82+
* var metadata = data[0];
83+
* var apiResponse = data[1];
84+
* });
85+
*/
86+
getMetadata: true
87+
};
88+
89+
common.ServiceObject.call(this, {
90+
parent: compute,
91+
baseUrl: '/',
92+
id: compute.projectId,
93+
methods: methods
94+
});
95+
96+
97+
this.name = compute.projectId;
98+
}
99+
100+
util.inherits(Project, common.ServiceObject);
101+
102+
/*! Developer Documentation
103+
*
104+
* All async methods (except for streams) will return a Promise in the event
105+
* that a callback is omitted.
106+
*/
107+
common.util.promisifyAll(Project);
108+
109+
module.exports = Project;

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

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
assert.deepEqual(calledWith.methods, {
83+
get: true,
84+
getMetadata: true
85+
});
86+
});
87+
});
88+
});

0 commit comments

Comments
 (0)