From 7db86e9bfca600209c5481072455267bf71eaf87 Mon Sep 17 00:00:00 2001 From: Thomas Conte Date: Tue, 11 Sep 2018 16:57:27 +0200 Subject: [PATCH] Add get_storage_client_with_key method --- azfilebak/backupconfiguration.py | 14 ++++++++++++++ config.txt | 2 +- tests/test_backupconfiguration.py | 9 ++++++++- tests/test_backupconfigurationfile.py | 4 ++-- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/azfilebak/backupconfiguration.py b/azfilebak/backupconfiguration.py index 0822f29..b1e5f40 100644 --- a/azfilebak/backupconfiguration.py +++ b/azfilebak/backupconfiguration.py @@ -151,3 +151,17 @@ class BackupConfiguration(object): _created = self._block_blob_service.create_container( container_name=self.azure_storage_container_name) return self._block_blob_service + + def get_storage_client_with_key(self, key): + """Create or return BlockBlobService client using given key.""" + if not self._block_blob_service: + account_name = self.get_azure_storage_account_name() + + self._block_blob_service = BlockBlobService( + account_name=account_name, + account_key=key) + + _created = self._block_blob_service.create_container( + container_name=self.azure_storage_container_name) + + return self._block_blob_service diff --git a/config.txt b/config.txt index a6b79a0..6b7de66 100644 --- a/config.txt +++ b/config.txt @@ -1,5 +1,5 @@ -azure.storage.account_name: sadjfksjlahfkj +azure.storage.account_name: testhecbackup azure.storage.container_name: immutab local_temp_directory: /tmp notification_command: /usr/sbin/ticmcmc --stdin diff --git a/tests/test_backupconfiguration.py b/tests/test_backupconfiguration.py index f121f4f..34773a4 100644 --- a/tests/test_backupconfiguration.py +++ b/tests/test_backupconfiguration.py @@ -1,4 +1,5 @@ """Unit tests for backupconfiguration.""" +import os import json import unittest from mock import patch @@ -48,7 +49,7 @@ class TestBackupConfiguration(unittest.TestCase): def test_get_azure_storage_account_name(self): """test get_azure_storage_account_name""" - self.assertEqual(self.cfg.get_azure_storage_account_name(), 'sadjfksjlahfkj') + self.assertEqual(self.cfg.get_azure_storage_account_name(), 'testhecbackup') def test_get_backup_command(self): """test get_commandline""" @@ -68,6 +69,12 @@ class TestBackupConfiguration(unittest.TestCase): self.assertIn('osdisk', filesets) self.assertIn('testecho', filesets) + def test_get_storage_client_with_key(self): + """test get_storage_client_with_key""" + key = os.environ['STORAGE_KEY'] + client = self.cfg.get_storage_client_with_key(key) + self.assertEqual(client.protocol, 'https') + def tearDown(self): self.patcher1.stop() diff --git a/tests/test_backupconfigurationfile.py b/tests/test_backupconfigurationfile.py index 8289dbc..89aae6b 100644 --- a/tests/test_backupconfigurationfile.py +++ b/tests/test_backupconfigurationfile.py @@ -10,14 +10,14 @@ class TestBackupConfigurationFile(unittest.TestCase): values = backupconfigurationfile.BackupConfigurationFile.read_key_value_file( filename="config.txt") self.assertEqual(values['local_temp_directory'], '/tmp') - self.assertEqual(values['azure.storage.account_name'], 'sadjfksjlahfkj') + self.assertEqual(values['azure.storage.account_name'], 'testhecbackup') self.assertEqual(values['azure.storage.container_name'], 'immutab') def test_get_value(self): """test get_value""" config = backupconfigurationfile.BackupConfigurationFile(filename="config.txt") self.assertEqual(config.get_value('local_temp_directory'), '/tmp') - self.assertEqual(config.get_value('azure.storage.account_name'), 'sadjfksjlahfkj') + self.assertEqual(config.get_value('azure.storage.account_name'), 'testhecbackup') self.assertEqual(config.get_value('azure.storage.container_name'), 'immutab') if __name__ == '__main__':