diff options
author | spisarski <s.pisarski@cablelabs.com> | 2017-11-17 08:35:45 -0700 |
---|---|---|
committer | spisarski <s.pisarski@cablelabs.com> | 2017-11-20 07:43:34 -0700 |
commit | 88c180252d78226e06ecf53a23d4f0eca9815bb8 (patch) | |
tree | d448f86a7c3f15cc5d255054465ff9b1f67967c6 /snaps/config | |
parent | f9d332f6c67f5e0cb70c1570499996926ffbdf5c (diff) |
Refactoring of KeypairSettings to extend KeypairConfig
KeypairSettings and glance_utils have a runtime cyclical
dependency. This patch reduces this dependency and
deprecates the KeypairSettings class.
JIRA: SNAPS-219
Change-Id: I92f51ecb77238444697e922c8f089e78d1c643aa
Signed-off-by: spisarski <s.pisarski@cablelabs.com>
Diffstat (limited to 'snaps/config')
-rw-r--r-- | snaps/config/keypair.py | 61 | ||||
-rw-r--r-- | snaps/config/tests/keypair_tests.py | 179 |
2 files changed, 240 insertions, 0 deletions
diff --git a/snaps/config/keypair.py b/snaps/config/keypair.py new file mode 100644 index 0000000..2304c6e --- /dev/null +++ b/snaps/config/keypair.py @@ -0,0 +1,61 @@ +# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs") +# and others. 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. +from neutronclient.common.utils import str2bool + + +class KeypairConfig(object): + """ + Class representing a keypair configuration + """ + + def __init__(self, **kwargs): + """ + Constructor - all parameters are optional + :param name: The keypair name. + :param public_filepath: The path to/from the filesystem where the + public key file is or will be stored + :param private_filepath: The path where the generated private key file + will be stored + :param key_size: The number of bytes for the key size when it needs to + be generated (Must be >=512 default 1024) + :param delete_on_clean: when True, the key files will be deleted when + OpenStackKeypair#clean() is called + :return: + """ + + self.name = kwargs.get('name') + self.public_filepath = kwargs.get('public_filepath') + self.private_filepath = kwargs.get('private_filepath') + self.key_size = int(kwargs.get('key_size', 1024)) + + if kwargs.get('delete_on_clean') is not None: + if isinstance(kwargs.get('delete_on_clean'), bool): + self.delete_on_clean = kwargs.get('delete_on_clean') + else: + self.delete_on_clean = str2bool(kwargs.get('delete_on_clean')) + else: + self.delete_on_clean = None + + if not self.name: + raise KeypairConfigError('Name is a required attribute') + + if self.key_size < 512: + raise KeypairConfigError('key_size must be >=512') + + +class KeypairConfigError(Exception): + """ + Exception to be thrown when keypair settings are incorrect + """ diff --git a/snaps/config/tests/keypair_tests.py b/snaps/config/tests/keypair_tests.py new file mode 100644 index 0000000..6d0fec4 --- /dev/null +++ b/snaps/config/tests/keypair_tests.py @@ -0,0 +1,179 @@ +# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs") +# and others. 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. +import unittest + +from snaps.config.keypair import KeypairConfigError, KeypairConfig + + +class KeypairConfigUnitTests(unittest.TestCase): + """ + Tests the construction of the KeypairConfig class + """ + + def test_no_params(self): + with self.assertRaises(KeypairConfigError): + KeypairConfig() + + def test_empty_config(self): + with self.assertRaises(KeypairConfigError): + KeypairConfig(**dict()) + + def test_small_key_size(self): + with self.assertRaises(KeypairConfigError): + KeypairConfig(name='foo', key_size=511) + + def test_name_only(self): + settings = KeypairConfig(name='foo') + self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) + self.assertIsNone(settings.public_filepath) + self.assertIsNone(settings.private_filepath) + self.assertIsNone(settings.delete_on_clean) + + def test_config_with_name_only(self): + settings = KeypairConfig(**{'name': 'foo'}) + self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) + self.assertIsNone(settings.public_filepath) + self.assertIsNone(settings.private_filepath) + self.assertIsNone(settings.delete_on_clean) + + def test_name_pub_only(self): + settings = KeypairConfig(name='foo', public_filepath='/foo/bar.pub') + self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) + self.assertEqual('/foo/bar.pub', settings.public_filepath) + self.assertIsNone(settings.private_filepath) + self.assertIsNone(settings.delete_on_clean) + + def test_config_with_name_pub_only(self): + settings = KeypairConfig( + **{'name': 'foo', 'public_filepath': '/foo/bar.pub'}) + self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) + self.assertEqual('/foo/bar.pub', settings.public_filepath) + self.assertIsNone(settings.private_filepath) + self.assertIsNone(settings.delete_on_clean) + + def test_name_priv_only(self): + settings = KeypairConfig(name='foo', private_filepath='/foo/bar') + self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) + self.assertIsNone(settings.public_filepath) + self.assertEqual('/foo/bar', settings.private_filepath) + self.assertIsNone(settings.delete_on_clean) + + def test_config_with_name_priv_only(self): + settings = KeypairConfig( + **{'name': 'foo', 'private_filepath': '/foo/bar'}) + self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) + self.assertIsNone(settings.public_filepath) + self.assertEqual('/foo/bar', settings.private_filepath) + self.assertIsNone(settings.delete_on_clean) + + def test_all_delete_bool(self): + settings = KeypairConfig( + name='foo', public_filepath='/foo/bar.pub', + private_filepath='/foo/bar', delete_on_clean=True, + key_size=999) + self.assertEqual('foo', settings.name) + self.assertEqual(999, settings.key_size) + self.assertEqual('/foo/bar.pub', settings.public_filepath) + self.assertEqual('/foo/bar', settings.private_filepath) + self.assertTrue(settings.delete_on_clean) + + def test_all_delete_str_true_cap(self): + settings = KeypairConfig( + name='foo', public_filepath='/foo/bar.pub', + private_filepath='/foo/bar', delete_on_clean='True') + self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) + self.assertEqual('/foo/bar.pub', settings.public_filepath) + self.assertEqual('/foo/bar', settings.private_filepath) + self.assertTrue(settings.delete_on_clean) + + def test_all_delete_str_true_lc(self): + settings = KeypairConfig( + name='foo', public_filepath='/foo/bar.pub', + private_filepath='/foo/bar', delete_on_clean='true') + self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) + self.assertEqual('/foo/bar.pub', settings.public_filepath) + self.assertEqual('/foo/bar', settings.private_filepath) + self.assertTrue(settings.delete_on_clean) + + def test_all_delete_str_false_cap(self): + settings = KeypairConfig( + name='foo', public_filepath='/foo/bar.pub', + private_filepath='/foo/bar', delete_on_clean='False') + self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) + self.assertEqual('/foo/bar.pub', settings.public_filepath) + self.assertEqual('/foo/bar', settings.private_filepath) + self.assertFalse(settings.delete_on_clean) + + def test_all_delete_str_false_lc(self): + settings = KeypairConfig( + name='foo', public_filepath='/foo/bar.pub', + private_filepath='/foo/bar', delete_on_clean='false', + key_size='999') + self.assertEqual('foo', settings.name) + self.assertEqual(999, settings.key_size) + self.assertEqual('/foo/bar.pub', settings.public_filepath) + self.assertEqual('/foo/bar', settings.private_filepath) + self.assertFalse(settings.delete_on_clean) + + def test_config_all_delete_false_bool(self): + settings = KeypairConfig( + **{'name': 'foo', 'public_filepath': '/foo/bar.pub', + 'private_filepath': '/foo/bar', 'delete_on_clean': False, + 'key_size': 999}) + self.assertEqual('foo', settings.name) + self.assertEqual(999, settings.key_size) + self.assertEqual('/foo/bar.pub', settings.public_filepath) + self.assertEqual('/foo/bar', settings.private_filepath) + self.assertFalse(settings.delete_on_clean) + + def test_config_all_delete_false_str_cap(self): + settings = KeypairConfig( + **{'name': 'foo', 'public_filepath': '/foo/bar.pub', + 'private_filepath': '/foo/bar', 'delete_on_clean': 'False'}) + self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) + self.assertEqual('/foo/bar.pub', settings.public_filepath) + self.assertEqual('/foo/bar', settings.private_filepath) + self.assertFalse(settings.delete_on_clean) + + def test_config_all_delete_false_str_lc(self): + settings = KeypairConfig( + **{'name': 'foo', 'public_filepath': '/foo/bar.pub', + 'private_filepath': '/foo/bar', 'delete_on_clean': 'false'}) + self.assertEqual('foo', settings.name) + self.assertEqual(1024, settings.key_size) + self.assertEqual('/foo/bar.pub', settings.public_filepath) + self.assertEqual('/foo/bar', settings.private_filepath) + self.assertFalse(settings.delete_on_clean) + + def test_config_all_delete_false_str_foo(self): + settings = KeypairConfig( + **{'name': 'foo', 'public_filepath': '/foo/bar.pub', + 'private_filepath': '/foo/bar', 'delete_on_clean': 'foo', + 'key_size': '999'}) + self.assertEqual('foo', settings.name) + self.assertEqual(999, settings.key_size) + self.assertEqual('/foo/bar.pub', settings.public_filepath) + self.assertEqual('/foo/bar', settings.private_filepath) + self.assertFalse(settings.delete_on_clean) |