1 /**
  2  * @fileOverview Base Configuration.
  3  */
  4 
  5 (function () {
  6   var CloudError = require("./errors").CloudError,
  7     Configuration;
  8 
  9   /**
 10    * Configuration class.
 11    *
 12    * @param  {Object}   options     Options object.
 13    * @config {string}   provider    Cloud provider enum.
 14    * @config {string}   account     Account name.
 15    * @config {string}   secretKey   Secret key.
 16    * @config {string}   [ssl=false] Use SSL?
 17    * @config {string}   [authUrl]   Authentication URL.
 18    * @config {number}   [timeout]   HTTP timeout in seconds.
 19    * @constructor
 20    */
 21   Configuration = function (options) {
 22     var self = this,
 23       provider = null,
 24       key;
 25 
 26     // Argument parsing and validation.
 27     options = options || {};
 28     if (!options.account) { throw new Error("No account name."); }
 29     if (!options.secretKey) { throw new Error("No secret key."); }
 30 
 31     // Manually bind constants.
 32     self.PROVIDERS = Configuration.PROVIDERS;
 33 
 34     // Defaults.
 35     self._auth = null;
 36     self._provider = options.provider;
 37 
 38     // Find provider
 39     for (key in self.PROVIDERS) {
 40       if (self.PROVIDERS.hasOwnProperty(key)) {
 41         provider = self.PROVIDERS[key];
 42         if (options.provider === provider.name) {
 43           // Found a provider. Set up auth and connection.
 44           self._auth = provider.authFn(options);
 45           break;
 46         }
 47       }
 48     }
 49 
 50     if (!self._auth) {
 51       throw new Error("Not a valid provider: \"" + options.provider + "\"");
 52     }
 53   };
 54 
 55   /**
 56    * Provider dictionary with delayed object creation.
 57    */
 58   Configuration.PROVIDERS = {
 59     /**#@+ @ignore */
 60     AWS: { name: 'aws', authFn: function (options) {
 61       var Authentication = require("./provider/aws").Authentication;
 62       return new Authentication(options);
 63     }},
 64     GOOGLE: { name: 'google', authFn: function (options) {
 65       var Authentication = require("./provider/google").Authentication;
 66       return new Authentication(options);
 67     }}
 68     /**#@-*/
 69   };
 70 
 71   Object.defineProperties(Configuration.prototype, {
 72     /**
 73      * Connection object.
 74      *
 75      * @name Configuration#connection
 76      * @type base.Connection
 77      */
 78     connection: {
 79       get: function () {
 80         return this._auth.connection;
 81       }
 82     },
 83 
 84     /**
 85      * Provider string.
 86      *
 87      * @name Configuration#provider
 88      * @type string
 89      */
 90     provider: {
 91       get: function () {
 92         return this._provider;
 93       }
 94     }
 95   });
 96 
 97   /** Test provider (AWS). */
 98   Configuration.prototype.isAws = function () {
 99     return this._provider === this.PROVIDERS.AWS.name;
100   };
101 
102   /** Test provider (Google Storage). */
103   Configuration.prototype.isGoogle = function () {
104     return this._provider === this.PROVIDERS.GOOGLE.name;
105   };
106 
107   /**
108    * Get configuration from options (settings) object.
109    *
110    * @param  {Object}   options     Options object.
111    * @config {string}   provider    Cloud provider enum.
112    * @config {string}   account     Account name.
113    * @config {string}   secretKey   Secret key.
114    * @config {string}   [ssl=false] Use SSL?
115    * @config {string}   [authUrl]   Authentication URL.
116    * @config {number}   [timeout]   HTTP timeout in seconds.
117    * @returns {Configuration} Configuration object.
118    */
119   Configuration.fromObj = function (options) {
120     return new Configuration(options);
121   };
122 
123   /**
124    * Get configuration from environment.
125    *
126    * ## Environment Variables
127    * Sunny can use the following environment variables:
128    *
129    * - **SUNNY_PROVIDER**: ``"aws"`` or ``"google"``
130    * - **SUNNY_ACCOUNT**: ``"ACCOUNT_NAME"``
131    * - **SUNNY_SECRET_KEY**: ``"ACCOUNT_SECRET_KEY"``
132    * - **SUNNY_AUTH_URL**: ``"s3.amazonaws.com"``
133    * - **SUNNY_SSL**: ``true`` or ``false``
134    *
135    * @returns {Configuration} Configuration object.
136    */
137   Configuration.fromEnv = function () {
138     var ssl = process.env.SUNNY_SSL,
139       useSsl = !!(typeof ssl === 'string' && ssl.toLowerCase() === 'true');
140 
141     return new Configuration({
142       provider: process.env.SUNNY_PROVIDER,
143       account: process.env.SUNNY_ACCOUNT,
144       secretKey: process.env.SUNNY_SECRET_KEY,
145       authUrl: process.env.SUNNY_AUTH_URL || null,
146       ssl: useSsl
147     });
148   };
149 
150   module.exports.Configuration = Configuration;
151 }());
152