Permalink
Browse files

Updating createFromConnectionString

  • Loading branch information...
1 parent 7a33f08 commit 3273bbbe1bd673e86d0f9284330f6d986e9dec04 Andre Rodrigues committed Oct 11, 2012
Showing with 134 additions and 61 deletions.
  1. +72 −1 lib/services/core/servicesettings.js
  2. +62 −60 lib/services/core/storageservicesettings.js
@@ -14,6 +14,8 @@
*/
var util = require('../../util/util');
+var _ = require('underscore');
+_.mixin(require('underscore.string'));
var ConnectionStringParser = require('./connectionstringparser');
@@ -52,6 +54,50 @@ exports.parseAndValidateKeys = function (connectionString, validKeys) {
return tokenizedSettings;
};
+/**
+* Creates an anonymous function that acts as predicate.
+*
+* @param array {requirements} The array of conditions to satisfy.
+* @param boolean {isRequired} Either these conditions are all required or all
+* optional.
+* @param boolean {atLeastOne} Indicates that at least one requirement must
+* succeed.
+*
+* @return {function}
+*/
+exports.getValidator = function (requirements, isRequired, atLeastOne) {
+ return function (userSettings) {
+ var oneFound = false;
+ var result = _.map(userSettings, function(setting) { return setting.toLowerCase(); });
+
+ for (var requirement in requirements) {
+ var settingName = requirements[requirement]['SettingName'].toLowerCase();
+
+ // Check if the setting name exists in the provided user settings.
+ if (result[settingName]) {
+ // Check if the provided user setting value is valid.
+ var validationFunc = requirement['SettingConstraint'];
+ var isValid = validationFunc(result[settingName]);
+
+ if (isValid) {
+ // Remove the setting as indicator for successful validation.
+ delete result[settingName];
+ oneFound = true;
+ }
+ } else if (isRequired) {
+ // If required then fail because the setting does not exist
+ return null;
+ }
+ }
+
+ if (atLeastOne) {
+ // At least one requirement must succeed, otherwise fail.
+ return oneFound ? result : null;
+ } else {
+ return result;
+ }
+ };
+};
/**
* Creates a setting value condition that validates it is one of the
@@ -101,4 +147,29 @@ exports.settingWithFunc = function (name, predicate) {
requirement['SettingConstraint'] = predicate;
return requirement;
-};
+};
+
+
+/**
+* Tests to see if a given list of settings matches a set of filters exactly.
+*
+* @param array $settings The settings to check.
+*
+* @return boolean If any filter returns null, false. If there are any settings
+* left over after all filters are processed, false. Otherwise true.
+*/
+exports.matchedSpecification = function (settings) {
+ var constraints = Array.prototype.slice.call(arguments, 1, arguments.length);
+
+ for (var constraint in constraints) {
+ var remainingSettings = constraints[constraint](settings);
+
+ if (!remainingSettings) {
+ return false;
+ } else {
+ settings = remainingSettings;
+ }
+ }
+
+ return (settings.length === 0);
+}
@@ -173,56 +173,6 @@ StorageServiceSettings._createStorageServiceSettings = function (settings, blobE
queueEndpointUri
);
- tableEndpointUri = Utilities::tryGetValueInsensitive(
- ConnectionStringKeys.TABLE_ENDPOINT_NAME,
- settings,
- tableEndpointUri
- );
-
- accountName = Utilities::tryGetValueInsensitive(
- ConnectionStringKeys.ACCOUNT_NAME_NAME,
- settings
- );
-
- accountKey = util.tryGetValueInsensitive(
- ConncetionStringKeys.ACCOUNT_KEY_NAME,
- settings
- );
-
- return new StorageServiceSettings(
- accountName,
- accountKey,
- blobEndpointUri,
- queueEndpointUri,
- tableEndpointUri
- );
-};
-
-/**
-* Creates StorageServiceSettings object given endpoints uri.
-*
-* @param {array} settings The service settings.
-* @param {string} blobEndpointUri The blob endpoint uri.
-* @param {string} queueEndpointUri The queue endpoint uri.
-* @param {string} tableEndpointUri The table endpoint uri.
-*
-* @return {StorageServiceSettings}
-*/
-StorageServiceSettings._createStorageServiceSettings = function (
- settings, blobEndpointUri, queueEndpointUri, tableEndpointUri
-) {
- blobEndpointUri = util.tryGetValueInsensitive(
- ConnectionStringKeys.BLOB_ENDPOINT_NAME,
- settings,
- blobEndpointUri
- );
-
- queueEndpointUri = util.tryGetValueInsensitive(
- ConnectionStringKeys.QUEUE_ENDPOINT_NAME,
- settings,
- queueEndpointUri
- );
-
tableEndpointUri = util.tryGetValueInsensitive(
ConnectionStringKeys.TABLE_ENDPOINT_NAME,
settings,
@@ -235,7 +185,7 @@ StorageServiceSettings._createStorageServiceSettings = function (
);
accountKey = util.tryGetValueInsensitive(
- ConnectionStringKeys.ACCOUNT_KEY_NAME,
+ ConncetionStringKeys.ACCOUNT_KEY_NAME,
settings
);
@@ -258,19 +208,71 @@ StorageServiceSettings._createStorageServiceSettings = function (
StorageServiceSettings.createFromConnectionString = function (connectionString) {
var tokenizedSettings = ServiceSettings.parseAndValidateKeys(connectionString, validKeys);
- return this._createStorageServiceSettings(
+ // Devstore case
+ matchedSpecs = ServiceSettings.matchedSpecification(
tokenizedSettings,
- this._getDefaultServiceEndpoint(
- tokenizedSettings,
- ConnectionStringKeys.BLOB_BASE_DNS_NAME
+ ServiceSettings.allRequired(_useDevelopmentStorageSetting),
+ ServiceSettings.optional(_developmentStorageProxyUriSetting)
+ );
+
+ if (matchedSpecs) {
+ proxyUri = util.tryGetValueInsensitive(
+ ConnectionStringKeys.DEVELOPMENT_STORAGE_PROXY_URI_NAME,
+ tokenizedSettings
+ );
+
+ return _getDevelopmentStorageAccount(proxyUri);
+ }
+
+ // Automatic case
+ matchedSpecs = ServiceSettings.matchedSpecification(
+ tokenizedSettings,
+ ServiceSettings.allRequired(
+ _defaultEndpointsProtocolSetting,
+ _accountNameSetting,
+ _accountKeySetting
),
- this._getDefaultServiceEndpoint(
+ ServiceSettings.optional(
+ _blobEndpointSetting,
+ _queueEndpointSetting,
+ _tableEndpointSetting
+ )
+ );
+
+ if (matchedSpecs) {
+ return this._createStorageServiceSettings(
tokenizedSettings,
- ConnectionStringKeys.QUEUE_BASE_DNS_NAME
+ this._getDefaultServiceEndpoint(
+ tokenizedSettings,
+ ConnectionStringKeys.BLOB_BASE_DNS_NAME
+ ),
+ this._getDefaultServiceEndpoint(
+ tokenizedSettings,
+ ConnectionStringKeys.QUEUE_BASE_DNS_NAME
+ ),
+ this._getDefaultServiceEndpoint(
+ tokenizedSettings,
+ ConnectionStringKeys.TABLE_BASE_DNS_NAME
+ ));
+ }
+
+ // Explicit case
+ matchedSpecs = ServiceSettings.matchedSpecification(
+ tokenizedSettings,
+ ServiceSettings.atLeastOne(
+ _blobEndpointSetting,
+ _queueEndpointSetting,
+ _tableEndpointSetting
),
- this._getDefaultServiceEndpoint(
- tokenizedSettings,
- ConnectionStringKeys.TABLE_BASE_DNS_NAME
+ ServiceSettings.allRequired(
+ _accountNameSetting,
+ _accountKeySetting
)
);
+
+ if (matchedSpecs) {
+ return this._createStorageServiceSettings(tokenizedSettings);
+ }
+
+ ServiceSettings.noMatch(connectionString);
};

0 comments on commit 3273bbb

Please sign in to comment.