From 3458322020d0317c87e95e1656eaa0b5bff9db33 Mon Sep 17 00:00:00 2001 From: Luke Sneeringer Date: Mon, 6 Nov 2017 15:32:27 -0800 Subject: [PATCH] Excise resource manager. (#2730) It now lives at https://github.com/googleapis/nodejs-resource --- README.md | 2 +- packages/resource/README.md | 92 ------ packages/resource/package.json | 75 ----- packages/resource/src/index.js | 335 ------------------- packages/resource/src/project.js | 281 ---------------- packages/resource/system-test/resource.js | 231 ------------- packages/resource/test/index.js | 381 ---------------------- packages/resource/test/project.js | 137 -------- 8 files changed, 1 insertion(+), 1533 deletions(-) delete mode 100644 packages/resource/README.md delete mode 100644 packages/resource/package.json delete mode 100644 packages/resource/src/index.js delete mode 100644 packages/resource/src/project.js delete mode 100644 packages/resource/system-test/resource.js delete mode 100644 packages/resource/test/index.js delete mode 100644 packages/resource/test/project.js diff --git a/README.md b/README.md index 9034ad6ed78..3020c5b4264 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ This client supports the following Google Cloud Platform services at an [Alpha]( * [Cloud Bigtable](https://github.com/googleapis/nodejs-bigtable) (Alpha) * [Cloud DNS](https://github.com/googleapis/nodejs-dns) (Alpha) -* [Cloud Resource Manager](#cloud-resource-manager-alpha) (Alpha) +* [Cloud Resource Manager](https://github.com/googleapis/nodejs-resource) (Alpha) * [Google Compute Engine](https://github.com/googleapis/nodejs-compute) (Alpha) * [Google Prediction API](#google-prediction-api-alpha) (Alpha) * [Google Stackdriver Debugger](#google-stackdriver-debugger-alpha) (Alpha) diff --git a/packages/resource/README.md b/packages/resource/README.md deleted file mode 100644 index 55cc0a285b1..00000000000 --- a/packages/resource/README.md +++ /dev/null @@ -1,92 +0,0 @@ -# @google-cloud/resource ([Alpha][versioning]) -> Cloud Resource Manager Client Library for Node.js - -*Looking for more Google APIs than just Resource Manager? You might want to check out [`google-cloud`][google-cloud].* - -- [API Documentation][gcloud-resource-docs] -- [Official Documentation][cloud-resource-docs] - - -```sh -$ npm install --save @google-cloud/resource -``` -```js -var resource = require('@google-cloud/resource')({ - projectId: 'grape-spaceship-123', - keyFilename: '/path/to/keyfile.json' -}); - -// Get all of the projects you maintain. -resource.getProjects(function(err, projects) { - if (!err) { - // `projects` contains all of your projects. - } -}); - -// Get the metadata from your project. (defaults to `grape-spaceship-123`) -var project = resource.project(); - -project.getMetadata(function(err, metadata) { - // `metadata` describes your project. -}); - -// Promises are also supported by omitting callbacks. -project.getMetadata().then(function(data) { - var metadata = data[0]; -}); - -// It's also possible to integrate with third-party Promise libraries. -var resource = require('@google-cloud/resource')({ - promise: require('bluebird') -}); -``` - - -## Authentication - -It's incredibly easy to get authenticated and start using Google's APIs. You can set your credentials on a global basis as well as on a per-API basis. See each individual API section below to see how you can auth on a per-API-basis. This is useful if you want to use different accounts for different Cloud services. - -### On Google Cloud Platform - -If you are running this client on Google Cloud Platform, we handle authentication for you with no configuration. You just need to make sure that when you [set up the GCE instance][gce-how-to], you add the correct scopes for the APIs you want to access. - -``` js -var resource = require('@google-cloud/resource')(); -// ...you're good to go! -``` - -### Elsewhere - -If you are not running this client on Google Cloud Platform, you need a Google Developers service account. To create a service account: - -1. Visit the [Google Developers Console][dev-console]. -2. Create a new project or click on an existing project. -3. Navigate to **APIs & auth** > **APIs section** and turn on the following APIs (you may need to enable billing in order to use these services): - * Google Cloud Resource Manager API -4. Navigate to **APIs & auth** > **Credentials** and then: - * If you want to use a new service account key, click on **Create credentials** and select **Service account key**. After the account key is created, you will be prompted to download the JSON key file that the library uses to authenticate your requests. - * If you want to generate a new service account key for an existing service account, click on **Generate new JSON key** and download the JSON key file. - -``` js -var projectId = process.env.GCLOUD_PROJECT; // E.g. 'grape-spaceship-123' - -var resource = require('@google-cloud/resource')({ - projectId: projectId, - - // The path to your key file: - keyFilename: '/path/to/keyfile.json' - - // Or the contents of the key file: - credentials: require('./path/to/keyfile.json') -}); - -// ...you're good to go! -``` - - -[versioning]: https://github.com/GoogleCloudPlatform/google-cloud-node#versioning -[google-cloud]: https://github.com/GoogleCloudPlatform/google-cloud-node/ -[gce-how-to]: https://cloud.google.com/compute/docs/authentication#using -[dev-console]: https://console.developers.google.com/project -[gcloud-resource-docs]: https://googlecloudplatform.github.io/google-cloud-node/#/docs/resource -[cloud-resource-docs]: https://cloud.google.com/resource-manager diff --git a/packages/resource/package.json b/packages/resource/package.json deleted file mode 100644 index 861b3051ce6..00000000000 --- a/packages/resource/package.json +++ /dev/null @@ -1,75 +0,0 @@ -{ - "name": "@google-cloud/resource", - "version": "0.7.3", - "author": "Google Inc.", - "description": "Cloud Resource Manager Client Library for Node.js", - "contributors": [ - { - "name": "Burcu Dogan", - "email": "jbd@google.com" - }, - { - "name": "Johan Euphrosine", - "email": "proppy@google.com" - }, - { - "name": "Patrick Costello", - "email": "pcostell@google.com" - }, - { - "name": "Ryan Seys", - "email": "ryan@ryanseys.com" - }, - { - "name": "Silvano Luciani", - "email": "silvano@google.com" - }, - { - "name": "Stephen Sawchuk", - "email": "sawchuk@gmail.com" - } - ], - "main": "./src/index.js", - "files": [ - "src", - "AUTHORS", - "CONTRIBUTORS", - "LICENSE" - ], - "repository": "googlecloudplatform/google-cloud-node", - "keywords": [ - "google apis client", - "google api client", - "google apis", - "google api", - "google", - "google cloud platform", - "google cloud", - "cloud", - "google resource", - "resource" - ], - "dependencies": { - "@google-cloud/common": "^0.13.0", - "extend": "^3.0.0", - "is": "^3.0.1" - }, - "devDependencies": { - "arrify": "^1.0.0", - "async": "^2.1.2", - "google-auto-auth": "^0.7.1", - "methmeth": "^1.1.0", - "mocha": "^3.0.1", - "proxyquire": "^1.7.10", - "uuid": "^3.0.1" - }, - "scripts": { - "publish-module": "node ../../scripts/publish.js resource", - "test": "mocha test/*.js", - "system-test": "mocha system-test/*.js --no-timeouts --bail" - }, - "license": "Apache-2.0", - "engines": { - "node": ">=4.0.0" - } -} diff --git a/packages/resource/src/index.js b/packages/resource/src/index.js deleted file mode 100644 index de2ddba706f..00000000000 --- a/packages/resource/src/index.js +++ /dev/null @@ -1,335 +0,0 @@ -/*! - * Copyright 2015 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 resource - */ - -'use strict'; - -var common = require('@google-cloud/common'); -var extend = require('extend'); -var is = require('is'); -var util = require('util'); - -/** - * @type {module:resource/project} - * @private - */ -var Project = require('./project.js'); - -/** * - * The [Cloud Resource Manager](https://cloud.google.com/resource-manager/) - * provides methods that you can use to programmatically manage your projects - * in the Google Cloud Platform. With this API, you can do the following: - * - * - Get a list of all projects associated with an account. - * - Create new projects. - * - Update existing projects. - * - Delete projects. - * - Recover projects. - * - * @alias module:resource - * @constructor - * - * @resource [What is the Cloud Resource Manager?]{@link https://cloud.google.com/resource-manager} - * - * @param {object} options - [Configuration object](#/docs). - */ -function Resource(options) { - if (!(this instanceof Resource)) { - options = common.util.normalizeArguments(this, options, { - projectIdRequired: false - }); - return new Resource(options); - } - - var config = { - baseUrl: 'https://cloudresourcemanager.googleapis.com/v1', - scopes: ['https://www.googleapis.com/auth/cloud-platform'], - projectIdRequired: false, - packageJson: require('../package.json') - }; - - common.Service.call(this, config, options); -} - -util.inherits(Resource, common.Service); - -/** - * Create a project. - * - * **This method only works if you are authenticated as yourself, e.g. using the - * gcloud SDK.** - * - * @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/v1/projects/create} - * - * @param {string} id - ID of the project. - * @param {object=} options - See a - * [Project resource](https://cloud.google.com/resource-manager/reference/rest/v1/projects#Project). - * @param {function=} callback - The callback function. - * @param {?error} callback.err - An error returned while making this request. - * @param {module:resource/project} callback.project - The created Project - * object. - * @param {object} callback.apiResponse - The full API response. - * - * @example - * var id = 'new-project-id'; - * - * resource.createProject(id, function(err, project, operation, apiResponse) { - * if (err) { - * // Error handling omitted. - * } - * - * // `project` is a new Project instance. - * // `operation` will emit `error` or `complete` when the status updates. - * - * operation - * .on('error', function(err) {}) - * .on('complete', function() { - * // Project was created successfully! - * }); - * }); - * - * //- - * // If the callback is omitted, we'll return a Promise. - * //- - * resource.createProject(id) - * .then(function(data) { - * var project = data[0]; - * var operation = data[1]; - * var apiResponse = data[2]; - * - * return operation.promise(); - * }) - * .then(function(data) { - * var operationMetadata = data[0]; - * - * // Project created successfully! - * }); - */ -Resource.prototype.createProject = function(id, options, callback) { - var self = this; - - if (is.fn(options)) { - callback = options; - options = {}; - } - - this.request({ - method: 'POST', - uri: '/projects', - json: extend({}, options, { - projectId: id - }) - }, function(err, resp) { - if (err) { - callback(err, null, resp); - return; - } - - var project = self.project(resp.projectId); - - var operation = self.operation(resp.name); - operation.metadata = resp; - - callback(null, project, operation, resp); - }); -}; - -/** - * Get a list of projects. - * - * @resource [Projects Overview]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects} - * @resource [projects: list API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects/list} - * - * @param {object=} options - Operation search options. - * @param {boolean} options.autoPaginate - Have pagination handled - * automatically. Default: true. - * @param {string} options.filter - An expression for filtering the results. - * @param {number} options.maxApiCalls - Maximum number of API calls to make. - * @param {number} options.maxResults - Maximum number of results to return. - * @param {number} options.pageSize - Maximum number of projects to return. - * @param {string} options.pageToken - A previously-returned page token - * representing part of the larger set of results to view. - * @param {function} callback - The callback function. - * @param {?error} callback.err - An error returned while making this request. - * @param {module:resource/project[]} callback.operations - Project objects from - * your account. - * @param {object} callback.apiResponse - The full API response. - * - * @example - * resource.getProjects(function(err, projects) { - * // `projects` is an array of `Project` objects. - * }); - * - * //- - * // To control how many API requests are made and page through the results - * // manually, set `autoPaginate` to `false`. - * //- - * function callback(err, projects, nextQuery, apiResponse) { - * if (nextQuery) { - * // More results exist. - * resource.getProjects(nextQuery, callback); - * } - * } - * - * resource.getProjects({ - * autoPaginate: false - * }, callback); - * - * //- - * // If the callback is omitted, we'll return a Promise. - * //- - * resource.getProjects().then(function(data) { - * var projects = data[0]; - * }); - */ -Resource.prototype.getProjects = function(options, callback) { - var self = this; - - if (is.fn(options)) { - callback = options; - options = {}; - } - - options = options || {}; - - this.request({ - uri: '/projects', - qs: options - }, function(err, resp) { - if (err) { - callback(err, null, null, resp); - return; - } - - var nextQuery = null; - - if (resp.nextPageToken) { - nextQuery = extend({}, options, { - pageToken: resp.nextPageToken - }); - } - - var projects = (resp.projects || []).map(function(project) { - var projectInstance = self.project(project.projectId); - projectInstance.metadata = project; - return projectInstance; - }); - - callback(null, projects, nextQuery, resp); - }); -}; - -/** - * Get a list of {module:resource/project} objects as a readable object stream. - * - * @param {object=} query - Configuration object. See - * {module:resource#getProjects} for a complete list of options. - * @return {stream} - * - * @example - * resource.getProjectsStream() - * .on('error', console.error) - * .on('data', function(project) { - * // `project` is a `Project` object. - * }) - * .on('end', function() { - * // All projects retrieved. - * }); - * - * //- - * // If you anticipate many results, you can end a stream early to prevent - * // unnecessary processing and API requests. - * //- - * resource.getProjectsStream() - * .on('data', function(project) { - * this.end(); - * }); - */ -Resource.prototype.getProjectsStream = - common.paginator.streamify('getProjects'); - -/*! Developer Documentation - * - * @returns {module:common/operation} - */ -/** - * Get a reference to an existing operation. - * - * @throws {Error} If a name is not provided. - * - * @param {string} name - The name of the operation. - * - * @example - * var operation = resource.operation('68850831366825'); - */ -Resource.prototype.operation = function(name) { - if (!name) { - throw new Error('A name must be specified for an operation.'); - } - - return new common.Operation({ - parent: this, - id: name - }); -}; - -/** - * Create a Project object. See {module:resoucemanager/createProject} to create - * a project. - * - * @throws {Error} If an ID is not provided. - * - * @param {string} id - The ID of the project (eg: `grape-spaceship-123`). - * @return {module:resource/project} - * - * @example - * var project = resource.project('grape-spaceship-123'); - */ -Resource.prototype.project = function(id) { - id = id || this.projectId; - - if (!id) { - throw new Error('A project ID is required.'); - } - - return new Project(this, id); -}; - -/*! Developer Documentation - * - * These methods can be auto-paginated. - */ -common.paginator.extend(Resource, ['getProjects']); - -/*! Developer Documentation - * - * All async methods (except for streams) will return a Promise in the event - * that a callback is omitted. - */ -common.util.promisifyAll(Resource, { - exclude: [ - 'operation', - 'project' - ] -}); - -Resource.Project = Project; - -module.exports = Resource; diff --git a/packages/resource/src/project.js b/packages/resource/src/project.js deleted file mode 100644 index f25e1bbc382..00000000000 --- a/packages/resource/src/project.js +++ /dev/null @@ -1,281 +0,0 @@ -/*! - * Copyright 2015 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 resource/project - */ - -'use strict'; - -var common = require('@google-cloud/common'); -var util = require('util'); - -/*! Developer Documentation - * - * @param {module:resource} resource - Resource object this project belongs to. - * @param {string} id - The project's ID. - */ -/** - * A Project object allows you to interact with a Google Cloud Platform project. - * - * @resource [Projects Overview]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects} - * @resource [Project Resource]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects#Project} - * - * @constructor - * @alias module:resource/project - * - * @example - * var project = resource.project('grape-spaceship-123'); - */ -function Project(resource, id) { - var methods = { - /** - * Create a project. - * - * @param {object=} config - See {module:resource#createProject}. - * - * @example - * project.create(function(err, project, operation, apiResponse) { - * if (err) { - * // Error handling omitted. - * } - * - * // `operation` will emit `error` or `complete` when the status updates. - * - * operation - * .on('error', function(err) {}) - * .on('complete', function() { - * // Project was created successfully! - * }); - * }); - * - * //- - * // If the callback is omitted, we'll return a Promise. - * //- - * project.create() - * .then(function(data) { - * var project = data[0]; - * var operation = data[1]; - * var apiResponse = data[2]; - * - * return operation.promise(); - * }) - * .then(function(data) { - * var operationMetadata = data[0]; - * - * // Project created successfully! - * }); - */ - create: true, - - /** - * Delete the project. - * - * **This method only works if you are authenticated as yourself, e.g. using - * the gcloud SDK.** - * - * @resource [projects: delete API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects/delete} - * - * @param {function=} callback - The callback function. - * @param {?error} callback.err - An error returned while making this - * request. - * @param {object} callback.apiResponse - The full API response. - * - * @example - * project.delete(function(err, apiResponse) { - * if (!err) { - * // The project was deleted! - * } - * }); - * - * //- - * // If the callback is omitted, we'll return a Promise. - * //- - * project.delete().then(function(data) { - * var apiResponse = data[0]; - * }); - */ - delete: true, - - /** - * Check if the project exists. - * - * @param {function} callback - The callback function. - * @param {?error} callback.err - An error returned while making this - * request. - * @param {boolean} callback.exists - Whether the project exists or not. - * - * @example - * project.exists(function(err, exists) {}); - * - * //- - * // If the callback is omitted, we'll return a Promise. - * //- - * project.exists().then(function(data) { - * var exists = data[0]; - * }); - */ - exists: true, - - /** - * Get a project if it exists. - * - * You may optionally use this to "get or create" an object by providing an - * object with `autoCreate` set to `true`. Any extra configuration that is - * normally required for the `create` method must be contained within this - * object as well. - * - * @param {options=} options - Configuration object. - * @param {boolean} options.autoCreate - Automatically create the object if - * it does not exist. Default: `false` - * - * @example - * project.get(function(err, project, apiResponse) { - * // `project.metadata` has been populated. - * }); - * - * //- - * // 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 metadata for the project. - * - * @resource [projects: get API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects/get} - * - * @param {function=} callback - The callback function. - * @param {?error} callback.err - An error returned while making this - * request. - * @param {?object} callback.metadata - Metadata of the project from the - * API. - * @param {object} callback.apiResponse - Raw 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, - - /** - * Set the project's metadata. - * - * **This method only works if you are authenticated as yourself, e.g. using - * the gcloud SDK.** - * - * @resource [projects: update API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects/update} - * @resource [Project Resource]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects#Project} - * - * @param {object} metadata - See a - * [Project resource](https://cloud.google.com/resource-manager/reference/rest/v1/projects#Project). - * @param {function=} callback - The callback function. - * @param {?error} callback.err - An error returned while making this - * request. - * @param {object} callback.apiResponse - The full API response. - * - * @example - * var metadata = { - * name: 'New name' - * }; - * - * project.setMetadata(metadata, function(err, apiResponse) { - * if (!err) { - * // The project has been successfully updated. - * } - * }); - * - * //- - * // If the callback is omitted, we'll return a Promise. - * //- - * project.setMetadata(metadata).then(function(data) { - * var apiResponse = data[0]; - * }); - */ - setMetadata: { - reqOpts: { - method: 'PUT' - } - } - }; - - common.ServiceObject.call(this, { - parent: resource, - baseUrl: '/projects', - id: id, - createMethod: resource.createProject.bind(resource), - methods: methods - }); -} - -util.inherits(Project, common.ServiceObject); - -/** - * Restore a project. - * - * **This method only works if you are authenticated as yourself, e.g. using the - * gcloud SDK.** - * - * @resource [projects: undelete API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects/undelete} - * - * @param {function=} callback - The callback function. - * @param {?error} callback.err - An error returned while making this request. - * @param {object} callback.apiResponse - Raw API response. - * - * @example - * project.restore(function(err, apiResponse) { - * if (!err) { - * // Project restored. - * } - * }); - * - * //- - * // If the callback is omitted, we'll return a Promise. - * //- - * project.restore().then(function(data) { - * var apiResponse = data[0]; - * }); - */ -Project.prototype.restore = function(callback) { - callback = callback || common.util.noop; - - this.request({ - method: 'POST', - uri: ':undelete' - }, function(err, resp) { - callback(err, resp); - }); -}; - -/*! 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; diff --git a/packages/resource/system-test/resource.js b/packages/resource/system-test/resource.js deleted file mode 100644 index 91fa8a6baf5..00000000000 --- a/packages/resource/system-test/resource.js +++ /dev/null @@ -1,231 +0,0 @@ -/*! - * Copyright 2015 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 async = require('async'); -var exec = require('methmeth'); -var googleAuth = require('google-auto-auth'); -var uuid = require('uuid'); - -var env = require('../../../system-test/env.js'); -var Resource = require('../'); - -describe('Resource', function() { - var PREFIX = 'gcloud-tests-'; - var resource = new Resource(env); - var project = resource.project(); - - describe('resource', function() { - it('should get a list of projects', function(done) { - resource.getProjects(function(err, projects) { - assert.ifError(err); - assert(projects.length > 0); - done(); - }); - }); - - it('should get a list of projects in stream mode', function(done) { - var resultsMatched = 0; - - resource.getProjectsStream() - .on('error', done) - .on('data', function() { - resultsMatched++; - }) - .on('end', function() { - assert(resultsMatched > 0); - done(); - }); - }); - }); - - describe('project', function() { - it('should get metadata', function(done) { - project.getMetadata(function(err, metadata) { - assert.ifError(err); - - assert.notStrictEqual(metadata.projectId, undefined); - - if (env.projectId) { - assert.strictEqual(metadata.projectId, env.projectId); - } - - done(); - }); - }); - }); - - // Auth through the gcloud SDK is required to: - // - // - Create a project - // - Set metadata - // - Restore a project - // - Delete a project - describe('lifecycle', function() { - var CAN_RUN_TESTS = true; - var testProjects = []; - - var resource = new Resource({ - projectId: env.projectId - }); - - var project = resource.project(generateName('project')); - - before(function(done) { - var authClient = googleAuth(); - - async.series([ - function(callback) { - // See if an auth token exists. - authClient.getToken(function(err) { - CAN_RUN_TESTS = err === null; - callback(); - }); - }, - - deleteTestProjects - ], function(err) { - if (err || !CAN_RUN_TESTS) { - done(err); - return; - } - - project.create(function(err, project, operation) { - if (err) { - done(err); - return; - } - - testProjects.push(project); - - operation - .on('error', done) - .on('complete', function() { - done(); - }); - }); - }); - }); - - beforeEach(function() { - if (!CAN_RUN_TESTS) { - this.skip(); - } - }); - - after(function(done) { - if (!CAN_RUN_TESTS) { - this.skip(); - return; - } - - deleteTestProjects(done); - }); - - it('should have created the project', function(done) { - project.getMetadata(function(err, metadata) { - assert.ifError(err); - assert.strictEqual(metadata.projectId, project.id); - done(); - }); - }); - - it('should run operation as a promise', function(done) { - var project = resource.project(generateName('project')); - - project.create() - .then(function(response) { - var operation = response[1]; - return operation.promise(); - }) - .then(function() { - testProjects.push(project); - return project.getMetadata(); - }) - .then(function(response) { - var metadata = response[0]; - assert.strictEqual(metadata.projectId, project.id); - done(); - }); - }); - - it('should set metadata', function(done) { - var newProjectName = 'gcloud-tests-project-name'; - - project.getMetadata(function(err, metadata) { - assert.ifError(err); - - var originalProjectName = metadata.name; - assert.notStrictEqual(originalProjectName, newProjectName); - - project.setMetadata({ - name: newProjectName - }, function(err) { - assert.ifError(err); - - project.setMetadata({ - name: originalProjectName - }, done); - }); - }); - }); - - it('should restore the project', function(done) { - project.delete(function(err) { - assert.ifError(err); - project.restore(done); - }); - }); - - function deleteTestProjects(callback) { - if (!CAN_RUN_TESTS) { - callback(); - return; - } - - async.series([ - function(callback) { - async.eachSeries(testProjects, exec('delete'), callback); - }, - - function(callback) { - resource.getProjects(function(err, projects) { - if (err) { - callback(err); - return; - } - - var projectsToDelete = projects.filter(function(project) { - var isTestProject = project.id.indexOf(PREFIX) === 0; - var deleted = - project.metadata.lifecycleState === 'DELETE_REQUESTED'; - - return isTestProject && !deleted; - }); - - async.each(projectsToDelete, exec('delete'), callback); - }); - } - ], callback); - } - - function generateName(resourceType) { - return PREFIX + resourceType + '-' + uuid.v1().substr(0, 8); - } - }); -}); diff --git a/packages/resource/test/index.js b/packages/resource/test/index.js deleted file mode 100644 index 79e5c4acfbd..00000000000 --- a/packages/resource/test/index.js +++ /dev/null @@ -1,381 +0,0 @@ -/*! - * Copyright 2015 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 arrify = require('arrify'); -var assert = require('assert'); -var extend = require('extend'); -var proxyquire = require('proxyquire'); -var util = require('@google-cloud/common').util; - -function FakeOperation() { - this.calledWith_ = arguments; -} - -function FakeProject() { - this.calledWith_ = arguments; -} - -function FakeService() { - this.calledWith_ = arguments; -} - -var extended = false; -var fakePaginator = { - extend: function(Class, methods) { - if (Class.name !== 'Resource') { - return; - } - - methods = arrify(methods); - assert.equal(Class.name, 'Resource'); - assert.deepEqual(methods, ['getProjects']); - extended = true; - }, - streamify: function(methodName) { - return methodName; - } -}; - -var promisified = true; -var makeAuthenticatedRequestFactoryOverride; -var fakeUtil = extend({}, util, { - makeAuthenticatedRequestFactory: function() { - if (makeAuthenticatedRequestFactoryOverride) { - return makeAuthenticatedRequestFactoryOverride.apply(null, arguments); - } - - return util.makeAuthenticatedRequestFactory.apply(null, arguments); - }, - promisifyAll: function(Class, options) { - if (Class.name !== 'Resource') { - return; - } - - promisified = true; - assert.deepEqual(options.exclude, ['operation', 'project']); - } -}); - -describe('Resource', function() { - var PROJECT_ID = 'test-project-id'; - - var Resource; - var resource; - - before(function() { - Resource = proxyquire('../', { - '@google-cloud/common': { - Operation: FakeOperation, - Service: FakeService, - paginator: fakePaginator, - util: fakeUtil - }, - './project.js': FakeProject - }); - }); - - beforeEach(function() { - makeAuthenticatedRequestFactoryOverride = null; - - resource = new Resource({ - projectId: PROJECT_ID - }); - }); - - describe('instantiation', function() { - it('should extend the correct methods', function() { - assert(extended); // See `fakePaginator.extend` - }); - - it('should streamify the correct methods', function() { - assert.strictEqual(resource.getProjectsStream, 'getProjects'); - }); - - it('should promisify all tlhe things', function() { - assert(promisified); - }); - - it('should normalize the arguments', function() { - var normalizeArguments = fakeUtil.normalizeArguments; - var normalizeArgumentsCalled = false; - var fakeOptions = { projectId: PROJECT_ID }; - var fakeContext = {}; - - fakeUtil.normalizeArguments = function(context, options) { - normalizeArgumentsCalled = true; - assert.strictEqual(context, fakeContext); - assert.strictEqual(options, fakeOptions); - return options; - }; - - Resource.call(fakeContext, fakeOptions); - assert(normalizeArgumentsCalled); - - fakeUtil.normalizeArguments = normalizeArguments; - }); - - it('should inherit from Service', function() { - assert(resource instanceof FakeService); - - var calledWith = resource.calledWith_[0]; - - var baseUrl = 'https://cloudresourcemanager.googleapis.com/v1'; - assert.strictEqual(calledWith.baseUrl, baseUrl); - assert.deepEqual(calledWith.scopes, [ - 'https://www.googleapis.com/auth/cloud-platform' - ]); - assert.strictEqual(calledWith.projectIdRequired, false); - assert.deepEqual(calledWith.packageJson, require('../package.json')); - }); - }); - - describe('createProject', function() { - var NEW_PROJECT_ID = 'new-project-id'; - var OPTIONS = { a: 'b', c: 'd' }; - var EXPECTED_BODY = extend({}, OPTIONS, { projectId: NEW_PROJECT_ID }); - - it('should not require any options', function(done) { - var expectedBody = { projectId: NEW_PROJECT_ID }; - - resource.request = function(reqOpts) { - assert.deepEqual(reqOpts.json, expectedBody); - done(); - }; - - resource.createProject(NEW_PROJECT_ID, assert.ifError); - }); - - it('should make the correct API request', function(done) { - resource.request = function(reqOpts) { - assert.strictEqual(reqOpts.method, 'POST'); - assert.strictEqual(reqOpts.uri, '/projects'); - assert.deepEqual(reqOpts.json, EXPECTED_BODY); - - done(); - }; - - resource.createProject(NEW_PROJECT_ID, OPTIONS, assert.ifError); - }); - - describe('error', function() { - var error = new Error('Error.'); - var apiResponse = { a: 'b', c: 'd' }; - - beforeEach(function() { - resource.request = function(reqOpts, callback) { - callback(error, apiResponse); - }; - }); - - it('should execute callback with error & API response', function(done) { - resource.createProject(NEW_PROJECT_ID, OPTIONS, function(err, p, res) { - assert.strictEqual(err, error); - assert.strictEqual(p, null); - assert.strictEqual(res, apiResponse); - done(); - }); - }); - }); - - describe('success', function() { - var apiResponse = { - name: 'operation-name' - }; - - beforeEach(function() { - resource.request = function(reqOpts, callback) { - callback(null, apiResponse); - }; - }); - - it('should exec callback with Project & API response', function(done) { - var project = {}; - var fakeOperation = {}; - - resource.project = function(id) { - assert.strictEqual(id, apiResponse.projectId); - return project; - }; - - resource.operation = function(name) { - assert.strictEqual(name, apiResponse.name); - return fakeOperation; - }; - - resource.createProject(NEW_PROJECT_ID, OPTIONS, function(e, p, o, res) { - assert.ifError(e); - - assert.strictEqual(p, project); - - assert.strictEqual(o, fakeOperation); - assert.strictEqual(o.metadata, apiResponse); - - assert.strictEqual(res, apiResponse); - - done(); - }); - }); - }); - }); - - describe('getProjects', function() { - it('should accept only a callback', function(done) { - resource.request = function(reqOpts) { - assert.deepEqual(reqOpts.qs, {}); - done(); - }; - - resource.getProjects(assert.ifError); - }); - - it('should make the correct API request', function(done) { - var query = { a: 'b', c: 'd' }; - - resource.request = function(reqOpts) { - assert.strictEqual(reqOpts.uri, '/projects'); - assert.strictEqual(reqOpts.qs, query); - - done(); - }; - - resource.getProjects(query, assert.ifError); - }); - - describe('error', function() { - var error = new Error('Error.'); - var apiResponse = { a: 'b', c: 'd' }; - - beforeEach(function() { - resource.request = function(reqOpts, callback) { - callback(error, apiResponse); - }; - }); - - it('should execute callback with error & API response', function(done) { - resource.getProjects({}, function(err, projects, nextQuery, apiResp) { - assert.strictEqual(err, error); - assert.strictEqual(projects, null); - assert.strictEqual(nextQuery, null); - assert.strictEqual(apiResp, apiResponse); - done(); - }); - }); - }); - - describe('success', function() { - var apiResponse = { - projects: [ - { projectId: PROJECT_ID } - ] - }; - - beforeEach(function() { - resource.request = function(reqOpts, callback) { - callback(null, apiResponse); - }; - }); - - it('should build a nextQuery if necessary', function(done) { - var nextPageToken = 'next-page-token'; - var apiResponseWithNextPageToken = extend({}, apiResponse, { - nextPageToken: nextPageToken - }); - var expectedNextQuery = { - pageToken: nextPageToken - }; - - resource.request = function(reqOpts, callback) { - callback(null, apiResponseWithNextPageToken); - }; - - resource.getProjects({}, function(err, projects, nextQuery) { - assert.ifError(err); - - assert.deepEqual(nextQuery, expectedNextQuery); - - done(); - }); - }); - - it('should execute callback with Projects & API resp', function(done) { - var project = {}; - - resource.project = function(name) { - assert.strictEqual(name, apiResponse.projects[0].projectId); - return project; - }; - - resource.getProjects({}, function(err, projects, nextQuery, apiResp) { - assert.ifError(err); - - assert.strictEqual(projects[0], project); - assert.strictEqual(projects[0].metadata, apiResponse.projects[0]); - - assert.strictEqual(apiResp, apiResponse); - - done(); - }); - }); - }); - }); - - describe('operation', function() { - var NAME = 'operation-name'; - - it('should throw if a name is not provided', function() { - assert.throws(function() { - resource.operation(); - }, /A name must be specified for an operation\./); - }); - - it('should return a common/operation', function() { - var operation = resource.operation(NAME); - - assert(operation instanceof FakeOperation); - - assert.deepEqual(operation.calledWith_[0], { - parent: resource, - id: NAME - }); - }); - }); - - describe('project', function() { - it('should return a Project object', function() { - var project = resource.project(PROJECT_ID); - assert(project instanceof FakeProject); - assert.strictEqual(project.calledWith_[0], resource); - assert.strictEqual(project.calledWith_[1], PROJECT_ID); - }); - - it('should use the project ID from the resource', function() { - resource.projectId = PROJECT_ID; - var project = resource.project(); - assert(project instanceof FakeProject); - assert.strictEqual(project.calledWith_[1], PROJECT_ID); - }); - - it('should throw if no project ID was given or found', function() { - var resourceWithoutProjectId = new Resource({}); - - assert.throws(function() { - resourceWithoutProjectId.project(); - }, /A project ID is required\./); - }); - }); -}); diff --git a/packages/resource/test/project.js b/packages/resource/test/project.js deleted file mode 100644 index dbf3efd0215..00000000000 --- a/packages/resource/test/project.js +++ /dev/null @@ -1,137 +0,0 @@ -/*! - * Copyright 2015 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 nodeutil = require('util'); -var proxyquire = require('proxyquire'); -var ServiceObject = require('@google-cloud/common').ServiceObject; -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; - ServiceObject.apply(this, arguments); -} - -nodeutil.inherits(FakeServiceObject, ServiceObject); - -describe('Project', function() { - var Project; - var project; - - var RESOURCE = { - createProject: util.noop - }; - var ID = 'project-id'; - - before(function() { - Project = proxyquire('../src/project.js', { - '@google-cloud/common': { - ServiceObject: FakeServiceObject, - util: fakeUtil - } - }); - }); - - beforeEach(function() { - project = new Project(RESOURCE, ID); - }); - - describe('instantiation', function() { - it('should inherit from ServiceObject', function(done) { - var resourceInstance = extend({}, RESOURCE, { - createProject: { - bind: function(context) { - assert.strictEqual(context, resourceInstance); - done(); - } - } - }); - - var project = new Project(resourceInstance, ID); - assert(project instanceof ServiceObject); - - var calledWith = project.calledWith_[0]; - - assert.strictEqual(calledWith.parent, resourceInstance); - assert.strictEqual(calledWith.baseUrl, '/projects'); - assert.strictEqual(calledWith.id, ID); - assert.deepEqual(calledWith.methods, { - create: true, - delete: true, - exists: true, - get: true, - getMetadata: true, - setMetadata: { - reqOpts: { - method: 'PUT' - } - } - }); - }); - - it('should promisify all tlhe things', function() { - assert(promisified); - }); - }); - - describe('restore', function() { - var error = new Error('Error.'); - var apiResponse = { a: 'b', c: 'd' }; - - beforeEach(function() { - project.request = function(reqOpts, callback) { - callback(error, apiResponse); - }; - }); - - it('should make the correct API request', function(done) { - project.request = function(reqOpts) { - assert.strictEqual(reqOpts.method, 'POST'); - assert.strictEqual(reqOpts.uri, ':undelete'); - - done(); - }; - - project.restore(assert.ifError); - }); - - it('should execute the callback with error & API response', function(done) { - project.restore(function(err, apiResponse_) { - assert.strictEqual(err, error); - assert.strictEqual(apiResponse_, apiResponse); - done(); - }); - }); - - it('should not require a callback', function() { - assert.doesNotThrow(function() { - project.restore(); - }); - }); - }); -});