Skip to content
Merged
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
140 changes: 89 additions & 51 deletions lib/compute/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@

'use strict';

var nodeutil = require('util');

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

/**
* @type {module:common/util}
* @private
Expand Down Expand Up @@ -54,11 +62,86 @@ var util = require('../common/util.js');
* var address = region.address('address1');
*/
function Address(region, name) {
this.region = region;
var methods = {
/**
* Create an address.
*
* @param {object=} options - See {module:compute#createAddress}.
*
* @example
* address.create(function(err, address, operation, apiResponse) {
* // `address` is an Address object.
*
* // `operation` is an Operation object that can be used to check the
* // of the request.
* });
*/
create: true,

/**
* Check if the address exists.
*
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this
* request.
* @param {boolean} callback.exists - Whether the address exists or not.
*
* @example
* address.exists(function(err, exists) {});
*/
exists: true,

/**
* Get an address if it exists.
*
* You may optionally use this to "get or create" an object by providing an
* object with `autoCreate` set to `true`. Any extra configuration that is
* normally required for the `create` method must be contained within this
* object as well.
*
* @param {options=} options - Configuration object.
* @param {boolean} options.autoCreate - Automatically create the object if
* it does not exist. Default: `false`
*
* @example
* address.get(function(err, address, apiResponse) {
* // `address` is an Address object.
* });
*/
get: true,

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

ServiceObject.call(this, {
parent: region,
baseUrl: '/addresses',
id: name,
createMethod: region.createAddress.bind(region),
methods: methods
});

this.name = name;
this.metadata = {};
this.region = region;
}

nodeutil.inherits(Address, ServiceObject);

/**
* Delete the address.
*
Expand All @@ -81,7 +164,10 @@ Address.prototype.delete = function(callback) {

var region = this.region;

this.makeReq_('DELETE', '', null, null, function(err, resp) {
this.request({
method: 'DELETE',
uri: ''
}, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
Expand All @@ -94,52 +180,4 @@ Address.prototype.delete = function(callback) {
});
};

/**
* Get the metadata of this address.
*
* @resource [Address Resource]{@link https://cloud.google.com/compute/docs/reference/v1/addresses}
* @resource [Addresses: get API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/addresses/get}
*
* @param {function=} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request
* @param {object} callback.metadata - The address's metadata.
* @param {object} callback.apiResponse - The full API response.
*
* @example
* address.getMetadata(function(err, metadata, apiResponse) {});
*/
Address.prototype.getMetadata = function(callback) {
callback = callback || util.noop;

var self = this;

this.makeReq_('GET', '', null, null, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}

self.metadata = resp;

callback(null, self.metadata, resp);
});
};

/**
* Make a new request object from the provided arguments and wrap the callback
* to intercept non-successful responses.
*
* @private
*
* @param {string} method - Action.
* @param {string} path - Request path.
* @param {*} query - Request query object.
* @param {*} body - Request body contents.
* @param {function} callback - The callback function.
*/
Address.prototype.makeReq_ = function(method, path, query, body, callback) {
path = '/addresses/' + this.name + path;
this.region.makeReq_(method, path, query, body, callback);
};

module.exports = Address;
160 changes: 111 additions & 49 deletions lib/compute/disk.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@
var extend = require('extend');
var format = require('string-format-obj');
var is = require('is');
var nodeutil = require('util');

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

/**
* @type {module:compute/snapshot}
* @private
*/
var Snapshot = require('./snapshot.js');

/**
* @type {module:common/util}
Expand Down Expand Up @@ -57,13 +70,92 @@ var util = require('../common/util.js');
* var disk = zone.disk('disk1');
*/
function Disk(zone, name) {
this.zone = zone;
var methods = {
/**
* Create a persistent disk.
*
* @param {object} config - See {module:compute/zone#createDisk}.
*
* @example
* var config = {
* // ...

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

* };
*
* disk.create(config, function(err, disk, operation, apiResponse) {
* // `disk` is a Disk object.
*
* // `operation` is an Operation object that can be used to check the
* // status of the request.
* });
*/
create: true,

/**
* Check if the disk exists.
*
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this
* request.
* @param {boolean} callback.exists - Whether the disk exists or not.
*
* @example
* disk.exists(function(err, exists) {});
*/
exists: true,

/**
* Get a disk if it exists.
*
* You may optionally use this to "get or create" an object by providing an
* object with `autoCreate` set to `true`. Any extra configuration that is
* normally required for the `create` method must be contained within this
* object as well.
*
* @param {options=} options - Configuration object.
* @param {boolean} options.autoCreate - Automatically create the object if
* it does not exist. Default: `false`
*
* @example
* disk.get(function(err, disk, apiResponse) {
* // `disk` is a Disk object.
* });
*/
get: true,

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

ServiceObject.call(this, {
parent: zone,
baseUrl: '/disks',
id: name,
createMethod: zone.createDisk.bind(zone),
methods: methods
});

this.name = name;
this.metadata = {};
this.zone = zone;

this.formattedName = Disk.formatName_(zone, name);
}

nodeutil.inherits(Disk, ServiceObject);

/**
* Format a disk's name how the API expects.
*
Expand Down Expand Up @@ -110,24 +202,27 @@ Disk.formatName_ = function(zone, name) {
* disk.createSnapshot('new-snapshot-name', callback);
*/
Disk.prototype.createSnapshot = function(name, options, callback) {
var self = this;
var zone = this.zone;

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

var body = extend({}, options, {
name: name
});

this.makeReq_('POST', '/createSnapshot', null, body, function(err, resp) {
this.request({
method: 'POST',
uri: '/createSnapshot',
json: extend({}, options, {
name: name
})
}, function(err, resp) {
if (err) {
callback(err, null, null, resp);
return;
}

var snapshot = zone.compute.snapshot(name);
var snapshot = self.snapshot(name);

var operation = zone.operation(resp.name);
operation.metadata = resp;
Expand Down Expand Up @@ -158,7 +253,7 @@ Disk.prototype.delete = function(callback) {

callback = callback || util.noop;

this.makeReq_('DELETE', '', null, null, function(err, resp) {
ServiceObject.prototype.delete.call(this, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
Expand All @@ -172,51 +267,18 @@ Disk.prototype.delete = function(callback) {
};

/**
* Get the disk's metadata.
* Get a reference to a snapshot from this disk.
*
* @resource [Disk Resource]{@link https://cloud.google.com/compute/docs/reference/v1/disks}
* @resource [Disks: get API Documentation]{@link https://cloud.google.com/compute/docs/reference/v1/disks/get}
* @resource [Snapshots Overview]{@link https://cloud.google.com/compute/docs/disks/persistent-disks#snapshots}
*
* @param {function=} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request
* @param {object} callback.metadata - The disk's metadata.
* @param {object} callback.apiResponse - The full API response.
* @param {string} name - Name of the snapshot.
* @return {module:compute/snapshot}
*
* @example
* disk.getMetadata(function(err, metadata, apiResponse) {});
*/
Disk.prototype.getMetadata = function(callback) {
var self = this;

callback = callback || util.noop;

this.makeReq_('GET', '', null, null, function(err, resp) {
if (err) {
callback(err, null, resp);
return;
}

self.metadata = resp;

callback(null, self.metadata, resp);
});
};

/**
* Make a new request object from the provided arguments and wrap the callback
* to intercept non-successful responses.
*
* @private
*
* @param {string} method - Action.
* @param {string} path - Request path.
* @param {*} query - Request query object.
* @param {*} body - Request body contents.
* @param {function} callback - The callback function.
* var snapshot = disk.snapshot('snapshot-name');
*/
Disk.prototype.makeReq_ = function(method, path, query, body, callback) {
path = '/disks/' + this.name + path;
this.zone.makeReq_(method, path, query, body, callback);
Disk.prototype.snapshot = function(name) {
return new Snapshot(this, name);
};

module.exports = Disk;
Loading