Skip to content

Commit 873b88e

Browse files
compute: implement HealthChecks
1 parent e9e4170 commit 873b88e

File tree

6 files changed

+1232
-77
lines changed

6 files changed

+1232
-77
lines changed

lib/compute/firewall.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function Firewall(compute, name) {
135135
baseUrl: '/global/firewalls',
136136
id: name,
137137
createMethod: compute.createFirewall.bind(compute),
138-
methods: methods,
138+
methods: methods
139139
});
140140

141141
this.compute = compute;

lib/compute/health-check.js

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
/*!
2+
* Copyright 2016 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/healthCheck
19+
*/
20+
21+
'use strict';
22+
23+
var extend = require('extend');
24+
var is = require('is');
25+
var nodeutil = require('util');
26+
27+
/**
28+
* @type {module:common/serviceObject}
29+
* @private
30+
*/
31+
var ServiceObject = require('../common/service-object.js');
32+
33+
/**
34+
* @type {module:common/util}
35+
* @private
36+
*/
37+
var util = require('../common/util.js');
38+
39+
/*! Developer Documentation
40+
*
41+
* @param {module:compute} compute - Compute object this health check belongs
42+
* to.
43+
* @param {string} name - Name of the health check.
44+
* @param {object=} options - Optional configuration.
45+
* @param {boolean} options.https - Specify if this is an HTTPS health check
46+
* resource. Default: `false`
47+
*/
48+
/**
49+
* Health checks ensure that Compute Engine forwards new connections only to
50+
* instances that are up and ready to receive them. Compute Engine sends health
51+
* check requests to each instance at the specified frequency; once an instance
52+
* exceeds its allowed number of health check failures, it is no longer
53+
* considered an eligible instance for receiving new traffic.
54+
*
55+
* @resource [Health Checks Overview]{@link https://cloud.google.com/compute/docs/load-balancing/health-checks}
56+
*
57+
* @constructor
58+
* @alias module:compute/healthCheck
59+
*
60+
* @example
61+
* var gcloud = require('gcloud')({
62+
* keyFilename: '/path/to/keyfile.json',
63+
* projectId: 'grape-spaceship-123'
64+
* });
65+
*
66+
* var gce = gcloud.compute();
67+
*
68+
* var healthCheck = gce.healthCheck('health-check-name');
69+
*/
70+
function HealthCheck(compute, name, options) {
71+
var methods = {
72+
/**
73+
* Create an HTTP or HTTPS health check.
74+
*
75+
* @param {object} options - See {module:compute#createHealthCheck}.
76+
*
77+
* @example
78+
* healthCheck.create(function(err, healthCheck, operation, apiResponse) {
79+
* // `healthCheck` is a HealthCheck object.
80+
*
81+
* // `operation` is an Operation object that can be used to check the
82+
* // status of the request.
83+
* });
84+
*/
85+
create: true,
86+
87+
/**
88+
* Check if the health check 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 health check exists or
94+
* not.
95+
*
96+
* @example
97+
* healthCheck.exists(function(err, exists) {});
98+
*/
99+
exists: true,
100+
101+
/**
102+
* Get the health check if it exists.
103+
*
104+
* You may optionally use this to "get or create" an object by providing an
105+
* object with `autoCreate` set to `true`. Any extra configuration that is
106+
* normally required for the `create` method must be contained within this
107+
* object as well.
108+
*
109+
* @param {options=} options - Configuration object.
110+
* @param {boolean} options.autoCreate - Automatically create the object if
111+
* it does not exist. Default: `false`
112+
*
113+
* @example
114+
* healthCheck.get(function(err, healthCheck, apiResponse) {
115+
* // `healthCheck` is a HealthCheck object.
116+
* });
117+
*/
118+
get: true,
119+
120+
/**
121+
* Get the health check's metadata.
122+
*
123+
* @resource [HttpHealthChecks: get API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/httpHealthChecks/get}
124+
* @resource [HttpHealthCheck resource](https://cloud.google.com/compute/docs/reference/v1/httpHealthChecks#resource)
125+
* @resource [HttpsHealthChecks: get API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/httpsHealthChecks/get}
126+
* @resource [HttpsHealthCheck resource](https://cloud.google.com/compute/docs/reference/v1/httpsHealthChecks#resource)
127+
*
128+
* @param {function=} callback - The callback function.
129+
* @param {?error} callback.err - An error returned while making this
130+
* request.
131+
* @param {object} callback.metadata - The health check's metadata.
132+
* @param {object} callback.apiResponse - The full API response.
133+
*
134+
* @example
135+
* healthCheck.getMetadata(function(err, metadata, apiResponse) {});
136+
*/
137+
getMetadata: true
138+
};
139+
140+
options = options || {};
141+
142+
var https = options.https;
143+
this.compute = compute;
144+
145+
ServiceObject.call(this, {
146+
parent: compute,
147+
baseUrl: '/global/' + (https ? 'httpsHealthChecks' : 'httpHealthChecks'),
148+
id: name,
149+
createMethod: function(name, options, callback) {
150+
if (is.fn(options)) {
151+
callback = options;
152+
options = {};
153+
}
154+
155+
options = extend({}, options, { https: https });
156+
157+
compute.createHealthCheck(name, options, callback);
158+
},
159+
methods: methods
160+
});
161+
}
162+
163+
nodeutil.inherits(HealthCheck, ServiceObject);
164+
165+
/**
166+
* Delete the health check.
167+
*
168+
* @resource [HttpHealthCheck: delete API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/httpHealthChecks/delete}
169+
* @resource [HttpsHealthCheck: delete API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/httpsHealthChecks/delete}
170+
*
171+
* @param {function=} callback - The callback function.
172+
* @param {?error} callback.err - An error returned while making this request.
173+
* @param {module:compute/operation} callback.operation - An operation object
174+
* that can be used to check the status of the request.
175+
* @param {object} callback.apiResponse - The full API response.
176+
*
177+
* @example
178+
* healthCheck.delete(function(err, operation, apiResponse) {
179+
* // `operation` is an Operation object that can be used to check the status
180+
* // of the request.
181+
* });
182+
*/
183+
HealthCheck.prototype.delete = function(callback) {
184+
var compute = this.compute;
185+
186+
callback = callback || util.noop;
187+
188+
ServiceObject.prototype.delete.call(this, function(err, resp) {
189+
if (err) {
190+
callback(err, null, resp);
191+
return;
192+
}
193+
194+
var operation = compute.operation(resp.name);
195+
operation.metadata = resp;
196+
197+
callback(null, operation, resp);
198+
});
199+
};
200+
201+
/**
202+
* Set the health check's metadata.
203+
*
204+
* @resource [HttpHealthCheck: insert API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/httpHealthChecks/insert}
205+
* @resource [HttpsHealthCheck: insert API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/httpsHealthChecks/insert}
206+
*
207+
* @param {object} metadata - See a
208+
* [HttpHealthCheck resource](https://cloud.google.com/compute/docs/reference/v1/httpHealthChecks#resource)
209+
* and [HttpsHealthCheck resource](https://cloud.google.com/compute/docs/reference/v1/httpsHealthChecks#resource).
210+
* @param {function=} callback - The callback function.
211+
* @param {?error} callback.err - An error returned while making this request.
212+
* @param {module:compute/operation} callback.operation - An operation object
213+
* that can be used to check the status of the request.
214+
* @param {object} callback.apiResponse - The full API response.
215+
*
216+
* @example
217+
* var metadata = {
218+
* description: 'New description'
219+
* };
220+
*
221+
* healthCheck.setMetadata(metadata, function(err, operation, apiResponse) {
222+
* // `operation` is an Operation object that can be used to check the status
223+
* // of the request.
224+
* });
225+
*/
226+
HealthCheck.prototype.setMetadata = function(metadata, callback) {
227+
var compute = this.compute;
228+
229+
callback = callback || util.noop;
230+
231+
ServiceObject.prototype.setMetadata.call(this, metadata, function(err, resp) {
232+
if (err) {
233+
callback(err, null, resp);
234+
return;
235+
}
236+
237+
var operation = compute.operation(resp.name);
238+
operation.metadata = resp;
239+
240+
callback(null, operation, resp);
241+
});
242+
};
243+
244+
module.exports = HealthCheck;

0 commit comments

Comments
 (0)