'use strict'; exports = module.exports = { getBackupDetails: getBackupDetails, getAppBackupDetails: getAppBackupDetails, getAllPaged: getAllPaged, getRestoreUrl: getRestoreUrl, copyObject: copyObject }; var assert = require('assert'), AWS = require('aws-sdk'); function getBackupCredentials(apiConfig, callback) { assert.strictEqual(typeof apiConfig, 'object'); assert.strictEqual(typeof callback, 'function'); assert(apiConfig.accessKeyId && apiConfig.secretAccessKey); var credentials = { signatureVersion: 'v4', accessKeyId: apiConfig.accessKeyId, secretAccessKey: apiConfig.secretAccessKey, region: apiConfig.region || 'us-east-1' }; if (apiConfig.endpoint) credentials.endpoint = new AWS.Endpoint(apiConfig.endpoint); callback(null, credentials); } function getBackupDetails(apiConfig, id, callback) { assert.strictEqual(typeof apiConfig, 'object'); assert.strictEqual(typeof id, 'string'); assert.strictEqual(typeof callback, 'function'); var s3Url = 's3://' + apiConfig.bucket + '/' + apiConfig.prefix + '/' + id; var region = apiConfig.region || 'us-east-1'; var details = { backupScriptArguments: [ 's3', s3Url, apiConfig.accessKeyId, apiConfig.secretAccessKey, region, apiConfig.key ] }; callback(null, details); } function getAppBackupDetails(apiConfig, appId, dataId, configId, callback) { assert.strictEqual(typeof apiConfig, 'object'); assert.strictEqual(typeof appId, 'string'); assert.strictEqual(typeof dataId, 'string'); assert.strictEqual(typeof configId, ''); assert.strictEqual(typeof callback, 'function'); var s3DataUrl = 's3://' + apiConfig.bucket + '/' + apiConfig.prefix + '/' + dataId; var s3ConfigUrl = 's3://' + apiConfig.bucket + '/' + apiConfig.prefix + '/' + configId; var region = apiConfig.region || 'us-east-1'; var details = { backupScriptArguments: [ 's3', appId, s3ConfigUrl, s3DataUrl, apiConfig.accessKeyId, apiConfig.secretAccessKey, region, apiConfig.key ] }; callback(null, details); } function getAllPaged(apiConfig, page, perPage, callback) { assert.strictEqual(typeof apiConfig, 'object'); assert.strictEqual(typeof page, 'number'); assert.strictEqual(typeof perPage, 'number'); assert.strictEqual(typeof callback, 'function'); getBackupCredentials(apiConfig, function (error, credentials) { if (error) return callback(error); var s3 = new AWS.S3(credentials); var params = { Bucket: apiConfig.bucket, EncodingType: 'url', Prefix: apiConfig.prefix + '/backup_' }; s3.listObjects(params, function (error, data) { if (error) return callback(error); var results = data.Contents.map(function (backup) { return { creationTime: backup.LastModified, restoreKey: backup.Key.slice(apiConfig.prefix.length + 1), dependsOn: [] // FIXME empty dependsOn is wrong and version property is missing!! }; }); results.sort(function (a, b) { return a.creationTime < b.creationTime; }); return callback(null, results); }); }); } function getRestoreUrl(apiConfig, filename, callback) { assert.strictEqual(typeof apiConfig, 'object'); assert.strictEqual(typeof filename, 'string'); assert.strictEqual(typeof callback, 'function'); getBackupCredentials(apiConfig, function (error, credentials) { if (error) return callback(error); var s3 = new AWS.S3(credentials); var params = { Bucket: apiConfig.bucket, Key: apiConfig.prefix + '/' + filename, Expires: 60 * 60 * 24 /* 1 day */ }; var url = s3.getSignedUrl('getObject', params); callback(null, { url: url }); }); } function copyObject(apiConfig, from, to, callback) { assert.strictEqual(typeof apiConfig, 'object'); assert.strictEqual(typeof from, 'string'); assert.strictEqual(typeof to, 'string'); assert.strictEqual(typeof callback, 'function'); getBackupCredentials(apiConfig, function (error, credentials) { if (error) return callback(error); var params = { Bucket: apiConfig.bucket, // target bucket Key: apiConfig.prefix + '/' + to, // target file CopySource: apiConfig.bucket + '/' + apiConfig.prefix + '/' + from, // source }; var s3 = new AWS.S3(credentials); s3.copyObject(params, callback); }); }