Skip to content

Commit eaefbca

Browse files
compute: implement autoscaler
1 parent 4a862bd commit eaefbca

File tree

8 files changed

+1519
-10
lines changed

8 files changed

+1519
-10
lines changed

docs/manifest.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@
9898
"title": "Address",
9999
"id": "address",
100100
"contents": "compute/address.json"
101+
}, {
102+
"title": "Autoscaler",
103+
"id": "autoscaler",
104+
"implemented": ">=0.29.0",
105+
"contents": "compute/autoscaler.json"
101106
}, {
102107
"title": "Disk",
103108
"id": "disk",

lib/compute/autoscaler.js

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
/*!
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*!
18+
* @module compute/autoscaler
19+
*/
20+
21+
'use strict';
22+
23+
var nodeutil = require('util');
24+
25+
/**
26+
* @type {module:common/serviceObject}
27+
* @private
28+
*/
29+
var ServiceObject = require('../common/service-object.js');
30+
31+
/**
32+
* @type {module:common/util}
33+
* @private
34+
*/
35+
var util = require('../common/util.js');
36+
37+
/*! Developer Documentation
38+
*
39+
* @param {module:zone} zone - Zone object this autoscaler belongs to.
40+
* @param {string} name - Name of the autoscaler.
41+
*/
42+
/**
43+
* Autoscalers allow you to automatically scale virtual machine instances in
44+
* managed instance groups according to an autoscaling policy that you define.
45+
*
46+
* @resource [Autoscaling Groups of Instances]{@link https://cloud.google.com/compute/docs/autoscaler}
47+
*
48+
* @constructor
49+
* @alias module:compute/autoscaler
50+
*
51+
* @example
52+
* var gcloud = require('gcloud')({
53+
* keyFilename: '/path/to/keyfile.json',
54+
* projectId: 'grape-spaceship-123'
55+
* });
56+
*
57+
* var gce = gcloud.compute();
58+
*
59+
* var zone = gce.zone('us-central1-a');
60+
*
61+
* var autoscaler = zone.autoscaler('autoscaler-name');
62+
*/
63+
function Autoscaler(zone, name) {
64+
var methods = {
65+
/**
66+
* Create an autoscaler.
67+
*
68+
* @param {object} config - See {module:compute/zone#createAutoscaler}.
69+
*
70+
* @example
71+
* autoscaler.create({
72+
* coolDown: 30,
73+
* cpu: 80,
74+
* loadBalance: 40,
75+
* maxReplicas: 5,
76+
* minReplicas: 0,
77+
* target: 'instance-group-1'
78+
* }, function(err, autoscaler, operation, apiResponse) {
79+
* // `autoscaler` is an Autoscaler object.
80+
*
81+
* // `operation` is an Operation object that can be used to check the
82+
* // of the request.
83+
* });
84+
*/
85+
create: true,
86+
87+
/**
88+
* Check if the autoscaler exists.
89+
*
90+
* @param {function} callback - The callback function.
91+
* @param {?error} callback.err - An error returned while making this
92+
* request.
93+
* @param {boolean} callback.exists - Whether the autoscaler exists or not.
94+
*
95+
* @example
96+
* autoscaler.exists(function(err, exists) {});
97+
*/
98+
exists: true,
99+
100+
/**
101+
* Get an autoscaler if it exists.
102+
*
103+
* You may optionally use this to "get or create" an object by providing an
104+
* object with `autoCreate` set to `true`. Any extra configuration that is
105+
* normally required for the `create` method must be contained within this
106+
* object as well.
107+
*
108+
* @param {options=} options - Configuration object.
109+
* @param {boolean} options.autoCreate - Automatically create the object if
110+
* it does not exist. Default: `false`
111+
*
112+
* @example
113+
* autoscaler.get(function(err, autoscaler, apiResponse) {
114+
* // `autoscaler` is an Autoscaler object.
115+
* });
116+
*/
117+
get: true,
118+
119+
/**
120+
* Get the metadata of this autoscaler.
121+
*
122+
* @resource [Autoscaler Resource]{@link https://cloud.google.com/compute/docs/reference/v1/autoscalers}
123+
* @resource [Autoscalers: get API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/autoscalers/get}
124+
*
125+
* @param {function=} callback - The callback function.
126+
* @param {?error} callback.err - An error returned while making this
127+
* request.
128+
* @param {object} callback.metadata - The autoscaler's metadata.
129+
* @param {object} callback.apiResponse - The full API response.
130+
*
131+
* @example
132+
* autoscaler.getMetadata(function(err, metadata, apiResponse) {});
133+
*/
134+
getMetadata: true
135+
};
136+
137+
ServiceObject.call(this, {
138+
parent: zone,
139+
baseUrl: '/autoscalers',
140+
id: name,
141+
createMethod: zone.createAutoscaler.bind(zone),
142+
methods: methods
143+
});
144+
145+
this.name = name;
146+
this.zone = zone;
147+
}
148+
149+
nodeutil.inherits(Autoscaler, ServiceObject);
150+
151+
/**
152+
* Delete the autoscaler.
153+
*
154+
* @resource [Autoscalers: delete API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/autoscalers/delete}
155+
*
156+
* @param {function=} callback - The callback function.
157+
* @param {?error} callback.err - An error returned while making this request.
158+
* @param {module:compute/operation} callback.operation - An operation object
159+
* that can be used to check the status of the request.
160+
* @param {object} callback.apiResponse - The full API response.
161+
*
162+
* @example
163+
* autoscaler.delete(function(err, operation, apiResponse) {
164+
* // `operation` is an Operation object that can be used to check the status
165+
* // of the request.
166+
* });
167+
*/
168+
Autoscaler.prototype.delete = function(callback) {
169+
callback = callback || util.noop;
170+
171+
var compute = this.zone.compute;
172+
173+
ServiceObject.prototype.delete.call(this, function(err, resp) {
174+
if (err) {
175+
callback(err, null, resp);
176+
return;
177+
}
178+
179+
var operation = compute.operation(resp.name);
180+
operation.metadata = resp;
181+
182+
callback(null, operation, resp);
183+
});
184+
};
185+
186+
/**
187+
* Set the autoscaler's metadata.
188+
*
189+
* @resource [Autoscaler Resource]{@link https://cloud.google.com/compute/docs/reference/v1/autoscalers}
190+
*
191+
* @param {object} metadata - See a
192+
* [Firewall resource](https://cloud.google.com/compute/docs/reference/v1/autoscalers).
193+
* @param {function=} callback - The callback function.
194+
* @param {?error} callback.err - An error returned while making this request.
195+
* @param {module:compute/operation} callback.operation - An operation object
196+
* that can be used to check the status of the request.
197+
* @param {object} callback.apiResponse - The full API response.
198+
*
199+
* @example
200+
* var metadata = {
201+
* description: 'New description'
202+
* };
203+
*
204+
* autoscaler.setMetadata(metadata, function(err, operation, apiResponse) {
205+
* // `operation` is an Operation object that can be used to check the status
206+
* // of the request.
207+
* });
208+
*/
209+
Autoscaler.prototype.setMetadata = function(metadata, callback) {
210+
var zone = this.zone;
211+
212+
callback = callback || util.noop;
213+
214+
metadata = metadata || {};
215+
metadata.name = this.name;
216+
metadata.zone = this.zone.name;
217+
218+
zone.request({
219+
method: 'PATCH',
220+
uri: '/autoscalers',
221+
qs: {
222+
autoscaler: this.name
223+
},
224+
json: metadata
225+
}, function(err, resp) {
226+
if (err) {
227+
callback(err, null, resp);
228+
return;
229+
}
230+
231+
var operation = zone.compute.operation(resp.name);
232+
operation.metadata = resp;
233+
234+
callback(null, operation, resp);
235+
});
236+
};
237+
238+
module.exports = Autoscaler;

lib/compute/index.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,126 @@ Compute.prototype.getAddresses = function(options, callback) {
438438
});
439439
};
440440

441+
/**
442+
* Get a list of autoscalers. For a detailed description of this method's
443+
* options, see [API reference](https://cloud.google.com/compute/docs/reference/v1/autoscalers/aggregatedList).
444+
*
445+
* @resource [Managing Autoscalers]{@link https://cloud.google.com/compute/docs/autoscaler/managing-autoscalers}
446+
* @resource [Understanding Autoscaler Decisions]{@link https://cloud.google.com/compute/docs/autoscaler/understanding-autoscaler-decisions}
447+
* @resource [Autoscalers: aggregatedList API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/autoscalers/aggregatedList}
448+
*
449+
* @param {object=} options - Address search options.
450+
* @param {boolean} options.autoPaginate - Have pagination handled
451+
* automatically. Default: true.
452+
* @param {string} options.filter - Search filter in the format of
453+
* `{name} {comparison} {filterString}`.
454+
* - **`name`**: the name of the field to compare
455+
* - **`comparison`**: the comparison operator, `eq` (equal) or `ne`
456+
* (not equal)
457+
* - **`filterString`**: the string to filter to. For string fields, this
458+
* can be a regular expression.
459+
* @param {number} options.maxResults - Maximum number of addresses to return.
460+
* @param {string} options.pageToken - A previously-returned page token
461+
* representing part of the larger set of results to view.
462+
* @param {function} callback - The callback function.
463+
* @param {?error} callback.err - An error returned while making this request.
464+
* @param {module:compute/autoscaler} callback.autoscalers - Autoscaler objects
465+
* from your project.
466+
* @param {?object} callback.nextQuery - If present, query with this object to
467+
* check for more results.
468+
* @param {object} callback.apiResponse - The full API response.
469+
*
470+
* @example
471+
* gce.getAutoscalers(function(err, autoscalers) {
472+
* // autoscalers is an array of `Autoscaler` objects.
473+
* });
474+
*
475+
* //-
476+
* // To control how many API requests are made and page through the results
477+
* // manually, set `autoPaginate` to `false`.
478+
* //-
479+
* function callback(err, autoscalers, nextQuery, apiResponse) {
480+
* if (nextQuery) {
481+
* // More results exist.
482+
* gce.getAutoscalers(nextQuery, callback);
483+
* }
484+
* }
485+
*
486+
* gce.getAutoscalers({
487+
* autoPaginate: false
488+
* }, callback);
489+
*
490+
* //-
491+
* // Get the autoscalers from your project as a readable object stream.
492+
* //-
493+
* gce.getAutoscalers()
494+
* .on('error', console.error)
495+
* .on('data', function(autoscaler) {
496+
* // `autoscaler` is an `Autoscaler` object.
497+
* })
498+
* .on('end', function() {
499+
* // All addresses retrieved.
500+
* });
501+
*
502+
* //-
503+
* // If you anticipate many results, you can end a stream early to prevent
504+
* // unnecessary processing and API requests.
505+
* //-
506+
* gce.getAutoscalers()
507+
* .on('data', function(address) {
508+
* this.end();
509+
* });
510+
*/
511+
Compute.prototype.getAutoscalers = function(options, callback) {
512+
var self = this;
513+
514+
if (is.fn(options)) {
515+
callback = options;
516+
options = {};
517+
}
518+
519+
options = options || {};
520+
521+
this.request({
522+
uri: '/aggregated/autoscalers',
523+
qs: options
524+
}, function(err, resp) {
525+
if (err) {
526+
callback(err, null, null, resp);
527+
return;
528+
}
529+
530+
var nextQuery = null;
531+
532+
if (resp.nextPageToken) {
533+
nextQuery = extend({}, options, {
534+
pageToken: resp.nextPageToken
535+
});
536+
}
537+
538+
var zones = resp.items || {};
539+
540+
var autoscalers = Object.keys(zones).reduce(function(acc, zoneName) {
541+
if (zoneName.indexOf('zones/') !== 0) {
542+
return acc;
543+
}
544+
545+
var zone = self.zone(zoneName.replace('zones/', ''));
546+
var zoneAutoscalers = zones[zoneName].autoscalers || [];
547+
548+
zoneAutoscalers.forEach(function(autoscaler) {
549+
var autoscalerInstance = zone.autoscaler(autoscaler.name);
550+
autoscalerInstance.metadata = autoscaler;
551+
acc.push(autoscalerInstance);
552+
});
553+
554+
return acc;
555+
}, []);
556+
557+
callback(null, autoscalers, nextQuery, resp);
558+
});
559+
};
560+
441561
/**
442562
* Get a list of disks.
443563
*
@@ -1372,6 +1492,7 @@ Compute.prototype.zone = function(name) {
13721492
*/
13731493
streamRouter.extend(Compute, [
13741494
'getAddresses',
1495+
'getAutoscalers',
13751496
'getDisks',
13761497
'getFirewalls',
13771498
'getNetworks',

0 commit comments

Comments
 (0)