Skip to content

Commit

Permalink
Add unified snapshot endpoints.
Browse files Browse the repository at this point in the history
Closes #9.
  • Loading branch information
phillbaker committed Jan 14, 2017
1 parent 65f061a commit 62a27b3
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/digitalocean/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Image = require('./image'),
Region = require('./region'),
Size = require('./size'),
Snapshot = require('./snapshot'),
Tag = require('./tag');

// Utilities
Expand Down Expand Up @@ -75,6 +76,7 @@
this.droplets = new Droplet(this);
this.regions = new Region(this);
this.sizes = new Size(this);
this.snapshots = new Snapshot(this);
this.tags = new Tag(this);
}

Expand Down
55 changes: 55 additions & 0 deletions lib/digitalocean/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
(function() {
var slice = [].slice,
util = require('./util');

/**
* Snapshot resource
* @class Snapshot
*/
var Snapshot = (function() {
function Snapshot(client) {
this.client = client;
}

/**
* List snapshot objects.
*
* @param {(number|object)} [page or queryObject] - page number to retrieve or key value pairs of query parameters
* @param {number} [perPage] - number of result per page to retrieve
* @param {requestCallback} [callback] - callback that handles the response
* @memberof Snapshot
*/
Snapshot.prototype.list = function() {
var args = util.extractListArguments(arguments, 0);
return this.client.get.apply(this.client, ['/snapshots', {}].concat(slice.call(args.params), [200, 'snapshots', args.callback]));
};

/**
* Get the identified snapshot object.
*
* @param {string} id - The id of the snapshot to retrieve
* @param {requestCallback} [callback] - callback that handles the response
* @memberof Snapshot
*/
Snapshot.prototype.get = function(id, callback) {
var url = util.safeUrl('snapshots', id);
return this.client.get(url, {}, 200, 'snapshot', callback);
};

/**
* Delete the identified snapshot object.
*
* @param {string} id - The id of the snapshot to delete
* @param {requestCallback} [callback] - callback that handles the response
* @memberof Snapshot
*/
Snapshot.prototype.delete = function(id, callback) {
var url = util.safeUrl('snapshots', id);
return this.client.delete(url, {}, 204, [], callback);
};

return Snapshot;
})();

module.exports = Snapshot;
}).call(this);
155 changes: 155 additions & 0 deletions test/digitalocean/snapshot_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
'use strict';

var expect = require('chai').expect;

var testUtils = require('../testUtils');

var digitalocean = require('../../lib/digitalocean');
var token = testUtils.getUserDigitalOceanToken();
var client = digitalocean.client(token);

describe('snapshot endpoints', function() {
describe('list', function() {
var data = {
"snapshots": [
{
"id": "119192817",
"name": "Ubuntu 13.04",
"regions": [
"nyc1"
],
"created_at": "2014-07-29T14:35:40Z",
"type": "snapshot"
},
{
"id": "449676376",
"name": "Ubuntu 13.04",
"regions": [
"nyc1"
],
"created_at": "2014-07-29T14:35:40Z",
"type": "snapshot"
}
],
"meta": {
"total": 2
}
};

it('returns snapshots', function() {
testUtils.api.get('/v2/snapshots').reply(200, JSON.stringify(data));

client.snapshots.list(function(err, snapshots, headers) {
expect(snapshots).to.shallowDeepEqual(data.snapshots);
});
});

it('returns snapshots at page', function() {
testUtils.api.get('/v2/snapshots?page=2').reply(200, JSON.stringify(data));

client.snapshots.list(2, function(err, snapshots, headers) {
expect(snapshots).to.shallowDeepEqual(data.snapshots);
});
});

it('returns snapshots at page with length', function() {
testUtils.api.get('/v2/snapshots?page=2&per_page=1').reply(200, JSON.stringify(data));

client.snapshots.list(2, 1, function(err, snapshots, headers) {
expect(snapshots).to.shallowDeepEqual(data.snapshots);
});
});

it('returns a promisable', function(done) {
testUtils.api.get('/v2/snapshots').reply(200, JSON.stringify(data));

client.snapshots.list().then(function(snapshots) {
expect(snapshots).to.shallowDeepEqual(data.snapshots);
done();
}).catch(function(err) {
done(err);
});
});

it('returns a promisable with a query object', function(done) {
testUtils.api.get('/v2/snapshots?page=2&per_page=1').reply(200, JSON.stringify(data));

client.snapshots.list({ page: 2, per_page: 1 }).then(function(snapshots) {
expect(snapshots).to.shallowDeepEqual(data.snapshots);
done();
}).catch(function(err) {
done(err);
});
});
});

describe('get', function() {
var data = {
"snapshot": {
"id": "146",
"name": "Ubuntu 13.04",
"regions": [
"region--1"
],
"created_at": "2014-07-29T14:35:41Z",
"type": "snapshot"
}
};

it('returns the snapshot', function() {
testUtils.api.get('/v2/snapshots/146').reply(200, JSON.stringify(data));

client.snapshots.get(146, function(err, snapshot, headers) {
expect(snapshot).to.shallowDeepEqual(data.snapshot);
});
});

it('escapes the name', function() {
testUtils.api.get('/v2/snapshots/foo%2Fbar').reply(200, JSON.stringify(data));

client.snapshots.get('foo/bar', function(err, snapshot, headers) {
expect(snapshot).to.shallowDeepEqual(data.snapshot);
});
});

it('returns a promisable', function(done) {
testUtils.api.get('/v2/snapshots/146').reply(200, JSON.stringify(data));

client.snapshots.get(146).then(function(snapshot) {
expect(snapshot).to.shallowDeepEqual(data.snapshot);
done();
}).catch(function(err) {
done(err);
});
});
});

describe('delete', function() {
it('deletes the snapshot', function() {
testUtils.api.delete('/v2/snapshots/123').reply(204, '');

client.snapshots.delete(123, function(err) {
expect(err).to.be.null;
});
});

it('escapes the name', function() {
testUtils.api.delete('/v2/snapshots/foo%2Fbar').reply(204, '');

client.snapshots.delete('foo/bar', function(err) {
expect(err).to.be.null;
});
});

it('returns a promisable', function(done) {
testUtils.api.delete('/v2/snapshots/123').reply(204, '');

client.snapshots.delete(123).then(function(snapshot) {
expect(snapshot.id).to.be.undefined;
done();
}).catch(function(err) {
done(err);
});
});
});
});

0 comments on commit 62a27b3

Please sign in to comment.