Skip to content

Commit fb15704

Browse files
overhaul Compute Engine
1 parent 267f350 commit fb15704

File tree

16 files changed

+1230
-453
lines changed

16 files changed

+1230
-453
lines changed

lib/bigquery/dataset.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ function Dataset(bigQuery, id) {
6363
});
6464

6565
this.bigQuery = bigQuery;
66-
this.id = id;
6766
}
6867

6968
nodeutil.inherits(Dataset, ServiceObject);
@@ -97,7 +96,7 @@ nodeutil.inherits(Dataset, ServiceObject);
9796
* var bigquery = gcloud.bigquery({
9897
* projectId: 'grape-spaceship-123'
9998
* });
100-
* var dataset = bigquery.dataset();
99+
* var dataset = bigquery.dataset('institutions');
101100
*
102101
* dataset.createTable(tableConfig, function(err, table, apiResponse) {});
103102
*/

lib/bigquery/job.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function Job(bigQuery, id) {
6262
parent: bigQuery,
6363
baseUrl: '/jobs',
6464
id: id,
65-
inherit: ['getMetadata']
65+
inherit: []
6666
});
6767

6868
this.bigQuery = bigQuery;

lib/common/service-object.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
'use strict';
2222

23+
var exec = require('methmeth');
2324
var is = require('is');
2425

2526
/**
@@ -61,12 +62,10 @@ function ServiceObject(config) {
6162
this.id = config.id; // Name or ID (e.g. dataset ID, bucket name, etc.)
6263
this.createMethod = config.createMethod;
6364

64-
if (config.inherit) {
65+
if (config.exclude) {
6566
var allMethods = Object.keys(ServiceObject.prototype);
66-
allMethods.splice(allMethods.indexOf('request'), 1);
67-
6867
allMethods.forEach(function(methodName) {
69-
if (config.inherit.indexOf(methodName) === -1) {
68+
if (config.exclude.indexOf(methodName) > -1) {
7069
self[methodName] = undefined;
7170
}
7271
});
@@ -120,10 +119,12 @@ ServiceObject.prototype.create = function(options, callback) {
120119
* @param {object} callback.apiResponse - The full API response.
121120
*/
122121
ServiceObject.prototype.delete = function(callback) {
123-
this.request({
122+
var reqOpts = {
124123
method: 'DELETE',
125124
uri: ''
126-
}, function(err, resp) {
125+
};
126+
127+
ServiceObject.prototype.request.call(this, reqOpts, function(err, resp) {
127128
callback(err, resp);
128129
});
129130
};
@@ -161,9 +162,11 @@ ServiceObject.prototype.get = function(callback) {
161162
ServiceObject.prototype.getMetadata = function(callback) {
162163
var self = this;
163164

164-
this.request({
165+
var reqOpts = {
165166
uri: ''
166-
}, function(err, resp) {
167+
};
168+
169+
ServiceObject.prototype.request.call(this, reqOpts, function(err, resp) {
167170
if (err) {
168171
callback(err, null, resp);
169172
return;
@@ -178,17 +181,18 @@ ServiceObject.prototype.getMetadata = function(callback) {
178181
/**
179182
* Get or create this object. If it doesn't exist, it will be created.
180183
*
184+
* @param {object=} options - Configuration object.
181185
* @param {function=} callback - The callback function.
182186
* @param {?error} callback.err - An error returned while making this request.
183187
* @param {object} callback.instance - The instance.
184188
* @param {object} callback.apiResponse - The full API response.
185189
*/
186-
ServiceObject.prototype.getOrCreate = function(callback) {
190+
ServiceObject.prototype.getOrCreate = function(options, callback) {
187191
var self = this;
188192

189193
this.get(function(err, instance, apiResponse) {
190194
if (err) {
191-
self.create(callback);
195+
self.create(options, callback);
192196
return;
193197
}
194198

@@ -210,11 +214,13 @@ ServiceObject.prototype.setMetadata = function(metadata, callback) {
210214

211215
callback = callback || util.noop;
212216

213-
this.request({
217+
var reqOpts = {
214218
method: 'PATCH',
215219
uri: '',
216220
json: metadata
217-
}, function(err, resp) {
221+
};
222+
223+
ServiceObject.prototype.request.call(this, reqOpts, function(err, resp) {
218224
if (err) {
219225
callback(err, resp);
220226
return;
@@ -242,9 +248,12 @@ ServiceObject.prototype.request = function(reqOpts, callback) {
242248
reqOpts.uri
243249
];
244250

245-
reqOpts.uri = uriComponents.map(function(uriComponent) {
246-
return uriComponent.replace(/^\/*|\/*$/g, '');
247-
}).join('/');
251+
reqOpts.uri = uriComponents
252+
.filter(exec('trim')) // Limit to non-empty strings.
253+
.map(function(uriComponent) {
254+
return uriComponent.replace(/^\/*|\/*$/g, '');
255+
})
256+
.join('/');
248257

249258
this.parent.request(reqOpts, callback);
250259
};

lib/common/service.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ function Service(config, options) {
4747
scopes: config.scopes,
4848
email: options.email
4949
});
50+
51+
this.authClient = this.makeAuthenticatedRequest.authClient;
5052
}
5153

5254
/**

lib/common/util.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,16 @@ util.noop = noop;
115115
var ApiError = createErrorClass('ApiError', function(errorBody) {
116116
this.errors = errorBody.errors;
117117
this.code = errorBody.code;
118-
this.message = errorBody.message || 'Error during request.';
118+
this.message = errorBody.message;
119119
this.response = errorBody.response;
120+
121+
if (!this.message) {
122+
if (errorBody.errors && errorBody.errors.length === 1) {
123+
this.message = errorBody.errors[0].message;
124+
} else {
125+
this.message = 'Error during request.';
126+
}
127+
}
120128
});
121129

122130
/**

lib/compute/address.js

Lines changed: 93 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020

2121
'use strict';
2222

23+
var nodeutil = require('util');
24+
25+
/**
26+
* @type {module:common/serviceObject}
27+
* @private
28+
*/
29+
var ServiceObject = require('../common/service-object.js');
30+
2331
/**
2432
* @type {module:common/util}
2533
* @private
@@ -54,11 +62,88 @@ var util = require('../common/util.js');
5462
* var address = region.address('address1');
5563
*/
5664
function Address(region, name) {
65+
ServiceObject.call(this, {
66+
parent: region,
67+
baseUrl: '/addresses',
68+
id: name,
69+
createMethod: region.createAddress.bind(region),
70+
exclude: ['setMetadata']
71+
});
72+
5773
this.region = region;
58-
this.name = name;
59-
this.metadata = {};
6074
}
6175

76+
nodeutil.inherits(Address, ServiceObject);
77+
78+
/**
79+
* Create an address.
80+
*
81+
* @resource [Instances and Networks]{@link https://cloud.google.com/compute/docs/instances-and-network}
82+
* @resource [Address Resource]{@link https://cloud.google.com/compute/docs/reference/v1/addresses}
83+
* @resource [Addresses: insert API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/addresses/insert}
84+
*
85+
* @param {object=} options - See an
86+
* [Address resource](https://cloud.google.com/compute/docs/reference/v1/addresses).
87+
* @param {function} callback - The callback function.
88+
* @param {?error} callback.err - An error returned while making this request.
89+
* @param {module:compute/address} callback.address - The created Address
90+
* object.
91+
* @param {module:compute/operation} callback.operation - An operation object
92+
* that can be used to check the status of the request.
93+
* @param {object} callback.apiResponse - The full API response.
94+
*
95+
* @example
96+
* address.create(function(err, address, operation, apiResponse) {
97+
* // `address` is an Address object.
98+
*
99+
* // `operation` is an Operation object that can be used to check the status
100+
* // of the request.
101+
* });
102+
*/
103+
Address.prototype.create = function() {
104+
ServiceObject.prototype.create.apply(this, arguments);
105+
};
106+
107+
/**
108+
* Get an address if it exists. Also see {module:compute/address#getOrCreate}.
109+
*
110+
* @resource [Address Resource]{@link https://cloud.google.com/compute/docs/reference/v1/addresses}
111+
*
112+
* @param {function} callback - The callback function.
113+
* @param {?error} callback.err - An error returned while making this request.
114+
* @param {module:compute/address} callback.address - The Address object.
115+
* @param {object} callback.apiResponse - The full API response.
116+
*
117+
* @example
118+
* address.get(function(err, address, apiResponse) {
119+
* // `address` is an Address object.
120+
* });
121+
*/
122+
Address.prototype.get = function() {
123+
ServiceObject.prototype.get.apply(this, arguments);
124+
};
125+
126+
/**
127+
* Get an address if it exists, otherwise create one.
128+
*
129+
* @resource [Instances and Networks]{@link https://cloud.google.com/compute/docs/instances-and-network}
130+
* @resource [Address Resource]{@link https://cloud.google.com/compute/docs/reference/v1/addresses}
131+
* @resource [Addresses: insert API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/addresses/insert}
132+
*
133+
* @param {function} callback - The callback function.
134+
* @param {?error} callback.err - An error returned while making this request.
135+
* @param {module:compute/address} callback.address - The Address object.
136+
* @param {object} callback.apiResponse - The full API response.
137+
*
138+
* @example
139+
* address.getOrCreate(function(err, address, apiResponse) {
140+
* // `address` is an Address object.
141+
* });
142+
*/
143+
Address.prototype.getOrCreate = function() {
144+
ServiceObject.prototype.getOrCreate.apply(this, arguments);
145+
};
146+
62147
/**
63148
* Delete the address.
64149
*
@@ -81,7 +166,10 @@ Address.prototype.delete = function(callback) {
81166

82167
var region = this.region;
83168

84-
this.makeReq_('DELETE', '', null, null, function(err, resp) {
169+
this.request({
170+
method: 'DELETE',
171+
uri: ''
172+
}, function(err, resp) {
85173
if (err) {
86174
callback(err, null, resp);
87175
return;
@@ -108,38 +196,8 @@ Address.prototype.delete = function(callback) {
108196
* @example
109197
* address.getMetadata(function(err, metadata, apiResponse) {});
110198
*/
111-
Address.prototype.getMetadata = function(callback) {
112-
callback = callback || util.noop;
113-
114-
var self = this;
115-
116-
this.makeReq_('GET', '', null, null, function(err, resp) {
117-
if (err) {
118-
callback(err, null, resp);
119-
return;
120-
}
121-
122-
self.metadata = resp;
123-
124-
callback(null, self.metadata, resp);
125-
});
126-
};
127-
128-
/**
129-
* Make a new request object from the provided arguments and wrap the callback
130-
* to intercept non-successful responses.
131-
*
132-
* @private
133-
*
134-
* @param {string} method - Action.
135-
* @param {string} path - Request path.
136-
* @param {*} query - Request query object.
137-
* @param {*} body - Request body contents.
138-
* @param {function} callback - The callback function.
139-
*/
140-
Address.prototype.makeReq_ = function(method, path, query, body, callback) {
141-
path = '/addresses/' + this.name + path;
142-
this.region.makeReq_(method, path, query, body, callback);
199+
Address.prototype.getMetadata = function() {
200+
ServiceObject.prototype.getMetadata.apply(this, arguments);
143201
};
144202

145203
module.exports = Address;

0 commit comments

Comments
 (0)