Skip to content

Commit 7d67d47

Browse files
committed
Compute: add operation support. Add operation parameter to callbacks of methods wrapping an asynchronous API (in place of APIresponse).
1 parent 4ebb6b0 commit 7d67d47

File tree

10 files changed

+747
-48
lines changed

10 files changed

+747
-48
lines changed

lib/compute/address.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,23 @@ function Address(region, name, metadata) {
6969
* @param {function} callback - The callback function.
7070
*
7171
* @example
72-
* address.delete(function(err) {});
72+
* address.delete(function(err, operation) {
73+
* // `operation` is an Operation object and can be used to check the status
74+
* // of address deletion.
75+
* });
7376
*/
7477
Address.prototype.delete = function(callback) {
7578
callback = callback || util.noop;
76-
this.makeReq_('DELETE', '', null, true, callback);
79+
var region = this.region;
80+
this.makeReq_('DELETE', '', null, true, function(err, resp) {
81+
if (err) {
82+
callback(err);
83+
return;
84+
}
85+
var operation = region.operation(resp.name);
86+
operation.metadata = resp;
87+
callback(null, operation);
88+
});
7789
};
7890

7991
/**

lib/compute/disk.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,23 @@ function Disk(zone, name, metadata) {
6969
* @param {function} callback - The callback function.
7070
*
7171
* @example
72-
* disk.delete(function(err) {});
72+
* disk.delete(function(err, operation) {
73+
* // `operation` is an Operation object and can be used to check the status
74+
* // of disk deletion.
75+
* });
7376
*/
7477
Disk.prototype.delete = function(callback) {
7578
callback = callback || util.noop;
76-
this.makeReq_('DELETE', '', null, true, callback);
79+
var zone = this.zone;
80+
this.makeReq_('DELETE', '', null, true, function(err, resp) {
81+
if (err) {
82+
callback(err);
83+
return;
84+
}
85+
var operation = zone.operation(resp.name);
86+
operation.metadata = resp;
87+
callback(null, operation);
88+
});
7789
};
7890

7991
/**
@@ -106,8 +118,10 @@ Disk.prototype.getMetadata = function(callback) {
106118
* @param {function} callback - The callback function.
107119
*
108120
* @example
109-
* disk.createSnapshot('my-snapshot', function(err, snapshot, apiResponse) {
121+
* disk.createSnapshot('my-snapshot', function(err, snapshot, operation) {
110122
* // `snapshot` is a Snapshot object.
123+
* // `operation` is an Operation object and can be used to check the status
124+
* // of snapshot creation.
111125
* });
112126
*/
113127
Disk.prototype.createSnapshot = function(options, callback) {
@@ -122,14 +136,16 @@ Disk.prototype.createSnapshot = function(options, callback) {
122136
throw new Error('Name must match [a-z]([-a-z0-9]{0,61}[a-z0-9])?');
123137
}
124138

125-
var compute = this.zone.compute;
139+
var self = this;
126140
this.makeReq_('POST', '/createSnapshot', null, options, function(err, resp) {
127141
if (err) {
128142
callback(err);
129143
return;
130144
}
131-
var snapshot = compute.snapshot(options.name);
132-
callback(null, snapshot, resp);
145+
var snapshot = self.zone.compute.snapshot(options.name);
146+
var operation = self.zone.operation(resp.name);
147+
operation.metadata = resp;
148+
callback(null, snapshot, operation);
133149
});
134150
};
135151

lib/compute/firewall.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,23 @@ function Firewall(compute, name, options) {
7777
* @param {function} callback - The callback function.
7878
*
7979
* @example
80-
* firewall.delete(function(err) {});
80+
* firewall.delete(function(err, operation) {
81+
* // `operation` is an Operation object and can be used to check the status
82+
* // of firewall rule deletion.
83+
* });
8184
*/
8285
Firewall.prototype.delete = function(callback) {
8386
callback = callback || util.noop;
84-
this.makeReq_('DELETE', '', null, true, callback);
87+
var compute = this.compute;
88+
this.makeReq_('DELETE', '', null, true, function(err, resp) {
89+
if (err) {
90+
callback(err);
91+
return;
92+
}
93+
var operation = compute.operation(resp.name);
94+
operation.metadata = resp;
95+
callback(null, operation);
96+
});
8597
};
8698

8799
/**
@@ -128,8 +140,9 @@ Firewall.prototype.getMetadata = function(callback) {
128140
* @param {function} callback - The callback function.
129141
*
130142
* @example
131-
* var callback = function(err, apiResponse) {
132-
* // ...
143+
* var callback = function(err, operation) {
144+
* // `operation` is an Operation object and can be used to check the status
145+
* // of firewall rule update.
133146
* };
134147
*
135148
* firewall.setMetadata(
@@ -153,7 +166,9 @@ Firewall.prototype.setMetadata = function(metadata, callback) {
153166
return;
154167
}
155168
self.metadata = metadata;
156-
callback(null, resp);
169+
var operation = self.compute.operation(resp.name);
170+
operation.metadata = resp;
171+
callback(null, operation);
157172
});
158173
};
159174

lib/compute/index.js

+137-5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ var Snapshot = require('./snapshot.js');
5252
*/
5353
var Network = require('./network.js');
5454

55+
/**
56+
* @type {module:compute/operation}
57+
* @private
58+
*/
59+
var Operation = require('./operation.js');
60+
5561
/**
5662
* @type {module:common/util}
5763
* @private
@@ -218,6 +224,25 @@ Compute.prototype.network = function(name) {
218224
return new Network(this, name);
219225
};
220226

227+
/**
228+
* Get a reference to a Google Compute Engine operation.
229+
*
230+
* @param {string} name - Name of the existing operation.
231+
* @return {module:compute/operation}
232+
*
233+
* @example
234+
* var gcloud = require('gcloud')({
235+
* keyFilename: '/path/to/keyfile.json'
236+
* });
237+
*
238+
* var compute = gcloud.compute();
239+
*
240+
* var operation = compute.operation('operation-name');
241+
*/
242+
Compute.prototype.operation = function(name) {
243+
return new Operation(this, name);
244+
};
245+
221246
/**
222247
* Create a firewall rule. For a detailed description of method's options see
223248
* [API reference](https://goo.gl/kTMHep).
@@ -242,8 +267,10 @@ Compute.prototype.network = function(name) {
242267
* @param {function} callback - The callback function.
243268
*
244269
* @example
245-
* var callback = function(err, firewall, apiResponse) {
270+
* var callback = function(err, firewall, operation) {
246271
* // `firewall` is a Firewall object.
272+
* // `operation` is an Operation object and can be used to check the status
273+
* // of firewall rule creation.
247274
* };
248275
*
249276
* var compute = gcloud.compute({
@@ -298,7 +325,9 @@ Compute.prototype.createFirewall = function(name, options, callback) {
298325
}
299326
var firewall = self.firewall(name);
300327
firewall.metadata = options;
301-
callback(null, firewall, resp);
328+
var operation = self.operation(resp.name);
329+
operation.metadata = resp;
330+
callback(null, firewall, operation);
302331
});
303332
};
304333

@@ -316,8 +345,10 @@ Compute.prototype.createFirewall = function(name, options, callback) {
316345
* @param {function} callback - The callback function.
317346
*
318347
* @example
319-
* var callback = function(err, network, apiResponse) {
348+
* var callback = function(err, network, operation) {
320349
* // `network` is a Network object.
350+
* // `operation` is an Operation object and can be used to check the status
351+
* // of network creation.
321352
* };
322353
*
323354
* var compute = gcloud.compute({
@@ -349,7 +380,9 @@ Compute.prototype.createNetwork = function(name, options, callback) {
349380
}
350381
var network = self.network(name);
351382
network.metadata = options;
352-
callback(null, network, resp);
383+
var operation = self.operation(resp.name);
384+
operation.metadata = resp;
385+
callback(null, network, operation);
353386
});
354387
};
355388

@@ -943,6 +976,104 @@ Compute.prototype.getNetworks = function(options, callback) {
943976
});
944977
};
945978

979+
/**
980+
* Get a list of global operations. For a detailed description of method's
981+
* options see [API reference](https://goo.gl/gX4C1u).
982+
*
983+
* @param {object} options - Operation search options.
984+
* @param {boolean=} options.autoPaginate - Have pagination handled
985+
* automatically. Default: true.
986+
* @param {number=} options.maxResults - Maximum number of operations to return.
987+
* @param {string=} options.filter - Search filter. The filter must contain:
988+
* `FIELD_NAME COMPARISON_STRING LITERAL_STRING`. `FIELD_NAME` is the name
989+
* of the field to compare. `COMPARISON` is the comparison operator that can
990+
* be either `eq` or `ne`. `LITERAL_STRING` is the string to filter to.
991+
* For string fields `LITERAL_STRING` can be a regular expression.
992+
* @param {string=} pageToken - Page identifier used in paginated search.
993+
* @param {function} callback - The callback function.
994+
*
995+
* @example
996+
* var callback = function(err, operations) {
997+
* // `operations` is an array of `Operation` objects.
998+
* };
999+
*
1000+
* compute.getOperations(
1001+
* {
1002+
* filter: 'name eq operation-[0-9]'
1003+
* }, callback);
1004+
*
1005+
* //-
1006+
* // To control how many API requests are made and page through the results
1007+
* // manually, set `autoPaginate` to `false`.
1008+
* //-
1009+
* var callback = function(err, operations, nextQuery, apiResponse) {
1010+
* if (nextQuery) {
1011+
* // More results exist.
1012+
* compute.getOperations(nextQuery, callback);
1013+
* }
1014+
* };
1015+
*
1016+
* compute.getOperations({
1017+
* autoPaginate: false
1018+
* }, callback);
1019+
*
1020+
* //-
1021+
* // Get the operations from your project as a readable object stream.
1022+
* //-
1023+
* compute.getOperations()
1024+
* .on('error', console.error)
1025+
* .on('data', function(operation) {
1026+
* // `operation` is a `Operation` object.
1027+
* })
1028+
* .on('end', function() {
1029+
* // All operations retrieved.
1030+
* });
1031+
*
1032+
* //-
1033+
* // If you anticipate many results, you can end a stream early to prevent
1034+
* // unnecessary processing and API requests.
1035+
* //-
1036+
* compute.getOperations()
1037+
* .on('data', function(operation) {
1038+
* this.end();
1039+
* });
1040+
*/
1041+
Compute.prototype.getOperations = function(options, callback) {
1042+
if (util.is(options, 'function')) {
1043+
callback = options;
1044+
options = {};
1045+
}
1046+
1047+
options = options || {};
1048+
1049+
var self = this;
1050+
this.makeReq_(
1051+
'GET',
1052+
'/global/operations',
1053+
options,
1054+
null, function(err, resp) {
1055+
if (err) {
1056+
callback(err);
1057+
return;
1058+
}
1059+
1060+
var nextQuery = null;
1061+
1062+
if (resp.nextPageToken) {
1063+
nextQuery = extend({}, options, {
1064+
pageToken: resp.nextPageToken
1065+
});
1066+
}
1067+
1068+
var operations = (resp.items || []).map(function(operationObject) {
1069+
var operation = self.operation(operationObject.name);
1070+
operation.metadata = operationObject;
1071+
return operation;
1072+
});
1073+
callback(null, operations, nextQuery, resp);
1074+
});
1075+
};
1076+
9461077
/**
9471078
* Make a new request object from the provided arguments and wrap the callback
9481079
* to intercept non-successful responses.
@@ -980,7 +1111,8 @@ streamRouter.extend(Compute, [
9801111
'getAddresses',
9811112
'getSnapshots',
9821113
'getFirewalls',
983-
'getNetworks'
1114+
'getNetworks',
1115+
'getOperations'
9841116
]);
9851117

9861118
module.exports = Compute;

0 commit comments

Comments
 (0)