Skip to content

Commit

Permalink
Add volume resize, reuse list utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
phillbaker committed Jul 19, 2016
1 parent c05a5d8 commit d144988
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 11 deletions.
37 changes: 26 additions & 11 deletions lib/digitalocean/volume.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
// perPage, optional
// callback, optional
Volume.prototype.list = function() {
var i,
params = 2 <= arguments.length ? slice.call(arguments, 0, i = arguments.length - 1) : (i = 0, []),
callback = arguments[i++];
var args = util.extractListArguments(arguments, 0);

return this.client.get.apply(this.client, ['/volumes', {}].concat(slice.call(params), [200, 'volumes', callback]));
return this.client.get.apply(this.client, ['/volumes', {}].concat(slice.call(args.params), [200, 'volumes', args.callback]));
};

// attributes, required
Expand Down Expand Up @@ -43,13 +41,9 @@
// perPage, optional
// callback, optional
Volume.prototype.listActions = function() {
var id = arguments[0],
i,
params = 2 <= arguments.length ? slice.call(arguments, 1, i = arguments.length - 1) : (i = 1, []),
callback = arguments[i++];

var url = util.safeUrl('volumes', id, 'actions');
return this.client.get.apply(this.client, [url, {}].concat(slice.call(params), [200, 'actions', callback]));
var args = util.extractListArguments(arguments, 1);
var url = util.safeUrl('volumes', args.identifier, 'actions');
return this.client.get.apply(this.client, [url, {}].concat(slice.call(args.params), [200, 'actions', args.callback]));
};

// id, rqeuired
Expand Down Expand Up @@ -100,6 +94,27 @@
return this.action(id, parameters, callback);
};

// id, required
// parametersOrSizeGibabytes, required keys: region slug, size_gigabytes
// region optional unless parameters provided
// callback, optional
Volume.prototype.resize = function(id, parametersOrSizeGibabytes, region, callback) {
var parameters;

if(typeof parametersOrSizeGibabytes !== 'object') {
parameters = {
size_gigabytes: parametersOrSizeGibabytes,
region: region
};
} else {
parameters = parametersOrSizeGibabytes;
callback = region;
}
parameters.type = 'resize';

return this.action(id, parameters, callback);
};

return Volume;
})();

Expand Down
64 changes: 64 additions & 0 deletions test/digitalocean/volume_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,68 @@ describe('volume endpoints', function() {
});
});
});

describe('resize', function() {
var data = {
"action": {
"id": 36804751,
"status": "in-progress",
"type": "resize_volume",
"started_at": "2014-11-14T16:31:07Z",
"completed_at": null,
"resource_id": null,
"resource_type": "volume",
"region": "nyc3",
"region_slug": "nyc3"
}
};

it('creates the action with a parameters hash', function() {
var parameters = {
region: 'nyc3',
size_gigabytes: 100
};

testUtils.api.post('/v2/volumes/123/actions',
{ type: 'resize', region: 'nyc3', size_gigabytes: 100 }
).reply(201, data);

client.volumes.resize(123, parameters, function(err, action, headers) {
expect(action).to.shallowDeepEqual(data.action);
});
});

it('creates the action with explicit arguments', function() {
testUtils.api.post('/v2/volumes/123/actions',
{ type: 'resize', region: 'nyc3', size_gigabytes: 100 }
).reply(201, data);

client.volumes.resize(123, 100, 'nyc3', function(err, action, headers) {
expect(action).to.shallowDeepEqual(data.action);
});
});

it('escapes the name', function() {
testUtils.api.post('/v2/volumes/foo%2Fbar/actions',
{ type: 'resize', region: 'nyc3', size_gigabytes: 100 }
).reply(201, data);

client.volumes.resize('foo/bar', 100, 'nyc3', function(err, action, headers) {
expect(action).to.shallowDeepEqual(data.action);
});
});

it('returns a promisable', function(done) {
testUtils.api.post('/v2/volumes/123/actions',
{ type: 'resize', region: 'nyc3', size_gigabytes: 100 }
).reply(201, data);

client.volumes.resize(123, 100, 'nyc3').then(function(action) {
expect(action).to.shallowDeepEqual(data.action);
done();
}).catch(function(err) {
done(err);
});
});
});
});

0 comments on commit d144988

Please sign in to comment.