Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compute: implement machineType #1441

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/toc.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
}, {
"title": "InstanceGroup",
"type": "compute/instance-group"
}, {
"title": "MachineType",
"type": "compute/machine-type"
}, {
"title": "Network",
"type": "compute/network"
Expand Down
118 changes: 118 additions & 0 deletions lib/compute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,123 @@ Compute.prototype.getHealthChecks = function(options, callback) {
});
};

/**
* Get a list of machine types in this project.
*
* @resource [MachineTypes: list API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/machineTypes/aggregatedList}
* @resource [Machine Types Overview]{@link https://cloud.google.com/compute/docs/machine-types}
* @resource [MachineType Resource]{@link https://cloud.google.com/compute/docs/reference/v1/machineTypes}
*
* @param {object=} options - Machine type search options.
* @param {boolean} options.autoPaginate - Have pagination handled
* automatically. Default: true.
* @param {string} options.filter - Search filter in the format of
* `{name} {comparison} {filterString}`.
* - **`name`**: the name of the field to compare
* - **`comparison`**: the comparison operator, `eq` (equal) or `ne`
* (not equal)
* - **`filterString`**: the string to filter to. For string fields, this
* can be a regular expression.
* @param {number} options.maxApiCalls - Maximum number of API calls to make.
* @param {number} options.maxResults - Maximum number of machineTypes to
* return.
* @param {string} options.pageToken - A previously-returned page token
* representing part of the larger set of results to view.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request.
* @param {module:compute/machine-type[]} callback.machineTypes - MachineType
* objects from your project.
* @param {?object} callback.nextQuery - If present, query with this object to
* check for more results.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* gce.getMachineTypes(function(err, machineTypes) {
* // `machineTypes` is an array of `MachineType` objects.
* });
*
* //-
* // To control how many API requests are made and page through the results
* // manually, set `autoPaginate` to `false`.
* //-
* function callback(err, machineTypes, nextQuery, apiResponse) {
* if (nextQuery) {
* // More results exist.
* gce.getMachineTypes(nextQuery, callback);
* }
* }
*
* gce.getMachineTypes({
* autoPaginate: false
* }, callback);
*
* //-
* // Get the machine types from your project as a readable object stream.
* //-
* gce.getMachineTypes()
* .on('error', console.error)
* .on('data', function(machineType) {
* // `machineType` is a `MachineType` object.
* })
* .on('end', function() {
* // All machine types retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* //-
* gce.getMachineTypes()
* .on('data', function(machineType) {
* this.end();
* });
*/
Compute.prototype.getMachineTypes = function(options, callback) {
var self = this;

if (is.fn(options)) {
callback = options;
options = {};
}

options = options || {};

this.request({
uri: '/aggregated/machineTypes',
qs: options
}, function(err, resp) {
if (err) {
callback(err, null, null, resp);
return;
}

var nextQuery = null;

if (resp.nextPageToken) {
nextQuery = extend({}, options, {
pageToken: resp.nextPageToken
});
}

var zones = resp.items || {};

var machineTypes = Object.keys(zones).reduce(function(acc, zoneName) {
var zone = self.zone(zoneName.replace('zones/', ''));
var machineTypesByZone = zones[zoneName].machineTypes || [];

machineTypesByZone.forEach(function(machineType) {
var machineTypeInstance = zone.machineType(machineType.name);
machineTypeInstance.metadata = machineType;
acc.push(machineTypeInstance);
});

return acc;
}, []);

callback(null, machineTypes, nextQuery, resp);
});
};

/**
* Get a list of networks.
*
Expand Down Expand Up @@ -2266,6 +2383,7 @@ streamRouter.extend(Compute, [
'getFirewalls',
'getHealthChecks',
'getInstanceGroups',
'getMachineTypes',
'getNetworks',
'getOperations',
'getRegions',
Expand Down
115 changes: 115 additions & 0 deletions lib/compute/machine-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*!
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*!
* @module compute/machine-type
*/

'use strict';

var nodeutil = require('util');

/**
* @type {module:common/service-object}
* @private
*/
var ServiceObject = require('../common/service-object.js');

/*! Developer Documentation
*
* @param {module:zone} zone - Zone object this machine type belongs to.
* @param {string} name - Name of the machine type.
*/
/**
* A MachineType object allows you to interact with a Google Compute Engine
* machine type.
*
* @resource [Machine Types Overview]{@link https://cloud.google.com/compute/docs/machine-types}
* @resource [MachineType Resource]{@link https://cloud.google.com/compute/docs/reference/v1/machineTypes}
*
* @constructor
* @alias module:compute/machine-type
*
* @example
* var gcloud = require('gcloud')({
* keyFilename: '/path/to/keyfile.json',
* projectId: 'grape-spaceship-123'
* });
*
* var gce = gcloud.compute();
*
* var zone = gce.zone('us-central1-b');
*
* var machineType = zone.machineType('g1-small');
*/
function MachineType(zone, name) {
var methods = {
/**
* Check if the machine type exists.
*
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this
* request.
* @param {boolean} callback.exists - Whether the machine type exists or
* not.
*
* @example
* machineType.exists(function(err, exists) {});
*/
exists: true,

/**
* Get a machine type if it exists.
*
* @example
* machineType.get(function(err, machineType, apiResponse) {
* // `machineType` is a MachineType object.
* });
*/
get: true,

/**
* Get the machine type's metadata.
*
* @resource [MachineTypes: get API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/machineTypes/get}
* @resource [MachineType Resource]{@link https://cloud.google.com/compute/docs/reference/v1/machineTypes}
*
* @param {function=} callback - The callback function.
* @param {?error} callback.err - An error returned while making this
* request.
* @param {object} callback.metadata - The machine type's metadata.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* machineType.getMetadata(function(err, metadata, apiResponse) {});
*/
getMetadata: true
};

ServiceObject.call(this, {
parent: zone,
baseUrl: '/machineTypes',
id: name,
methods: methods
});

this.zone = zone;
this.name = name;
}

nodeutil.inherits(MachineType, ServiceObject);

module.exports = MachineType;
100 changes: 100 additions & 0 deletions lib/compute/zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ var Disk = require('./disk.js');
*/
var InstanceGroup = require('./instance-group.js');

/**
* @type {module:compute/machine-type}
* @private
*/
var MachineType = require('./machine-type.js');

/**
* @type {module:compute/operation}
* @private
Expand Down Expand Up @@ -974,6 +980,83 @@ Zone.prototype.getInstanceGroups = function(options, callback) {
});
};

/**
* Get a list of machine types for this zone.
*
* @resource [MachineTypes: list API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/machineTypes/list}
* @resource [Machine Types Overview]{@link https://cloud.google.com/compute/docs/machine-types}
* @resource [MachineType Resource]{@link https://cloud.google.com/compute/docs/reference/v1/machineTypes}
*
* @param {object=} options - Machine type search options.
* @param {boolean} options.autoPaginate - Have pagination handled
* automatically. Default: true.
* @param {number} options.maxApiCalls - Maximum number of API calls to make.
* @param {number} options.maxResults - Maximum number of machineTypes
* to return.
* @param {string} options.pageToken - A previously-returned page token
* representing part of the larger set of results to view.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request.
* @param {module:compute/machine-type[]} callback.machineTypes - MachineType
* objects from this zone.
* @param {?object} callback.nextQuery - If present, query with this object to
* check for more results.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* zone.getMachineTypes(function(err, machineTypes) {
* // `machineTypes` is an array of `MachineType` objects.
* });
*
* //-
* // To control how many API requests are made and page through the results
* // manually, set `autoPaginate` to `false`.
* //-
* function callback(err, machineTypes, nextQuery, apiResponse) {
* if (nextQuery) {
* // More results exist.
* zone.getMachineTypes(nextQuery, callback);
* }
* }
*
* zone.getMachineTypes({
* autoPaginate: false
* }, callback);
*
* //-
* // Get the machine types from this zone as a readable object stream.
* //-
* zone.getMachineTypes()
* .on('error', console.error)
* .on('data', function(machineType) {
* // `machineType` is a `MachineType` object.
* })
* .on('end', function() {
* // All machine types retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* //-
* zone.getMachineTypes()
* .on('data', function(machineType) {
* this.end();
* });
*/
Zone.prototype.getMachineTypes = function(options, callback) {
if (is.fn(options)) {
callback = options;
options = {};
}

options = extend({}, options, {
filter: 'zone eq .*' + this.name
});

return this.compute.getMachineTypes(options, callback);
};

/**
* Get a list of operations for this zone.
*
Expand Down Expand Up @@ -1198,6 +1281,22 @@ Zone.prototype.instanceGroup = function(name) {
return new InstanceGroup(this, name);
};

/**
* Get a reference to a Google Compute Engine machine type.
*
* @resource [Machine Types Overview]{@link https://cloud.google.com/compute/docs/machine-types}
* @resource [MachineType Resource]{@link https://cloud.google.com/compute/docs/reference/v1/machineTypes}
*
* @param {string} name - Name of the existing machine type.
* @return {module:compute/machine-type}
*
* @example
* var machienType = zone.machineType('g1-small');
*/
Zone.prototype.machineType = function(name) {
return new MachineType(this, name);
};

/**
* Get a reference to a Google Compute Engine zone operation.
*
Expand Down Expand Up @@ -1281,6 +1380,7 @@ streamRouter.extend(Zone, [
'getAutoscalers',
'getDisks',
'getInstanceGroups',
'getMachineTypes',
'getOperations',
'getVMs'
]);
Expand Down
Loading