Skip to content
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

Implement Service & Service Object for Resource #943

Merged
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
91 changes: 33 additions & 58 deletions lib/resource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

var extend = require('extend');
var is = require('is');
var nodeutil = require('util');

/**
* @type {module:resource/project}
Expand All @@ -30,31 +31,22 @@ var is = require('is');
var Project = require('./project.js');

/**
* @type {module:common/streamrouter}
* @type {module:common/service}
* @private
*/
var streamRouter = require('../common/stream-router.js');
var Service = require('../common/service.js');

/**
* @type {module:common/util}
* @private
*/
var util = require('../common/util.js');

/**
* @const {string}
* @type {module:common/streamrouter}
* @private
*/
var BASE_URL = 'https://cloudresourcemanager.googleapis.com/v1beta1/projects';
var streamRouter = require('../common/stream-router.js');

/**
* Required scopes for Google Cloud Resource Manager API.
* @const {array}
* @type {module:common/util}
* @private
*/
var SCOPES = [
'https://www.googleapis.com/auth/cloud-platform'
];
var util = require('../common/util.js');

/**
* [The Cloud Resource Manager](https://cloud.google.com/resource-manager/)
Expand Down Expand Up @@ -88,24 +80,28 @@ function Resource(options) {
return new Resource(options);
}

this.defaultProjectId_ = options.projectId;
var config = {
baseUrl: 'https://cloudresourcemanager.googleapis.com/v1beta1',
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
projectIdRequired: false
};

this.makeAuthenticatedRequest_ = util.makeAuthenticatedRequestFactory({
credentials: options.credentials,
keyFile: options.keyFilename,
scopes: SCOPES,
email: options.email
});
Service.call(this, config, options);

this.defaultProjectId_ = options.projectId;
}

nodeutil.inherits(Resource, Service);

/**
* Create a project.
*
* **This method only works if you are authenticated as yourself, e.g. using the
* gcloud SDK.**

This comment was marked as spam.

*
* @resource [Projects Overview]{@link https://cloud.google.com/compute/docs/networking#networks}
* @resource [projects: create API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/create}
*
* @private
*
* @param {string} name - Name of the project.
* @param {object=} options - See a
* [Project resource](https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects#Project).
Expand All @@ -130,11 +126,13 @@ Resource.prototype.createProject = function(id, options, callback) {
options = {};
}

var body = extend({}, options, {
projectId: id
});

this.makeReq_('POST', '/', null, body, function(err, resp) {
this.request({
method: 'POST',
uri: '/projects',
json: extend({}, options, {
projectId: id
})
}, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
Expand Down Expand Up @@ -219,7 +217,10 @@ Resource.prototype.getProjects = function(options, callback) {

options = options || {};

this.makeReq_('GET', '/', options, null, function(err, resp) {
this.request({
uri: '/projects',
qs: options
}, function(err, resp) {
if (err) {
callback(err, null, null, resp);
return;
Expand All @@ -244,8 +245,8 @@ Resource.prototype.getProjects = function(options, callback) {
};

/**
* Create a Project object to reference an existing project. See
* {module:resoucemanager/createProject} to create a project.
* Create a Project object. See {module:resoucemanager/createProject} to create
* a project.
*
* @throws {Error} If an ID is not provided.
*
Expand All @@ -265,32 +266,6 @@ Resource.prototype.project = function(id) {
return new Project(this, id);
};

/**
* Make a new request object from the provided arguments and wrap the callback
* to intercept non-successful responses.
*
* @private
*
* @param {string} method - Action.
* @param {string} path - Request path.
* @param {*} query - Request query object.
* @param {*} body - Request body contents.
* @param {function} callback - The callback function.
*/
Resource.prototype.makeReq_ = function(method, path, query, body, callback) {
var reqOpts = {
method: method,
qs: query,
uri: BASE_URL + path
};

if (body) {
reqOpts.json = body;
}

this.makeAuthenticatedRequest_(reqOpts, callback);
};

/*! Developer Documentation
*
* These methods can be used with either a callback or as a readable object
Expand Down
Loading