Permalink
Browse files

[api test] Added createContainer functionality

  • Loading branch information...
indexzero committed Sep 23, 2010
1 parent 71df419 commit 22c29f80578a4523285e5cf326ed769f7fcf78de
Showing with 71 additions and 12 deletions.
  1. +8 −7 lib/cloudfiles.js
  2. +6 −2 lib/cloudfiles/container.js
  3. +25 −3 lib/cloudfiles/core.js
  4. +24 −0 test/container-test.js
  5. +8 −0 test/helpers.js
View
@@ -11,14 +11,15 @@ require.paths.unshift(__dirname);
var cloudfiles = exports;
// Core
-cloudfiles.config = require('cloudfiles/config').config;
-cloudfiles.setAuth = require('cloudfiles/core').setAuth;
+cloudfiles.config = require('cloudfiles/config').config;
+cloudfiles.setAuth = require('cloudfiles/core').setAuth;
// Containers
-cloudfiles.Container = require('cloudfiles/container').Container;
-cloudfiles.getContainers = require('cloudfiles/core').getContainers;
-cloudfiles.getContainer = require('cloudfiles/core').getContainer;
+cloudfiles.Container = require('cloudfiles/container').Container;
+cloudfiles.createContainer = require('cloudfiles/core').createContainer;
+cloudfiles.getContainers = require('cloudfiles/core').getContainers;
+cloudfiles.getContainer = require('cloudfiles/core').getContainer;
// Storage Object
-cloudfiles.StorageObject = require('cloudfiles/storage-object').StorageObject;
-cloudfiles.getFiles = require('cloudfiles/core').getFiles;
+cloudfiles.StorageObject = require('cloudfiles/storage-object').StorageObject;
+cloudfiles.getFiles = require('cloudfiles/core').getFiles;
@@ -29,8 +29,12 @@ Container.prototype = {
_setProperties: function (details) {
this.name = details.name;
- this.count = details.count;
- this.bytes = details.bytes;
+ this.cdnEnabled = details.cdnEnabled || false;
+ this.cdnUri = details.cdnUri;
+ this.ttl = details.ttl;
+ this.logRetention = details.logRetention;
+ this.count = details.count || 0;
+ this.bytes = details.bytes || 0;
}
};
View
@@ -52,7 +52,7 @@ core.setAuth = function (options, callback) {
// Remark: What about CDN Containers?
core.getContainers = function (callback) {
- utils.rackspace(utils.storageUrl(), callback, function (body) {
+ utils.rackspace(utils.storageUrl(true), callback, function (body) {
var results = [], containers = JSON.parse(body);
containers.forEach(function (container) {
results.push(new (cloudfiles.Container)(container));
@@ -66,7 +66,7 @@ core.getContainers = function (callback) {
core.getContainer = function (container, callback) {
utils.rackspace('HEAD', utils.storageUrl(container), callback, function (body, res) {
var container = {
- name: name,
+ name: container,
count: new Number(res.headers['x-container-object-count']),
bytes: new Number(res.headers['x-container-bytes-used'])
};
@@ -75,7 +75,29 @@ core.getContainer = function (container, callback) {
};
core.createContainer = function (container, callback) {
-
+ utils.rackspace('PUT', utils.storageUrl(container.name), callback, function (body, res) {
+ if (typeof container.cdnEnabled !== 'undefined' && container.cdnEnabled) {
+ container.ttl = container.ttl || cloudfiles.config.cdn.ttl;
+ container.logRetention = container.logRetention || cloudfiles.config.cdn.logRetention;
+
+ var cdnOptions = {
+ uri: utils.cdnUrl(container.name),
+ method: 'PUT',
+ headers: {
+ 'X-TTL': container.ttl,
+ 'X-LOG-RETENTION': container.logRetention
+ }
+ };
+
+ utils.rackspace(cdnOptions, callback, function (body, res) {
+ container.cdnUri = res.headers['x-cdn-uri'];
+ callback(null, new (cloudfiles.Container)(container));
+ });
+ }
+ else {
+ callback(null, new (cloudfiles.Container)(container));
+ }
+ });
};
core.getFiles = function (container, callback) {
View
@@ -28,6 +28,30 @@ vows.describe('node-cloudfiles/containers').addBatch({
}
}
}
+}).addBatch({
+ "The node-cloudfiles client": {
+ "the createContainer() method": {
+ "when creating a container": {
+ topic: function () {
+ cloudfiles.createContainer({ name: 'test_container' }, this.callback);
+ },
+ "should return a valid container": function (err, container) {
+ helpers.assertContainer(container);
+ }
+ },
+ "when creating a CDN-enabled container": {
+ topic: function () {
+ cloudfiles.createContainer({
+ name: 'test_cdn_container',
+ cdnEnabled: true
+ }, this.callback);
+ },
+ "should return a valid container": function (err, container) {
+ helpers.assertCdnContainer(container);
+ }
+ }
+ }
+ }
}).addBatch({
"The node-cloudfiles client": {
"the getContainers() method": {
View
@@ -23,6 +23,14 @@ helpers.assertContainer = function (container) {
assert.isNotNull(container.bytes);
};
+helpers.assertCdnContainer = function (container) {
+ helpers.assertContainer(container);
+ assert.isTrue(typeof container.ttl === 'number');
+ assert.isTrue(typeof container.logRetention === 'boolean');
+ assert.isTrue(typeof container.cdnUri === 'string');
+ assert.isTrue(container.cdnEnabled);
+};
+
helpers.assertFile = function (file) {
assert.instanceOf(file, cloudfiles.StorageObject);
assert.isNotNull(file.name);

0 comments on commit 22c29f8

Please sign in to comment.