Permalink
Browse files

# adding snapshot support

  • Loading branch information...
1 parent 4b6f473 commit a77d56e7036cd7cd38bceb2cb63745284e197382 Juan Pablo Garcia Dalolla committed Aug 22, 2011
Showing with 143 additions and 24 deletions.
  1. +4 −3 History.md
  2. +1 −2 Readme.md
  3. +24 −12 examples/blobs.js
  4. +16 −1 lib/waz-blobs/blob.js
  5. +16 −1 lib/waz-blobs/service.js
  6. +22 −1 test/waz-blobs/blob.test.js
  7. +60 −4 test/waz-blobs/service.test.js
View
@@ -1,11 +1,12 @@
-0.1.1 / TDB
+0.1.1 / 2011-08-22
==================
* Added remaining Blob functionality:
- Retrieve and update blob metadata + properties
- Copy
- Delete
+ - Snapshots
* Service & CoreService refactoring
-0.1.0 / 2011-07-31
+0.1.0 / 2011-07-31
==================
-* Initial Release: Basic support for Blobs
+* Initial Release: Basic support for Blobs
View
@@ -109,8 +109,7 @@ To install via npm
* SharedAccessSignature
## Blobs
-* Snapshot
-* Update contents
+* Shared Access Signature
* Blocks
## Queues
View
@@ -55,6 +55,11 @@ waz.blobs.container.create('container1', function(err, container1){
console.log(err || newBlob);
});
+ blob.snapshot(function(err, blobWithSnapshot){
+ console.log('\n_________| creating a snapshot of a blob |_________\n');
+ console.log(err || blobWithSnapshot.snapshotDate);
+ });
+
blob.getContents(function(err,data){
console.log('\n_________| displaying blob contents |_________\n');
console.log(err || data);
@@ -75,21 +80,28 @@ waz.blobs.container.create('container1', function(err, container1){
blob.properties(function(err, properties){
console.log('\n_________| displaying blob properties |_________\n');
console.log(err || properties);
-
- blob.destroy(function(err){
- console.log('\n_________| destroying a blob |_________\n');
- console.log(err || 'blob `' + blob.path + '` removed!');
- waz.blobs.container.delete('container1', function(err){
- console.log('\n_________| removing a container |_________\n');
- console.log(err || '`container1` container removed!');
- });
+ blob.contentType = 'newContentType'
+ blob.metadata = {'x-ms-new-metadata': 'value'};
+ console.log(blob);
+ blob.updateContents('new-content', function(err, updatedBlob){
+ console.log('\n_________| updating blob contents |_________\n');
+ console.log(err || updatedBlob.requestId);
+ updatedBlob.destroy(function(err){
+ console.log('\n_________| destroying a blob |_________\n');
+ console.log(err || 'blob `' + updatedBlob.path + '` removed!');
+
+ waz.blobs.container.delete('container1', function(err){
+ console.log('\n_________| removing a container |_________\n');
+ console.log(err || '`container1` container removed!');
+ });
- waz.blobs.container.delete('container2', function(err){
- console.log('\n_________| removing a container |_________\n');
- console.log(err || '`container2` container removed!');
+ waz.blobs.container.delete('container2', function(err){
+ console.log('\n_________| removing a container |_________\n');
+ console.log(err || '`container2` container removed!');
+ });
});
- });
+ });
});
});
});
View
@@ -45,7 +45,7 @@ Blob.prototype.getContents = function(callback){
Blob.prototype.updateContents = function(value, callback){
var err = null;
- var blobInstance = this;
+ var blobInstance = Object.create(this);
if (!this.snapshotDate)
{
// TODO: replace metadata/properties methods by getMetadata / getProperties to have
@@ -81,4 +81,19 @@ Blob.prototype.copy = function(destination, callback){
callback(err, null)
}
});
+};
+
+Blob.prototype.snapshot = function(callback){
+ var blobInstance = Object.create(this);
+ serviceInstance.snapshotBlob(this.path, function(err, data) {
+ if (!err) {
+ blobInstance.snapshotDate = data['x-ms-snapshot'];
+ blobInstance.requestId = data['x-ms-request-id'];
+ blobInstance.etag = data['ETag'];
+ blobInstance.lastModified = data['Last-Modified'];
+ callback(err, blobInstance);
+ } else {
+ callback(err, null)
+ }
+ });
};
View
@@ -224,9 +224,24 @@ Service.prototype.copyBlob = function(source, destination, callback){
this.execute('put', destination, null, headers, null, function(err, response) {
var error = null, data = null
- if (err != null && err.statusCode == 400)
+ if (err != null && err.statusCode == 404)
error = { message: 'blob `' + source + '` not found' };
callback(error);
});
+}
+
+Service.prototype.snapshotBlob = function(path, callback){
+ var headers = {'x-ms-version': '2009-09-19'};
+
+ this.execute('put', path, {'comp': 'snapshot'}, headers, null, function(err, response) {
+ var error = null, data = null
+
+ if (err != null && err.statusCode == 404)
+ error = { message: 'blob `' + path + '` not found' };
+ else
+ data = response.headers;
+
+ callback(error, data);
+ });
}
@@ -200,5 +200,26 @@ module.exports = {
mock.verify();
},
-
+
+ 'should shapshot a given blob and fill corresponding blob properties': function(){
+ waz.establishConnection({ accountName : 'name', accountKey : 'key' });
+ var blobService = new Service({});
+ var mock = sinon.mock(blobService);
+
+ mock.expects("snapshotBlob").withArgs("containerName/blobName")
+ .yields(null, {'x-ms-snapshot' : 'mock-snapshotDate', 'x-ms-request-id' : 'mock-request-id', 'ETag': 'mock-etag', 'Last-Modified': 'mock-lastModified'})
+ .once();
+
+ var blob = new Blob({name: 'blobName', url: 'http://localhost/containerName/blobName', path: 'containerName/blobName', contentType: 'text/xml', serviceInstance: blobService});
+
+ blob.snapshot(function(err, blob){
+ assert.equal(blob.requestId, 'mock-request-id');
+ assert.equal(blob.etag, 'mock-etag');
+ assert.equal(blob.snapshotDate, 'mock-snapshotDate');
+ assert.equal(blob.lastModified, 'mock-lastModified');
+ assert.isNull(err);
+ });
+
+ mock.verify();
+ },
}
@@ -471,11 +471,11 @@ module.exports = {
var mock = sinon.mock(blobService);
var mockData = {body: '', headers: null};
- mock.expects("execute").withArgs('delete', 'mock-container/blob', null, {'x-ms-version': '2009-09-19'}, null)
+ mock.expects("execute").withArgs('delete', 'container/blob', null, {'x-ms-version': '2009-09-19'}, null)
.yields(null)
.once();
- var properties = blobService.deleteBlob('mock-container/blob', function(err){
+ var properties = blobService.deleteBlob('container/blob', function(err){
assert.isNull(err);
});
@@ -496,9 +496,30 @@ module.exports = {
assert.equal(err.message, 'blob `mock-container/unexisting` not found');
});
+ mock.verify();
+ },
+
+ 'should throw when trying to copy an unexisting blob': function(){
+ var blobService = new Service({});
+ var mock = sinon.mock(blobService);
+ var mockData = {body: '', headers: null};
+
+ mock.expects("canonicalizeMessage").withArgs('container/unexisting')
+ .returns('/account/container/unexisting')
+ .once();
+
+ mock.expects("execute").withArgs('put', 'container/destinationFile', null, {'x-ms-version': '2009-09-19', 'x-ms-copy-source': '/account/container/unexisting'}, null)
+ .yields({statusCode:404})
+ .once();
+
+ var properties = blobService.copyBlob('container/unexisting', 'container/destinationFile', function(err, data){
+ assert.isNotNull(err)
+ assert.equal(err.message, 'blob `container/unexisting` not found');
+ });
+
mock.verify();
},
-
+
'should copy a blob': function(){
var blobService = new Service({accountName: 'account'});
var mock = sinon.mock(blobService);
@@ -518,5 +539,40 @@ module.exports = {
mock.verify();
},
-
+
+ 'should create a snapshot of a given blob': function(){
+ var blobService = new Service({accountName: 'account'});
+ var mock = sinon.mock(blobService);
+ var mockData = {body: '', headers: null};
+
+ var expectedHeaders = { headers: {'x-ms-snapshot': 'mock-date' }};
+
+ mock.expects("execute").withArgs('put', 'container/blob', {comp: 'snapshot'}, {'x-ms-version': '2009-09-19'}, null)
+ .yields(null, expectedHeaders)
+ .once();
+
+ var properties = blobService.snapshotBlob('container/blob', function(err, data){
+ assert.isNull(err);
+ assert.equal(data, expectedHeaders.headers);
+ });
+
+ mock.verify();
+ },
+
+ 'should throw when trying to snapshot an unexisting blob': function(){
+ var blobService = new Service({});
+ var mock = sinon.mock(blobService);
+ var mockData = {body: '', headers: null};
+
+ mock.expects("execute").withArgs('put', 'container/unexisting', {comp: 'snapshot'}, {'x-ms-version': '2009-09-19'}, null)
+ .yields({statusCode:404}, null)
+ .once();
+
+ var properties = blobService.snapshotBlob('container/unexisting', function(err, data){
+ assert.isNotNull(err)
+ assert.equal(err.message, 'blob `container/unexisting` not found');
+ });
+
+ mock.verify();
+ },
}

0 comments on commit a77d56e

Please sign in to comment.