diff --git a/lib/azure.js b/lib/azure.js index 10b3df180..9255bbe45 100644 --- a/lib/azure.js +++ b/lib/azure.js @@ -151,6 +151,7 @@ exports.ISO8061Date = require('./util/iso8061date'); exports.Logger = require('./diagnostics/logger'); exports.ConnectionStringParser = require('./services/core/connectionstringparser'); exports.ServiceSettings = require('./services/core/servicesettings'); +exports.StorageServiceSettings = require('./services/core/storageservicesettings'); exports.Validate = require('./util/validate'); /* diff --git a/lib/services/core/storageservicesettings.js b/lib/services/core/storageservicesettings.js index 9954061bf..03c2676b9 100644 --- a/lib/services/core/storageservicesettings.js +++ b/lib/services/core/storageservicesettings.js @@ -13,6 +13,8 @@ * limitations under the License. */ +var url = require('url'); + var util = require('../../util/util'); var ConnectionStringParser = require('./connectionstringparser'); @@ -23,22 +25,12 @@ var Validate = require('../../util/validate'); exports = module.exports = StorageServiceSettings; -var validKeys = [ - ConnectionStringKeys.USE_DEVELOPMENT_STORAGE_NAME, - ConnectionStringKeys.DEVELOPMENT_STORAGE_PROXY_URI_NAME, - ConnectionStringKeys.DEFAULT_ENDPOINTS_PROTOCOL_NAME, - ConnectionStringKeys.ACCOUNT_NAME_NAME, - ConnectionStringKeys.ACCOUNT_KEY_NAME, - ConnectionStringKeys.BLOB_ENDPOINT_NAME, - ConnectionStringKeys.QUEUE_ENDPOINT_NAME, - ConnectionStringKeys.TABLE_ENDPOINT_NAME -]; - -var _useDevelopmentStorageSetting = ServiceSetting.setting(ConnectionStringKeys.USE_DEVELOPMENT_STORAGE_NAME, 'true'); -var _developmentStorageProxyUriSetting = ServiceSetting.settingWithFunc(ConnectionStringKeys.DEVELOPMENT_STORAGE_PROXY_URI_NAME, Validate.getIsValidUri()); -var _defaultEndpointsProtocolSetting = ServiceSetting.setting(ConnectionStringKeys.DEFAULT_ENDPOINTS_PROTOCOL_NAME, 'http', 'https'); -var _accountNameSetting = ServiceSetting.setting(ConnectionStringKeys.ACCOUNT_NAME_NAME); -var _accountKeySetting = ServiceSetting.settingWithFunc( +var _devStoreAccount = null; +var _useDevelopmentStorageSetting = ServiceSettings.setting(ConnectionStringKeys.USE_DEVELOPMENT_STORAGE_NAME, 'true'); +var _developmentStorageProxyUriSetting = ServiceSettings.settingWithFunc(ConnectionStringKeys.DEVELOPMENT_STORAGE_PROXY_URI_NAME, Validate.getIsValidUri()); +var _defaultEndpointsProtocolSetting = ServiceSettings.setting(ConnectionStringKeys.DEFAULT_ENDPOINTS_PROTOCOL_NAME, 'http', 'https'); +var _accountNameSetting = ServiceSettings.setting(ConnectionStringKeys.ACCOUNT_NAME_NAME); +var _accountKeySetting = ServiceSettings.settingWithFunc( ConnectionStringKeys.ACCOUNT_KEY_NAME, // base64_decode will return false if the $key is not in base64 format. function (key) { @@ -50,21 +42,32 @@ var _accountKeySetting = ServiceSetting.settingWithFunc( } }); -var _blobEndpointSetting = ServiceSetting.settingWithFunc( +var _blobEndpointSetting = ServiceSettings.settingWithFunc( ConnectionStringKeys.BLOB_ENDPOINT_NAME, - Validate::getIsValidUri() + Validate.getIsValidUri() ); -var _queueEndpointSetting = ServiceSetting.settingWithFunc( +var _queueEndpointSetting = ServiceSettings.settingWithFunc( ConnectionStringKeys.QUEUE_ENDPOINT_NAME, - Validate::getIsValidUri() + Validate.getIsValidUri() ); -var _tableEndpointSetting = ServiceSetting.settingWithFunc( +var _tableEndpointSetting = ServiceSettings.settingWithFunc( ConnectionStringKeys.TABLE_ENDPOINT_NAME, - Validate::getIsValidUri() + Validate.getIsValidUri() ); +var validKeys = [ + ConnectionStringKeys.USE_DEVELOPMENT_STORAGE_NAME, + ConnectionStringKeys.DEVELOPMENT_STORAGE_PROXY_URI_NAME, + ConnectionStringKeys.DEFAULT_ENDPOINTS_PROTOCOL_NAME, + ConnectionStringKeys.ACCOUNT_NAME_NAME, + ConnectionStringKeys.ACCOUNT_KEY_NAME, + ConnectionStringKeys.BLOB_ENDPOINT_NAME, + ConnectionStringKeys.QUEUE_ENDPOINT_NAME, + ConnectionStringKeys.TABLE_ENDPOINT_NAME +]; + /** * Creates new storage service settings instance. * @@ -82,6 +85,47 @@ function StorageServiceSettings(name, key, blobEndpointUri, queueEndpointUri, ta this._tableEndpointUri = tableEndpointUri; } +/** +* Returns a StorageServiceSettings with development storage credentials using +* the specified proxy Uri. +* +* @param {string} proxyUri The proxy endpoint to use. +* +* @return {StorageServiceSettings} +*/ +StorageServiceSettings._getDevelopmentStorageAccount = function (proxyUri) { + if (!proxyUri) { + return StorageServiceSettings.developmentStorageAccount(); + } + + var parsedUri = url.parse(proxyUri); + var scheme = parsedUri.protocol; + var host = parsedUri.host; + var prefix = scheme + '://' + host; + + return new StorageServiceSettings( + ConnectionStringKeys.DEV_STORE_NAME, + ConnectionStringKeys.DEV_STORE_KEY, + prefix + ':10000/devstoreaccount1/', + prefix + ':10001/devstoreaccount1/', + prefix + ':10002/devstoreaccount1/' + ); +}; + +/** +* Gets a StorageServiceSettings object that references the development storage +* account. +* +* @return {StorageServiceSettings} +*/ +StorageServiceSettings.developmentStorageAccount = function () { + if (!_devStoreAccount) { + _devStoreAccount = StorageServiceSettings._getDevelopmentStorageAccount(Constants.DEV_STORE_URI); + } + + return _devStoreAccount; +} + /** * Creates StorageServiceSettings object given endpoints uri. * diff --git a/lib/util/constants.js b/lib/util/constants.js index fb6c02e48..ac870250e 100644 --- a/lib/util/constants.js +++ b/lib/util/constants.js @@ -817,6 +817,14 @@ var Constants = { */ ATOM_SUBSCRIPTION_DESCRIPTION_MARKER: 'SubscriptionDescription', + /** + * The development store URI. + * + * @const + * @type {string} + */ + DEV_STORE_URI: 'http://127.0.0.1', + /** * Defines constants for use with blob operations. */ diff --git a/test/services/core/storageservicesettings-tests.js b/test/services/core/storageservicesettings-tests.js new file mode 100644 index 000000000..d82add1b8 --- /dev/null +++ b/test/services/core/storageservicesettings-tests.js @@ -0,0 +1,32 @@ +/** +* Copyright (c) Microsoft. All rights reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +var should = require('should'); + +var testutil = require('../../util/util'); +var azure = testutil.libRequire('azure'); +var StorageServiceSettings = azure.StorageServiceSettings; + +suite('storageservicesettings-tests', function () { + test('getDevelopmentStorageAccount', function () { + var developmentStorageAccount = StorageServiceSettings._getDevelopmentStorageAccount(); + + developmentStorageAccount._name.should.equal('devstoreaccount1'); + developmentStorageAccount._key.should.equal('Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=='); + developmentStorageAccount._blobEndpointUri.should.equal('http:://127.0.0.1:10000/devstoreaccount1/'); + developmentStorageAccount._queueEndpointUri.should.equal('http:://127.0.0.1:10001/devstoreaccount1/'); + developmentStorageAccount._tableEndpointUri.should.equal('http:://127.0.0.1:10002/devstoreaccount1/'); + }); +}); \ No newline at end of file diff --git a/test/testlist.txt b/test/testlist.txt index 113f327df..a6c167892 100644 --- a/test/testlist.txt +++ b/test/testlist.txt @@ -1,2 +1,3 @@ services/core/servicesettings-tests.js +services/core/storageservicesettings-tests.js util/validate-tests.js \ No newline at end of file