|
| 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; |
0 commit comments