Skip to content

added get project api call #2506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 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 @@ -2632,6 +2638,20 @@ 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}
*
* @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 @@ -2771,6 +2791,7 @@ common.util.promisifyAll(Compute, {
'machineType',
'network',
'operation',
'project',
'region',
'rule',
'service',
Expand All @@ -2785,6 +2806,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
108 changes: 108 additions & 0 deletions packages/compute/src/project.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*!
* Copyright 2017 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 Project 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 project = gce.project();
*/
function Project(compute) {
this.id = compute.projectId;

var methods = {
/**
* Get a Project object.
*
* @example
* project.get(function(err, project, apiResponse) {
* // `project` is a Project object.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* project.get().then(function(data) {
* var project = data[0];
* var apiResponse = data[1];
* });
*/
get: true,

/**
* Get the project's metadata.
*
* @resource [Projects: get API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/projects/get}
* @resource [Projects Resource]{@link https://cloud.google.com/compute/docs/reference/v1/projects}
*
* @param {function=} callback - The callback function.
* @param {?error} callback.err - An error returned while making this
* request.
* @param {object} callback.metadata - The machine type's metadata.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* project.getMetadata(function(err, metadata, apiResponse) {});
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* project.getMetadata().then(function(data) {
* var metadata = data[0];
* var apiResponse = data[1];
* });
*/
getMetadata: true
};

common.ServiceObject.call(this, {
parent: compute,
baseUrl: '',
id: '',
methods: methods
});
}

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() {
var project;

beforeEach(function() {
project = compute.project();
});

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

it('should get metadata about the project', function(done) {
project.getMetadata(function(err, metadata) {
assert.ifError(err);
assert(metadata);
done();
});
});
});

describe('regions', function() {
it('should get a list of regions', function(done) {
compute.getRegions(function(err, regions) {
Expand Down
14 changes: 14 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 @@ -106,6 +107,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 +160,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 @@ -2545,6 +2551,14 @@ describe('Compute', function() {
});
});

describe('project', function() {
it('should return a Project object', function() {
var project = compute.project();
assert(project instanceof FakeProject);
assert.strictEqual(project.calledWith_[0], compute);
});
});

describe('region', function() {
var NAME = 'region-name';

Expand Down
83 changes: 83 additions & 0 deletions packages/compute/test/project.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright 2017 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.
*/

'use strict';

var assert = require('assert');
var extend = require('extend');
var proxyquire = require('proxyquire');
var util = require('@google-cloud/common').util;

var promisified = false;
var fakeUtil = extend({}, util, {
promisifyAll: function(Class) {
if (Class.name === 'Project') {
promisified = true;
}
}
});

function FakeServiceObject() {
this.calledWith_ = arguments;
}

describe('Project', function() {
var Project;
var project;

var PROJECT_ID = 'project-1';
var COMPUTE = {
projectId: PROJECT_ID,
authConfig: { a: 'b', c: 'd' }
};

before(function() {
Project = proxyquire('../src/project.js', {
'@google-cloud/common': {
ServiceObject: FakeServiceObject,
util: fakeUtil
}
});
});

beforeEach(function() {
project = new Project(COMPUTE);
});

describe('instantiation', function() {
it('should promisify all the things', function() {
assert(promisified);
});

it('should localize the ID', function() {
assert.strictEqual(project.id, PROJECT_ID);
});

it('should inherit from ServiceObject', function() {
assert(project instanceof FakeServiceObject);

var calledWith = project.calledWith_[0];

assert.strictEqual(calledWith.parent, COMPUTE);
assert.strictEqual(calledWith.baseUrl, '');
assert.strictEqual(calledWith.id, '');
assert.deepStrictEqual(calledWith.methods, {
get: true,
getMetadata: true
});
});
});
});