Skip to content

Commit 5c7ff92

Browse files
resource: use v1 of API (#1795)
1 parent 75992b3 commit 5c7ff92

File tree

5 files changed

+245
-57
lines changed

5 files changed

+245
-57
lines changed

packages/resource/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,17 @@
5050
"resource"
5151
],
5252
"dependencies": {
53-
"@google-cloud/common": "^0.7.0",
53+
"@google-cloud/common": "^0.8.0",
5454
"extend": "^3.0.0",
5555
"is": "^3.0.1"
5656
},
5757
"devDependencies": {
5858
"arrify": "^1.0.0",
59+
"async": "^2.1.2",
5960
"google-auto-auth": "^0.2.4",
61+
"methmeth": "^1.1.0",
6062
"mocha": "^3.0.1",
63+
"node-uuid": "^1.4.7",
6164
"proxyquire": "^1.7.10"
6265
},
6366
"scripts": {

packages/resource/src/index.js

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function Resource(options) {
6464
}
6565

6666
var config = {
67-
baseUrl: 'https://cloudresourcemanager.googleapis.com/v1beta1',
67+
baseUrl: 'https://cloudresourcemanager.googleapis.com/v1',
6868
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
6969
projectIdRequired: false,
7070
packageJson: require('../package.json')
@@ -84,31 +84,51 @@ util.inherits(Resource, common.Service);
8484
* gcloud SDK.**
8585
*
8686
* @resource [Projects Overview]{@link https://cloud.google.com/compute/docs/networking#networks}
87-
* @resource [projects: create API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/create}
87+
* @resource [projects: create API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects/create}
8888
*
89-
* @param {string} name - Name of the project.
89+
* @param {string} id - ID of the project.
9090
* @param {object=} options - See a
91-
* [Project resource](https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects#Project).
91+
* [Project resource](https://cloud.google.com/resource-manager/reference/rest/v1/projects#Project).
9292
* @param {function=} callback - The callback function.
9393
* @param {?error} callback.err - An error returned while making this request.
9494
* @param {module:resource/project} callback.project - The created Project
9595
* object.
9696
* @param {object} callback.apiResponse - The full API response.
9797
*
9898
* @example
99-
* resource.createProject('new project name', function(err, project) {
100-
* if (!err) {
101-
* // `project` is a new Project instance.
99+
* var id = 'new-project-id';
100+
*
101+
* resource.createProject(id, function(err, project, operation, apiResponse) {
102+
* if (err) {
103+
* // Error handling omitted.
102104
* }
105+
*
106+
* // `project` is a new Project instance.
107+
* // `operation` will emit `error` or `complete` when the status updates.
108+
*
109+
* operation
110+
* .on('error', function(err) {})
111+
* .on('complete', function() {
112+
* // Project was created successfully!
113+
* });
103114
* });
104115
*
105116
* //-
106117
* // If the callback is omitted, we'll return a Promise.
107118
* //-
108-
* resource.createProject('new project name').then(function(data) {
109-
* var project = data[0];
110-
* var apiResponse = data[1];
111-
* });
119+
* resource.createProject(id)
120+
* .then(function(data) {
121+
* var project = data[0];
122+
* var operation = data[1];
123+
* var apiResponse = data[2];
124+
*
125+
* return operation.promise();
126+
* })
127+
* .then(function(data) {
128+
* var operationMetadata = data[0];
129+
*
130+
* // Project created successfully!
131+
* });
112132
*/
113133
Resource.prototype.createProject = function(id, options, callback) {
114134
var self = this;
@@ -131,17 +151,19 @@ Resource.prototype.createProject = function(id, options, callback) {
131151
}
132152

133153
var project = self.project(resp.projectId);
134-
project.metadata = resp;
135154

136-
callback(null, project, resp);
155+
var operation = self.operation(resp.name);
156+
operation.metadata = resp;
157+
158+
callback(null, project, operation, resp);
137159
});
138160
};
139161

140162
/**
141163
* Get a list of projects.
142164
*
143-
* @resource [Projects Overview]{@link https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects}
144-
* @resource [projects: list API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/list}
165+
* @resource [Projects Overview]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects}
166+
* @resource [projects: list API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects/list}
145167
*
146168
* @param {object=} options - Operation search options.
147169
* @param {boolean} options.autoPaginate - Have pagination handled
@@ -251,6 +273,31 @@ Resource.prototype.getProjects = function(options, callback) {
251273
Resource.prototype.getProjectsStream =
252274
common.paginator.streamify('getProjects');
253275

276+
/*! Developer Documentation
277+
*
278+
* @returns {module:common/operation}
279+
*/
280+
/**
281+
* Get a reference to an existing operation.
282+
*
283+
* @throws {Error} If a name is not provided.
284+
*
285+
* @param {string} name - The name of the operation.
286+
*
287+
* @example
288+
* var operation = resource.operation('68850831366825');
289+
*/
290+
Resource.prototype.operation = function(name) {
291+
if (!name) {
292+
throw new Error('A name must be specified for an operation.');
293+
}
294+
295+
return new common.Operation({
296+
parent: this,
297+
id: name
298+
});
299+
};
300+
254301
/**
255302
* Create a Project object. See {module:resoucemanager/createProject} to create
256303
* a project.
@@ -285,7 +332,10 @@ common.paginator.extend(Resource, ['getProjects']);
285332
* that a callback is omitted.
286333
*/
287334
common.util.promisifyAll(Resource, {
288-
exclude: ['project']
335+
exclude: [
336+
'operation',
337+
'project'
338+
]
289339
});
290340

291341
Resource.Project = Project;

packages/resource/src/project.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ var util = require('util');
3131
/**
3232
* A Project object allows you to interact with a Google Cloud Platform project.
3333
*
34-
* @resource [Projects Overview]{@link https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects}
35-
* @resource [Project Resource]{@link https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects#Project}
34+
* @resource [Projects Overview]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects}
35+
* @resource [Project Resource]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects#Project}
3636
*
3737
* @constructor
3838
* @alias module:resource/project
@@ -48,19 +48,36 @@ function Project(resource, id) {
4848
* @param {object=} config - See {module:resource#createProject}.
4949
*
5050
* @example
51-
* project.create(function(err, zone, apiResponse) {
52-
* if (!err) {
53-
* // The zone was created successfully.
51+
* project.create(function(err, project, operation, apiResponse) {
52+
* if (err) {
53+
* // Error handling omitted.
5454
* }
55+
*
56+
* // `operation` will emit `error` or `complete` when the status updates.
57+
*
58+
* operation
59+
* .on('error', function(err) {})
60+
* .on('complete', function() {
61+
* // Project was created successfully!
62+
* });
5563
* });
5664
*
5765
* //-
5866
* // If the callback is omitted, we'll return a Promise.
5967
* //-
60-
* project.create().then(function(data) {
61-
* var zone = data[0];
62-
* var apiResponse = data[0];
63-
* });
68+
* project.create()
69+
* .then(function(data) {
70+
* var project = data[0];
71+
* var operation = data[1];
72+
* var apiResponse = data[2];
73+
*
74+
* return operation.promise();
75+
* })
76+
* .then(function(data) {
77+
* var operationMetadata = data[0];
78+
*
79+
* // Project created successfully!
80+
* });
6481
*/
6582
create: true,
6683

@@ -70,7 +87,7 @@ function Project(resource, id) {
7087
* **This method only works if you are authenticated as yourself, e.g. using
7188
* the gcloud SDK.**
7289
*
73-
* @resource [projects: delete API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/delete}
90+
* @resource [projects: delete API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects/delete}
7491
*
7592
* @param {function=} callback - The callback function.
7693
* @param {?error} callback.err - An error returned while making this
@@ -143,7 +160,7 @@ function Project(resource, id) {
143160
/**
144161
* Get the metadata for the project.
145162
*
146-
* @resource [projects: get API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/get}
163+
* @resource [projects: get API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects/get}
147164
*
148165
* @param {function=} callback - The callback function.
149166
* @param {?error} callback.err - An error returned while making this
@@ -171,11 +188,11 @@ function Project(resource, id) {
171188
* **This method only works if you are authenticated as yourself, e.g. using
172189
* the gcloud SDK.**
173190
*
174-
* @resource [projects: update API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/update}
175-
* @resource [Project Resource]{@link https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects#Project}
191+
* @resource [projects: update API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects/update}
192+
* @resource [Project Resource]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects#Project}
176193
*
177194
* @param {object} metadata - See a
178-
* [Project resource](https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects#Project).
195+
* [Project resource](https://cloud.google.com/resource-manager/reference/rest/v1/projects#Project).
179196
* @param {function=} callback - The callback function.
180197
* @param {?error} callback.err - An error returned while making this
181198
* request.
@@ -223,7 +240,7 @@ util.inherits(Project, common.ServiceObject);
223240
* **This method only works if you are authenticated as yourself, e.g. using the
224241
* gcloud SDK.**
225242
*
226-
* @resource [projects: undelete API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/undelete}
243+
* @resource [projects: undelete API Documentation]{@link https://cloud.google.com/resource-manager/reference/rest/v1/projects/undelete}
227244
*
228245
* @param {function=} callback - The callback function.
229246
* @param {?error} callback.err - An error returned while making this request.

0 commit comments

Comments
 (0)