Permalink
Browse files

support latest cloudfront api

  • Loading branch information...
1 parent 884e8a2 commit 8012598636cd08b56caccfb80c8adbcabcfe45f8 @tellnes committed May 28, 2012
Showing with 137 additions and 107 deletions.
  1. +137 −107 lib/index.js
View
@@ -37,6 +37,16 @@ function listOptsToPath(path, opts) {
return path + (qs ? '?' + qs : '');
}
+function readItems(data, key) {
+ if (!Number(data.Quantity)) return []
+
+ var arr = Array.isArray(data.Items[key])
+ ? data.Items[key]
+ : [data.Items[key]]
+
+ return arr
+}
+
function listFactory(that, DataConstr, opts) {
var cb = opts.cb;
@@ -55,16 +65,9 @@ function listFactory(that, DataConstr, opts) {
listKey = 'Streaming' + listKey;
}
- var list = body[listKey];
- if (!list) {
- list = [];
- } else if (!Array.isArray(list)) {
- list = [list];
- }
-
opts.isList = true;
- info.list = list.map(function(data) {
+ info.list = readItems(body, listKey).map(function(data) {
return new DataConstr(that, opts, data);
});
@@ -127,7 +130,7 @@ function CloudFront(key, secret) {
this.key = key;
this.secret = secret;
}
-CloudFront.version = '2012-03-15';
+CloudFront.version = '2012-05-05';
CloudFront.endpoint = 'cloudfront.amazonaws.com';
CloudFront.prototype.request = function(method, path, headers, cb) {
@@ -193,6 +196,23 @@ CloudFront.prototype.request = function(method, path, headers, cb) {
return req;
};
+function generateCacheBehavior(config) {
+ var data = {}
+ if (config.pathPattern) data.PathPattern = config.pathPattern
+ data.TargetOriginId = config.targetOriginId
+ data.ForwardedValues = {}
+ data.ForwardedValues.QueryString = config.forwardQueryString
+ data.TrustedSigners = {}
+ data.TrustedSigners.Enabled = config.trustedSigners.enabled
+ data.TrustedSigners.Quantity = config.trustedSigners.length
+ if (config.trustedSigners.length) {
+ data.TrustedSigners.Items = {}
+ data.TrustedSigners.Items.AwsAccountNumber = config.trustedSigners
+ }
+ data.ViewerProtocolPolicy = config.viewerProtocolPolicy
+ data.MinTTL = config.minTTL
+ return data
+}
CloudFront.prototype.generateDistributionXml = function(config) {
var data = {
@@ -203,54 +223,56 @@ CloudFront.prototype.generateDistributionXml = function(config) {
if (config.defaultRootObject) {
data.DefaultRootObject = config.defaultRootObject;
}
- if (config.cname) {
- data.CNAME = config.cname;
- }
+ if (config.aliases) {
+ data.Aliases = {}
+ data.Aliases.Quantity = config.aliases.length
+ data.Aliases.Items = {}
+ data.Aliases.Items.CNAME = config.aliases
+ }
+
+ data.Origins = {}
+ data.Origins.Quantity = config.origins.length
+ data.Origins.Items = {}
+ data.Origins.Items.Origin = config.origins.map(function(config) {
+ var data = { Id: config.id
+ , DomainName: config.domainName
+ }
+
+ if (config.type == 'custom') {
+ data.CustomOriginConfig = { OriginProtocolPolicy: config.protocolPolicy || '' }
+ if (config.httpPort) {
+ data.CustomOriginConfig.HTTPPort = config.httpPort;
+ }
+ if (config.httpsPort) {
+ data.CustomOriginConfig.HTTPSPort = config.httpsPort;
+ }
- if (config.originType == 's3') {
- data.S3Origin = {
- DNSName: config.originDNSName
- };
- if (config.originAccessIdentity) {
- data.S3Origin.OriginAccessIdentity = 'origin-access-identity/cloudfront/' + config.originAccessIdentity;
- }
- } else if (config.originType == 'custom') {
- data.CustomOrigin = {
- DNSName: config.originDNSName,
- OriginProtocolPolicy: config.originProtocolPolicy
- };
- if (config.originHTTPPort) {
- data.CustomOrigin.HTTPPort = config.originHTTPPort;
- }
- if (config.originHTTPSPort) {
- data.CustomOrigin.HTTPSPort = config.originHTTPSPort;
+ } else {
+ data.S3OriginConfig = {}
+
+ if (config.originAccessIdentity) {
+ data.S3OriginConfig.OriginAccessIdentity = 'origin-access-identity/cloudfront/' + config.originAccessIdentity
+ } else {
+ data.S3OriginConfig.OriginAccessIdentity = ''
+ }
}
- }
+
+ return data
+ })
+
if (config.logging) {
data.Logging = {
+ Enabled: config.logging.enabled,
Bucket: config.logging.bucket,
Prefix: config.logging.prefix
};
}
- if (config.requiredProtocols) {
- data.RequiredProtocols = {
- Protocol: config.requiredProtocols
- };
- }
- if (config.trustedSigners) {
- data.TrustedSigners = {};
- data.TrustedSigners.AwsAccountNumber = [];
- config.trustedSigners.forEach(function(signer) {
- if (signer == 'self') {
- data.TrustedSigners.Self = '';
- } else {
- signer = signer.replace(/-/g, '');
- data.TrustedSigners.AwsAccountNumber.push(signer);
- }
- });
-
- }
+ data.DefaultCacheBehavior = generateCacheBehavior(config.defaultCacheBehavior)
+ data.CacheBehaviors = {}
+ data.CacheBehaviors.Quantity = config.cacheBehaviors.length
+ data.CacheBehaviors.Items = {}
+ data.CacheBehaviors.Items.CacheBehavior = config.cacheBehaviors.map(generateCacheBehavior)
return data2xml('DistributionConfig', data);
};
@@ -630,30 +652,15 @@ function Distribution(client, info, data) {
this.inProgressInvalidationBatches = Number(data.InProgressInvalidationBatches) || 0;
this.domainName = data.DomainName;
- this.activeTrustedSigners = [];
if (data.ActiveTrustedSigners) {
- var signers = data.ActiveTrustedSigners.Signer;
- if (!Array.isArray(signers)) {
- signers = [signers];
- }
- signers.forEach(function(signer) {
- resultingSigner = {};
- if (signer.Self) {
- resultingSigner.self = true;
- } else {
- resultingSigner.awsAccountNumber = signer.AwsAccountNumber;
- }
-
- if (!signer.KeyPairId) {
- resultingSigner.keyPairId = [];
- } else if (!Array.isArray(signer.KeyPairId)) {
- resultingSigner.keyPairId = [signer.KeyPairId];
- } else {
- resultingSigner.keyPairId = signer.KeyPairId;
- }
+ this.activeTrustedSigners = readItems(data.ActiveTrustedSigners, 'Signer').map(function(data) {
+ var obj = { awsAccountNumber: data.AwsAccountNumber
+ , keyPairIds: readItems(data.KeyPairIds, 'KeyPairId')
+ }
- this.activeTrustedSigners.push(resultingSigner);
- }, this);
+ return obj
+ })
+ this.activeTrustedSigners.enabled = data.ActiveTrustedSigners.Enabled === 'true'
}
this.config = new DistributionConfig(this.client, {
@@ -668,7 +675,7 @@ Object.defineProperty(Distribution.prototype, 'origin', {
}
});
- ['cname', 'comment', 'enabled'].forEach(function(key) {
+;['aliases', 'comment', 'enabled'].forEach(function(key) {
Object.defineProperty(Distribution.prototype, key, {
get: function() {
return this.config[key];
@@ -699,60 +706,83 @@ function Invalidation(client, info, data) {
util.inherits(Invalidation, CFObject);
+function readCacheBehavior(data) {
+ var obj = {}
+
+ if (data.PathPattern) {
+ obj.pathPattern = data.PathPattern
+ }
+
+ obj.targetOriginId = data.TargetOriginId
+ obj.forwardQueryString = data.ForwardedValues.QueryString === 'true'
+
+ obj.trustedSigners = []
+
+ if (data.TrustedSigners) {
+ if (Number(data.TrustedSigners.Quantity)) {
+ obj.trustedSigners = data.TrustedSigners.Items
+ }
+
+ obj.trustedSigners.enabled = data.TrustedSigners.Enabled === 'true'
+ }
+
+ obj.viewerProtocolPolicy = data.ViewerProtocolPolicy
+
+ obj.minTTL = data.MinTTL
+
+ return obj
+}
+
function DistributionConfig(client, info, data) {
CFObject.call(this, client);
this.distribution = info.distribution;
this.callerReference = data.CallerReference;
- if (data.S3Origin) {
- this.originType = 's3';
- this.originDNSName = data.S3Origin.DNSName;
-
- if (data.S3Origin.OriginAccessIdentity) {
- this.originAccessIdentity = data.S3Origin.OriginAccessIdentity.substr('origin-access-identity/cloudfront/'.length);
- }
-
- } else if (data.CustomOrigin) {
- this.originType = 'custom';
- this.originDNSName = data.CustomOrigin.DNSName;
- this.originHTTPPort = data.CustomOrigin.HTTPPort;
- this.originHTTPSPort = data.CustomOrigin.HTTPSPort;
- this.originProtocolPolicy = data.CustomOrigin.OriginProtocolPolicy;
- }
+ this.aliases = data.Aliases.Items
+ ? Array.isArray(data.Aliases.Items.CNAME)
+ ? data.Aliases.Items.CNAME
+ : [data.Aliases.Items.CNAME]
+ : []
- this.cname = data.CNAME ? (Array.isArray(data.CNAME) ? data.CNAME : [data.CNAME]) : [];
this.comment = data.Comment;
this.enabled = data.Enabled == 'true';
this.defaultRootObject = data.DefaultRootObject;
+
+
+ // Origins
+ this.origins = readItems(data.Origins, 'Origin').map(function(data) {
+ var obj = { id: data.Id
+ , domainName: data.DomainName
+ , type: data.CustomOriginConfig ? 'custom' : 's3'
+ }
+
+ if (data.CustomOriginConfig) {
+ obj.httpPort = data.CustomOriginConfig.HTTPPort
+ obj.httpsPort = data.CustomOriginConfig.HTTPSPort
+ obj.protocolPolicy = data.CustomOriginConfig.OriginProtocolPolicy
+
+ } else if (typeof data.S3OriginConfig.OriginAccessIdentity === 'string') {
+ obj.originAccessIdentity = data.S3OriginConfig.OriginAccessIdentity
+ .substr('origin-access-identity/cloudfront/'.length)
+
+ }
+ return obj
+ })
+
+ // Logging
+
if (data.Logging) {
this.logging = {
+ enabled: data.Logging.Enabled === 'true',
bucket: data.Logging.Bucket,
prefix: data.Logging.Prefix
};
}
- if (data.RequiredProtocols) {
- this.requiredProtocols = Array.isArray(data.RequiredProtocols.Protocol) ? data.RequiredProtocols.Protocol : [data.RequiredProtocols.Protocol];
- }
-
- this.trustedSigners = [];
-
- if (data.TrustedSigners) {
- if (data.TrustedSigners.Self) {
- this.trustedSigners.push('self');
- }
- var signers = data.TrustedSigners.AwsAccountNumber;
- if (signers) {
- if (!Array.isArray(signers)) {
- signers = [signers];
- }
+ this.defaultCacheBehavior = readCacheBehavior(data.DefaultCacheBehavior)
- signers.forEach(function(signer) {
- this.trustedSigners.push(signer);
- }, this);
- }
- }
+ this.cacheBehaviors = readItems(data.CacheBehaviors, 'CacheBehavior').map(readCacheBehavior)
}
util.inherits(DistributionConfig, CFObject);

0 comments on commit 8012598

Please sign in to comment.