-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); | ||
}); |