Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rackspace CDN methods #96

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
147 changes: 147 additions & 0 deletions lib/pkgcloud/rackspace/cdn/client/containers.js
@@ -0,0 +1,147 @@
/*
* containers.js Instance methods for working with containers from Rackspace Cloudfiles
*
* (C) 2013 Nodejitsu Inc.
*
*/

var base = require('../../../core/storage'),
async = require('async'),
pkgcloud = require('../../../../../lib/pkgcloud'),
cdn = pkgcloud.providers.rackspace.cdn;

//
// ### funtion getContainers (callback)
// #### @callback {function} Continuation to respond to when complete
// Gets all of the CDN Enabled Rackspace Cloudfiles containers for this instance
//
exports.getContainers = function getContainers (callback) {
var self = this;

this.request({ path: [true]}, callback, function (body) {
callback(null, body.map(function (container) {
container.cdnEnabled = container.cdn_enabled;
container.logRetention = container.log_retention;
container.cdnUri = container.cdn_uri;
container.cdnSslUri = container.cdn_ssl_uri;
container.cdnIosUri = container.cdn_ios_uri;

return new cdn.Container(self, container);
}));
});
};

//
// ### function getContainer (container, callback)
// #### @container {string|cdn.Container} Name of the container to return
// #### @callback {function} Continuation to respond when complete
// Responds with the CDN Enabled Rackspace Cloudfiles container for specified `container`
//
exports.getContainer = function getContainer(container, callback) {
var containerName = container instanceof cdn.Container ? container.name : container,
self = this;

this.request('HEAD', containerName, callback, function (body, res) {
container = {
name: containerName
};

container.cdnUri = res.headers['x-cdn-uri'];
container.cdnSslUri = res.headers['x-cdn-ssl-uri'];
container.cdnIosUri = res.headers['x-cdn-ios-uri'];
container.cdnEnabled = res.headers['x-cdn-enabled'] == 'True';
container.ttl = parseInt(res.headers['x-ttl'], 10);
container.logRetention = res.headers['x-log-retention'] == 'True';

callback(null, new (cdn.Container)(self, container));
});
};

//
// ### function createContainer (container, callback);
// #### @container {string|Container} Container to create
// #### @callback {function} Continuation to respond to when complete.
// Creates a CDN Enabled `container` in Rackspace Cloudfiles associated with
// this instance
//
exports.createContainer = function (container, callback) {
var containerName = container instanceof cdn.Container ? container.name : container,
self = this;
// Have to manually call auth here in order to pull the storageUrl from the
// client config. All the config urls are loaded on auth
if(this.authorized) {
request();
}
else {
this.auth(request);
}
function request () {
var createOptions = {
method: 'PUT',
url: self.serviceUrl.apply(self, ['storage', containerName])
};
self.request(createOptions, callback, function (body, res) {
var cdnOptions = {
method: 'PUT',
path: containerName,
headers: {
'X-TTL': 259200,
'X-CDN-Enabled': true
}
};
self.request(cdnOptions, callback, function (body, res) {
container = {
name: containerName
};
container.cdnUri = res.headers['x-cdn-uri'];
container.cdnSslUri = res.headers['x-cdn-ssl-uri'];
container.cdnIosUri = res.headers['x-cdn-ios-uri'];
container.cdnEnabled = true;
container.ttl = 259200;
container.logRetention = res.headers['x-log-retention'] == 'True';

callback(null, new (cdn.Container)(self, container));
});
});
}
};

//
// ### function destroyContainer (container, callback)
// #### @container {string} Name of the container to destroy
// #### @callback {function} Continuation to respond to when complete.
// Destroys the specified `container` and all files in it.
//
exports.destroyContainer = function (container, callback) {
var containerName = container instanceof cdn.Container ? container.name : container,
self = this;

this.getFiles(container, function (err, files) {
if (err) {
return callback(err);
}
function deleteContainer (err) {
if (err) {
return callback(err);
}

var options = {
method: 'DELETE',
url: self.serviceUrl.apply(self, ['storage', containerName])
};

self.request(options, callback, callback.bind(null, null, true));
}

function destroyFile (file, next) {
file.remove(next);
}

if (files.length === 0) {
return deleteContainer();
}

async.forEach(files, destroyFile, deleteContainer);
});

};
78 changes: 78 additions & 0 deletions lib/pkgcloud/rackspace/cdn/client/files.js
@@ -0,0 +1,78 @@
/*
* files.js Instance methods for working with files from Rackspace Cloudfiles
*
* (C) 2013 Nodejitsu Inc.
*
*/

var base = require('../../../core/storage'),
pkgcloud = require('../../../../../lib/pkgcloud'),
storage = pkgcloud.providers.rackspace.storage;

exports.removeFile = function (container, file, callback) {
var containerName = container instanceof base.Container ? container.name : container,
self = this;

if (this.authorized) {
request();
}
else {
this.auth(request);
}

function request () {
var options = {
method: 'DELETE',
url: self.serviceUrl.apply(self, ['storage', containerName, file])
};
self.request(options, callback, callback.bind(null, null, true));
}
};

exports.getFile = function (container, file, callback) {
var containerName = container instanceof base.Container ? container.name : container,
self = this;

if (this.authorized) {
request();
}
else {
this.auth(request);
}
function request () {
var options = {
method: 'HEAD',
url: self.serviceUrl.apply(self, ['storage', containerName, file, true])
};
self.request(options, callback, function (body, res) {
callback(null, new storage.File(self, utile.mixin(res.headers, {
container: containerName,
name: file
})));
});
}
};

exports.getFiles = function (container, callback) {
var containerName = container instanceof base.Container ? container.name : container,
self = this;
if (this.authorized) {
request();
}
else {
this.auth(request);
}

function request () {
var options = {
method: 'GET',
url: self.serviceUrl.apply(self, ['storage', containerName, true])
};
self.request(options, callback, function (body, res) {
callback(null, body.map(function (file) {
file.container = containerName;
return new storage.File(self, file);
}));
});
}
};
5 changes: 4 additions & 1 deletion lib/pkgcloud/rackspace/cdn/client/index.js
Expand Up @@ -10,10 +10,13 @@ var utile = require('utile'),

var Client = exports.Client = function (options) {
storage.Client.call(this, options);

utile.mixin(this, require('./containers'));
utile.mixin(this, require('./files'));
};

utile.inherits(Client, storage.Client);

Client.prototype.url = function () {
return this.serviceUrl.apply(this, ['cdn'].concat(Array.prototype.slice.call(arguments)));
};
};
18 changes: 18 additions & 0 deletions lib/pkgcloud/rackspace/cdn/container.js
@@ -0,0 +1,18 @@
/*
* container.js Rackspace Cloudfiles container as CDN
*
* (C) 2013 Nodejitsu Inc.
*
*/

var utile = require('utile'),
base = require('../storage/container');

var Container = exports.Container = function Container(client, details) {
base.Container.call(this, client, details);

this.cdnIosUri = details.cdnIosUri;
};

utile.inherits(Container, base.Container);

13 changes: 13 additions & 0 deletions lib/pkgcloud/rackspace/cdn/index.js
@@ -0,0 +1,13 @@
/*
* index.js: Top-level include for the Rackspace cdn module
*
* (C) 2013 Nodejitsu Inc.
*
*/

exports.Client = require('./client').Client;
exports.Container = require('./container').Container;

exports.createClient = function (options) {
return new exports.Client(options);
};