Skip to content

Commit 90f54fe

Browse files
Ashley Schuettstephenplusplus
Ashley Schuett
authored andcommitted
added get project api call (#2506)
added get project call https://cloud.google.com/compute/docs/reference/v1/projects
1 parent cf1d73a commit 90f54fe

File tree

5 files changed

+251
-0
lines changed

5 files changed

+251
-0
lines changed

packages/google-cloud-compute/src/index.js

+22
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,20 @@ 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+
* @return {module:compute/project}
2647+
*
2648+
* @example
2649+
* var project = gce.project();
2650+
*/
2651+
Compute.prototype.project = function() {
2652+
return new Project(this);
2653+
};
2654+
26352655
/**
26362656
* Get a reference to a Google Compute Engine region.
26372657
*
@@ -2771,6 +2791,7 @@ common.util.promisifyAll(Compute, {
27712791
'machineType',
27722792
'network',
27732793
'operation',
2794+
'project',
27742795
'region',
27752796
'rule',
27762797
'service',
@@ -2785,6 +2806,7 @@ Compute.Firewall = Firewall;
27852806
Compute.HealthCheck = HealthCheck;
27862807
Compute.Network = Network;
27872808
Compute.Operation = Operation;
2809+
Compute.Project = Project;
27882810
Compute.Region = Region;
27892811
Compute.Rule = Rule;
27902812
Compute.Service = Service;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*!
2+
* Copyright 2017 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 Project 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 project = gce.project();
42+
*/
43+
function Project(compute) {
44+
this.id = compute.projectId;
45+
46+
var methods = {
47+
/**
48+
* Get a Project object.
49+
*
50+
* @example
51+
* project.get(function(err, project, apiResponse) {
52+
* // `project` is a Project object.
53+
* });
54+
*
55+
* //-
56+
* // If the callback is omitted, we'll return a Promise.
57+
* //-
58+
* project.get().then(function(data) {
59+
* var project = data[0];
60+
* var apiResponse = data[1];
61+
* });
62+
*/
63+
get: true,
64+
65+
/**
66+
* Get the project's metadata.
67+
*
68+
* @resource [Projects: get API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/projects/get}
69+
* @resource [Projects Resource]{@link https://cloud.google.com/compute/docs/reference/v1/projects}
70+
*
71+
* @param {function=} callback - The callback function.
72+
* @param {?error} callback.err - An error returned while making this
73+
* request.
74+
* @param {object} callback.metadata - The machine type's metadata.
75+
* @param {object} callback.apiResponse - The full API response.
76+
*
77+
* @example
78+
* project.getMetadata(function(err, metadata, apiResponse) {});
79+
*
80+
* //-
81+
* // If the callback is omitted, we'll return a Promise.
82+
* //-
83+
* project.getMetadata().then(function(data) {
84+
* var metadata = data[0];
85+
* var apiResponse = data[1];
86+
* });
87+
*/
88+
getMetadata: true
89+
};
90+
91+
common.ServiceObject.call(this, {
92+
parent: compute,
93+
baseUrl: '',
94+
id: '',
95+
methods: methods
96+
});
97+
}
98+
99+
util.inherits(Project, common.ServiceObject);
100+
101+
/*! Developer Documentation
102+
*
103+
* All async methods (except for streams) will return a Promise in the event
104+
* that a callback is omitted.
105+
*/
106+
common.util.promisifyAll(Project);
107+
108+
module.exports = Project;

packages/google-cloud-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+
var project;
744+
745+
beforeEach(function() {
746+
project = compute.project();
747+
});
748+
749+
it('should get the project', function(done) {
750+
project.get(function(err, project) {
751+
assert.ifError(err);
752+
assert(project.metadata);
753+
done();
754+
});
755+
});
756+
757+
it('should get metadata about the project', function(done) {
758+
project.getMetadata(function(err, metadata) {
759+
assert.ifError(err);
760+
assert(metadata);
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/google-cloud-compute/test/index.js

+14
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,
@@ -2545,6 +2551,14 @@ describe('Compute', function() {
25452551
});
25462552
});
25472553

2554+
describe('project', function() {
2555+
it('should return a Project object', function() {
2556+
var project = compute.project();
2557+
assert(project instanceof FakeProject);
2558+
assert.strictEqual(project.calledWith_[0], compute);
2559+
});
2560+
});
2561+
25482562
describe('region', function() {
25492563
var NAME = 'region-name';
25502564

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

0 commit comments

Comments
 (0)