From 88c180252d78226e06ecf53a23d4f0eca9815bb8 Mon Sep 17 00:00:00 2001 From: spisarski Date: Fri, 17 Nov 2017 08:35:45 -0700 Subject: [PATCH] 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 --- docs/how-to-use/LibraryUsage.rst | 9 +- docs/how-to-use/UnitTests.rst | 8 +- examples/launch.py | 5 +- snaps/config/keypair.py | 61 +++++++ snaps/config/tests/keypair_tests.py | 179 +++++++++++++++++++++ snaps/openstack/create_keypairs.py | 48 +----- snaps/openstack/create_stack.py | 4 +- snaps/openstack/tests/create_instance_tests.py | 9 +- snaps/openstack/tests/create_keypairs_tests.py | 35 ++-- snaps/openstack/utils/deploy_utils.py | 2 +- snaps/openstack/utils/nova_utils.py | 48 +++--- snaps/openstack/utils/settings_utils.py | 28 ++-- snaps/openstack/utils/tests/heat_utils_tests.py | 4 +- .../openstack/utils/tests/settings_utils_tests.py | 3 +- snaps/provisioning/tests/ansible_utils_tests.py | 4 +- snaps/test_suite_builder.py | 3 + 16 files changed, 336 insertions(+), 114 deletions(-) create mode 100644 snaps/config/keypair.py create mode 100644 snaps/config/tests/keypair_tests.py diff --git a/docs/how-to-use/LibraryUsage.rst b/docs/how-to-use/LibraryUsage.rst index c6b40a1..c45cc83 100644 --- a/docs/how-to-use/LibraryUsage.rst +++ b/docs/how-to-use/LibraryUsage.rst @@ -211,7 +211,7 @@ Create Keypair -------------- - Keypair - snaps.openstack.create\_keypair.OpenStackKeypair - - snaps.openstack.create\_keypair.KeypairSettings + - snaps.openstack.keypair.KeypairConfig - name - the keypair name (required) - public\_filepath - the file location to where the public key is @@ -227,8 +227,9 @@ Create Keypair .. code:: python - from snaps.openstack.create_keypairs import KeypairSettings, OpenStackKeypair - keypair_settings = KeypairSettings(name='kepair-name', private_filepath='/tmp/priv-kp') + from snaps.openstack.keypair.KeypairConfig + from snaps.openstack.create_keypairs import OpenStackKeypair + keypair_settings = KeypairConfig(name='kepair-name', private_filepath='/tmp/priv-kp') keypair_creator = OpenStackKeypair(os_creds, keypair_settings) keypair_creator.create() @@ -576,7 +577,7 @@ Create VM Instance - image\_settings - see snaps.config.image.ImageConfig above (required) - keypair\_settings - see - snaps.openstack.create\_keypairs.KeypairSettings above (optional) + snaps.openstack.keypair.KeypairConfig above (optional) .. code:: python diff --git a/docs/how-to-use/UnitTests.rst b/docs/how-to-use/UnitTests.rst index 398a08e..013b646 100644 --- a/docs/how-to-use/UnitTests.rst +++ b/docs/how-to-use/UnitTests.rst @@ -96,11 +96,17 @@ FlavorDomainObjectTests Ensures that all required members are included when constructing a Flavor domain object +KeypairConfigUnitTests +---------------------- + +Ensures that all required members are included when constructing a +KeypairConfig object + KeypairSettingsUnitTests ------------------------ Ensures that all required members are included when constructing a -KeypairSettings object +deprecated KeypairSettings object KeypairDomainObjectTests ------------------------ diff --git a/examples/launch.py b/examples/launch.py index 499d259..4d2a69f 100644 --- a/examples/launch.py +++ b/examples/launch.py @@ -28,10 +28,11 @@ import yaml from snaps import file_utils from snaps.config.flavor import FlavorConfig from snaps.config.image import ImageConfig +from snaps.config.keypair import KeypairConfig from snaps.openstack.create_flavor import OpenStackFlavor from snaps.openstack.create_image import OpenStackImage from snaps.openstack.create_instance import VmInstanceSettings -from snaps.openstack.create_keypairs import KeypairSettings, OpenStackKeypair +from snaps.openstack.create_keypairs import OpenStackKeypair from snaps.openstack.create_network import ( PortSettings, NetworkSettings, OpenStackNetwork) from snaps.openstack.create_project import OpenStackProject, ProjectSettings @@ -677,7 +678,7 @@ def main(arguments): # Create keypairs keypairs_dict = __create_instances( - os_creds_dict, OpenStackKeypair, KeypairSettings, + os_creds_dict, OpenStackKeypair, KeypairConfig, os_config.get('keypairs'), 'keypair', clean, users_dict) creators.append(keypairs_dict) 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) diff --git a/snaps/openstack/create_keypairs.py b/snaps/openstack/create_keypairs.py index 6c66134..a181a7b 100644 --- a/snaps/openstack/create_keypairs.py +++ b/snaps/openstack/create_keypairs.py @@ -15,10 +15,10 @@ import logging import os -from neutronclient.common.utils import str2bool from novaclient.exceptions import NotFound from snaps import file_utils +from snaps.config.keypair import KeypairConfig from snaps.openstack.openstack_creator import OpenStackComputeObject from snaps.openstack.utils import nova_utils @@ -36,7 +36,7 @@ class OpenStackKeypair(OpenStackComputeObject): """ Constructor - all parameters are required :param os_creds: The credentials to connect with OpenStack - :param keypair_settings: The settings used to create a keypair + :param keypair_settings: a KeypairConfig object """ super(self.__class__, self).__init__(os_creds) @@ -141,47 +141,13 @@ class OpenStackKeypair(OpenStackComputeObject): return self.__keypair -class KeypairSettings: +class KeypairSettings(KeypairConfig): """ 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 KeypairSettingsError('Name is a required attribute') - - if self.key_size < 512: - raise KeypairSettingsError('key_size must be >=512') - - -class KeypairSettingsError(Exception): - """ - Exception to be thrown when keypair settings are incorrect - """ + from warnings import warn + warn('Use snaps.config.keypair.KeypairConfig instead', + DeprecationWarning) + super(self.__class__, self).__init__(**kwargs) diff --git a/snaps/openstack/create_stack.py b/snaps/openstack/create_stack.py index aebe52a..4ed293d 100644 --- a/snaps/openstack/create_stack.py +++ b/snaps/openstack/create_stack.py @@ -58,7 +58,7 @@ class OpenStackHeatStack(OpenStackCloudObject, object): :param stack_settings: The stack settings :param image_settings: A list of ImageConfig objects that were used for spawning this stack - :param keypair_settings: A list of KeypairSettings objects that were + :param keypair_settings: A list of KeypairConfig objects that were used for spawning this stack :return: """ @@ -299,7 +299,7 @@ class OpenStackHeatStack(OpenStackCloudObject, object): nova, neutron, stack_server) image_settings = settings_utils.determine_image_config( glance, stack_server, self.image_settings) - keypair_settings = settings_utils.determine_keypair_settings( + keypair_settings = settings_utils.determine_keypair_config( self.__heat_cli, self.__stack, stack_server, keypair_settings=self.keypair_settings, priv_key_key=heat_keypair_option) diff --git a/snaps/openstack/tests/create_instance_tests.py b/snaps/openstack/tests/create_instance_tests.py index 7d0c83a..0e34caa 100644 --- a/snaps/openstack/tests/create_instance_tests.py +++ b/snaps/openstack/tests/create_instance_tests.py @@ -24,6 +24,7 @@ from neutronclient.common.exceptions import InvalidIpForSubnetClient from novaclient.exceptions import BadRequest from snaps import file_utils +from snaps.config.keypair import KeypairConfig from snaps.openstack import create_network, create_router from snaps.config.flavor import FlavorConfig from snaps.openstack.create_flavor import OpenStackFlavor @@ -32,7 +33,7 @@ from snaps.openstack.create_image import OpenStackImage from snaps.openstack.create_instance import ( VmInstanceSettings, OpenStackVmInstance, FloatingIpSettings, VmInstanceSettingsError, FloatingIpSettingsError) -from snaps.openstack.create_keypairs import OpenStackKeypair, KeypairSettings +from snaps.openstack.create_keypairs import OpenStackKeypair from snaps.openstack.create_network import ( OpenStackNetwork, PortSettings, NetworkSettings, SubnetSettings) from snaps.openstack.create_router import OpenStackRouter, RouterSettings @@ -573,7 +574,7 @@ class CreateInstanceSingleNetworkTests(OSIntegrationTestCase): self.flavor_creator.create() self.keypair_creator = OpenStackKeypair( - self.os_creds, KeypairSettings( + self.os_creds, KeypairConfig( name=self.keypair_name, public_filepath=self.keypair_pub_filepath, private_filepath=self.keypair_priv_filepath)) @@ -852,7 +853,7 @@ class CreateInstanceIPv6NetworkTests(OSIntegrationTestCase): self.flavor_creator.create() self.keypair_creator = OpenStackKeypair( - self.os_creds, KeypairSettings( + self.os_creds, KeypairConfig( name=self.keypair_name, public_filepath=self.keypair_pub_filepath, private_filepath=self.keypair_priv_filepath)) @@ -1542,7 +1543,7 @@ class CreateInstancePubPrivNetTests(OSIntegrationTestCase): # Create Keypair self.keypair_creator = OpenStackKeypair( - self.os_creds, KeypairSettings( + self.os_creds, KeypairConfig( name=self.keypair_name, public_filepath=self.keypair_pub_filepath, private_filepath=self.keypair_priv_filepath)) diff --git a/snaps/openstack/tests/create_keypairs_tests.py b/snaps/openstack/tests/create_keypairs_tests.py index d2de6fe..63e0bcc 100644 --- a/snaps/openstack/tests/create_keypairs_tests.py +++ b/snaps/openstack/tests/create_keypairs_tests.py @@ -18,8 +18,9 @@ import uuid import os from snaps import file_utils +from snaps.config.keypair import KeypairConfig, KeypairConfigError from snaps.openstack.create_keypairs import ( - KeypairSettings, OpenStackKeypair, KeypairSettingsError) + KeypairSettings, OpenStackKeypair) from snaps.openstack.tests.os_source_file_test import OSIntegrationTestCase from snaps.openstack.utils import nova_utils @@ -32,15 +33,15 @@ class KeypairSettingsUnitTests(unittest.TestCase): """ def test_no_params(self): - with self.assertRaises(KeypairSettingsError): + with self.assertRaises(KeypairConfigError): KeypairSettings() def test_empty_config(self): - with self.assertRaises(KeypairSettingsError): + with self.assertRaises(KeypairConfigError): KeypairSettings(**dict()) def test_small_key_size(self): - with self.assertRaises(KeypairSettingsError): + with self.assertRaises(KeypairConfigError): KeypairSettings(name='foo', key_size=511) def test_name_only(self): @@ -228,7 +229,7 @@ class CreateKeypairsTests(OSIntegrationTestCase): Tests the creation of a generated keypair without saving to file :return: """ - self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings( + self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairConfig( name=self.keypair_name)) self.keypair_creator.create() @@ -241,7 +242,7 @@ class CreateKeypairsTests(OSIntegrationTestCase): Tests the creation of a generated keypair without saving to file :return: """ - self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings( + self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairConfig( name=self.keypair_name, key_size=10000)) self.keypair_creator.create() @@ -255,7 +256,7 @@ class CreateKeypairsTests(OSIntegrationTestCase): clean() does not raise an Exception. """ # Create Image - self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairSettings( + self.keypair_creator = OpenStackKeypair(self.os_creds, KeypairConfig( name=self.keypair_name)) created_keypair = self.keypair_creator.create() self.assertIsNotNone(created_keypair) @@ -277,8 +278,8 @@ class CreateKeypairsTests(OSIntegrationTestCase): :return: """ self.keypair_creator = OpenStackKeypair( - self.os_creds, KeypairSettings(name=self.keypair_name, - public_filepath=self.pub_file_path)) + self.os_creds, KeypairConfig( + name=self.keypair_name, public_filepath=self.pub_file_path)) self.keypair_creator.create() keypair = nova_utils.keypair_exists(self.nova, @@ -302,7 +303,7 @@ class CreateKeypairsTests(OSIntegrationTestCase): :return: """ self.keypair_creator = OpenStackKeypair( - self.os_creds, KeypairSettings( + self.os_creds, KeypairConfig( name=self.keypair_name, public_filepath=self.pub_file_path, private_filepath=self.priv_file_path)) self.keypair_creator.create() @@ -335,8 +336,8 @@ class CreateKeypairsTests(OSIntegrationTestCase): file_utils.save_keys_to_files(keys=keys, pub_file_path=self.pub_file_path) self.keypair_creator = OpenStackKeypair( - self.os_creds, KeypairSettings(name=self.keypair_name, - public_filepath=self.pub_file_path)) + self.os_creds, KeypairConfig( + name=self.keypair_name, public_filepath=self.pub_file_path)) self.keypair_creator.create() keypair = nova_utils.keypair_exists(self.nova, @@ -400,7 +401,7 @@ class CreateKeypairsCleanupTests(OSIntegrationTestCase): :return: """ self.keypair_creator = OpenStackKeypair( - self.os_creds, KeypairSettings( + self.os_creds, KeypairConfig( name=self.keypair_name, public_filepath=self.pub_file_path, private_filepath=self.priv_file_path)) self.keypair_creator.create() @@ -416,7 +417,7 @@ class CreateKeypairsCleanupTests(OSIntegrationTestCase): :return: """ self.keypair_creator = OpenStackKeypair( - self.os_creds, KeypairSettings( + self.os_creds, KeypairConfig( name=self.keypair_name, public_filepath=self.pub_file_path, private_filepath=self.priv_file_path, delete_on_clean=True)) self.keypair_creator.create() @@ -432,7 +433,7 @@ class CreateKeypairsCleanupTests(OSIntegrationTestCase): :return: """ self.keypair_creator = OpenStackKeypair( - self.os_creds, KeypairSettings( + self.os_creds, KeypairConfig( name=self.keypair_name, public_filepath=self.pub_file_path, private_filepath=self.priv_file_path, delete_on_clean=False)) self.keypair_creator.create() @@ -452,7 +453,7 @@ class CreateKeypairsCleanupTests(OSIntegrationTestCase): keys=keys, pub_file_path=self.pub_file_path, priv_file_path=self.priv_file_path) self.keypair_creator = OpenStackKeypair( - self.os_creds, KeypairSettings( + self.os_creds, KeypairConfig( name=self.keypair_name, public_filepath=self.pub_file_path, private_filepath=self.priv_file_path, delete_on_clean=False)) self.keypair_creator.create() @@ -472,7 +473,7 @@ class CreateKeypairsCleanupTests(OSIntegrationTestCase): keys=keys, pub_file_path=self.pub_file_path, priv_file_path=self.priv_file_path) self.keypair_creator = OpenStackKeypair( - self.os_creds, KeypairSettings( + self.os_creds, KeypairConfig( name=self.keypair_name, public_filepath=self.pub_file_path, private_filepath=self.priv_file_path, delete_on_clean=True)) self.keypair_creator.create() diff --git a/snaps/openstack/utils/deploy_utils.py b/snaps/openstack/utils/deploy_utils.py index c936c1f..028da84 100644 --- a/snaps/openstack/utils/deploy_utils.py +++ b/snaps/openstack/utils/deploy_utils.py @@ -103,7 +103,7 @@ def create_keypair(os_creds, keypair_settings, cleanup=False): """ Creates a keypair that can be applied to an instance :param os_creds: The OpenStack credentials object - :param keypair_settings: The KeypairSettings object + :param keypair_settings: The KeypairConfig object :param cleanup: Denotes whether or not this is being called for cleanup :return: A reference to the keypair creator object """ diff --git a/snaps/openstack/utils/nova_utils.py b/snaps/openstack/utils/nova_utils.py index 0820289..b974f2e 100644 --- a/snaps/openstack/utils/nova_utils.py +++ b/snaps/openstack/utils/nova_utils.py @@ -52,22 +52,22 @@ def nova_client(os_creds): region_name=os_creds.region_name) -def create_server(nova, neutron, glance, instance_settings, image_settings, - keypair_settings=None): +def create_server(nova, neutron, glance, instance_config, image_config, + keypair_config=None): """ Creates a VM instance :param nova: the nova client (required) :param neutron: the neutron client for retrieving ports (required) :param glance: the glance client (required) - :param instance_settings: the VM instance settings object (required) - :param image_settings: the VM's image settings object (required) - :param keypair_settings: the VM's keypair settings object (optional) + :param instance_config: the VMInstConfig object (required) + :param image_config: the VM's ImageConfig object (required) + :param keypair_config: the VM's KeypairConfig object (optional) :return: a snaps.domain.VmInst object """ ports = list() - for port_setting in instance_settings.port_settings: + for port_setting in instance_config.port_settings: ports.append(neutron_utils.get_port( neutron, port_settings=port_setting)) nics = [] @@ -76,41 +76,41 @@ def create_server(nova, neutron, glance, instance_settings, image_settings, kv['port-id'] = port.id nics.append(kv) - logger.info('Creating VM with name - ' + instance_settings.name) + logger.info('Creating VM with name - ' + instance_config.name) keypair_name = None - if keypair_settings: - keypair_name = keypair_settings.name + if keypair_config: + keypair_name = keypair_config.name - flavor = get_flavor_by_name(nova, instance_settings.flavor) + flavor = get_flavor_by_name(nova, instance_config.flavor) if not flavor: raise NovaException( - 'Flavor not found with name - %s', instance_settings.flavor) + 'Flavor not found with name - %s', instance_config.flavor) - image = glance_utils.get_image(glance, image_settings=image_settings) + image = glance_utils.get_image(glance, image_settings=image_config) if image: userdata = None - if instance_settings.userdata: - if isinstance(instance_settings.userdata, str): - userdata = instance_settings.userdata + '\n' - elif (isinstance(instance_settings.userdata, dict) and - 'script_file' in instance_settings.userdata): + if instance_config.userdata: + if isinstance(instance_config.userdata, str): + userdata = instance_config.userdata + '\n' + elif (isinstance(instance_config.userdata, dict) and + 'script_file' in instance_config.userdata): try: userdata = file_utils.read_file( - instance_settings.userdata['script_file']) + instance_config.userdata['script_file']) except Exception as e: logger.warn('error reading userdata file %s - %s', - instance_settings.userdata, e) - args = {'name': instance_settings.name, + instance_config.userdata, e) + args = {'name': instance_config.name, 'flavor': flavor, 'image': image, 'nics': nics, 'key_name': keypair_name, 'security_groups': - instance_settings.security_group_names, + instance_config.security_group_names, 'userdata': userdata} - if instance_settings.availability_zone: - args['availability_zone'] = instance_settings.availability_zone + if instance_config.availability_zone: + args['availability_zone'] = instance_config.availability_zone server = nova.servers.create(**args) @@ -118,7 +118,7 @@ def create_server(nova, neutron, glance, instance_settings, image_settings, else: raise NovaException( 'Cannot create instance, image cannot be located with name %s', - image_settings.name) + image_config.name) def get_server(nova, vm_inst_settings=None, server_name=None): diff --git a/snaps/openstack/utils/settings_utils.py b/snaps/openstack/utils/settings_utils.py index 0619f15..cdcdeab 100644 --- a/snaps/openstack/utils/settings_utils.py +++ b/snaps/openstack/utils/settings_utils.py @@ -16,9 +16,9 @@ import uuid from snaps import file_utils from snaps.config.flavor import FlavorConfig +from snaps.config.keypair import KeypairConfig from snaps.openstack.create_instance import ( VmInstanceSettings, FloatingIpSettings) -from snaps.openstack.create_keypairs import KeypairSettings from snaps.openstack.create_network import ( PortSettings, SubnetSettings, NetworkSettings) from snaps.openstack.create_security_group import ( @@ -205,13 +205,13 @@ def create_flavor_config(flavor): def create_keypair_settings(heat_cli, stack, keypair, pk_output_key): """ - Instantiates a KeypairSettings object from a Keypair domain objects + Instantiates a KeypairConfig object from a Keypair domain objects :param heat_cli: the heat client :param stack: the Stack domain object :param keypair: the Keypair SNAPS domain object :param pk_output_key: the key to the heat template's outputs for retrieval of the private key file - :return: a KeypairSettings object + :return: a KeypairConfig object """ if pk_output_key: outputs = heat_utils.get_outputs(heat_cli, stack) @@ -222,11 +222,11 @@ def create_keypair_settings(heat_cli, stack, keypair, pk_output_key): key_file = file_utils.save_string_to_file( output.value, str(guid), 0o400) - # Use outputs, file and resources for the KeypairSettings - return KeypairSettings( + # Use outputs, file and resources for the KeypairConfig + return KeypairConfig( name=keypair.name, private_filepath=key_file.name) - return KeypairSettings(name=keypair.name) + return KeypairConfig(name=keypair.name) def create_vm_inst_settings(nova, neutron, server): @@ -364,20 +364,20 @@ def determine_image_config(glance, server, image_settings): return image_setting -def determine_keypair_settings(heat_cli, stack, server, keypair_settings=None, - priv_key_key=None): +def determine_keypair_config(heat_cli, stack, server, keypair_settings=None, + priv_key_key=None): """ - Returns a KeypairSettings object from the list that matches the + Returns a KeypairConfig object from the list that matches the server.keypair_name value in the keypair_settings parameter if not None, else if the output_key is not None, the output's value when contains the string 'BEGIN RSA PRIVATE KEY', this value will be stored into a file and - encoded into the KeypairSettings object returned + encoded into the KeypairConfig object returned :param heat_cli: the OpenStack heat client :param stack: a SNAPS-OO Stack domain object :param server: a SNAPS-OO VmInst domain object - :param keypair_settings: list of KeypairSettings objects + :param keypair_settings: list of KeypairConfig objects :param priv_key_key: the stack options that holds the private key value - :return: KeypairSettings or None + :return: KeypairConfig or None """ # Existing keypair being used by Heat Template if keypair_settings: @@ -395,6 +395,6 @@ def determine_keypair_settings(heat_cli, stack, server, keypair_settings=None, key_file = file_utils.save_string_to_file( output.value, str(guid), 0o400) - # Use outputs, file and resources for the KeypairSettings - return KeypairSettings( + # Use outputs, file and resources for the KeypairConfig + return KeypairConfig( name=server.keypair_name, private_filepath=key_file.name) diff --git a/snaps/openstack/utils/tests/heat_utils_tests.py b/snaps/openstack/utils/tests/heat_utils_tests.py index d3d05a7..a5b684f 100644 --- a/snaps/openstack/utils/tests/heat_utils_tests.py +++ b/snaps/openstack/utils/tests/heat_utils_tests.py @@ -410,13 +410,13 @@ class HeatUtilsCreateComplexStackTests(OSComponentTestCase): self.assertEqual( self.image_creator2.image_settings.name, image_settings.name) - self.keypair1_settings = settings_utils.determine_keypair_settings( + self.keypair1_settings = settings_utils.determine_keypair_config( self.heat_client, self.stack, servers[0], priv_key_key='private_key') self.assertIsNotNone(self.keypair1_settings) self.assertEqual(self.keypair_name, self.keypair1_settings.name) - self.keypair2_settings = settings_utils.determine_keypair_settings( + self.keypair2_settings = settings_utils.determine_keypair_config( self.heat_client, self.stack, servers[1], priv_key_key='private_key') self.assertIsNotNone(self.keypair2_settings) diff --git a/snaps/openstack/utils/tests/settings_utils_tests.py b/snaps/openstack/utils/tests/settings_utils_tests.py index 97dfd3c..3fba4fc 100644 --- a/snaps/openstack/utils/tests/settings_utils_tests.py +++ b/snaps/openstack/utils/tests/settings_utils_tests.py @@ -19,6 +19,7 @@ import os import uuid from snaps.config.flavor import FlavorConfig +from snaps.config.keypair import KeypairConfig from snaps.domain.flavor import Flavor from snaps.domain.volume import ( Volume, VolumeType, VolumeTypeEncryption, QoSSpec) @@ -207,7 +208,7 @@ class SettingsUtilsVmInstTests(OSComponentTestCase): # Create Key/Pair self.keypair_creator = create_keypairs.OpenStackKeypair( - self.os_creds, create_keypairs.KeypairSettings( + self.os_creds, KeypairConfig( name=self.keypair_name, public_filepath=self.keypair_pub_filepath, private_filepath=self.keypair_priv_filepath)) diff --git a/snaps/provisioning/tests/ansible_utils_tests.py b/snaps/provisioning/tests/ansible_utils_tests.py index ef8c83f..f50b7de 100644 --- a/snaps/provisioning/tests/ansible_utils_tests.py +++ b/snaps/provisioning/tests/ansible_utils_tests.py @@ -19,6 +19,8 @@ import os import pkg_resources from scp import SCPClient +from snaps.config.keypair import KeypairConfig + from snaps.config.flavor import FlavorConfig from snaps.openstack import create_flavor from snaps.openstack import create_image @@ -109,7 +111,7 @@ class AnsibleProvisioningTests(OSIntegrationTestCase): # Create Key/Pair self.keypair_creator = create_keypairs.OpenStackKeypair( - self.os_creds, create_keypairs.KeypairSettings( + self.os_creds, KeypairConfig( name=self.keypair_name, public_filepath=self.keypair_pub_filepath, private_filepath=self.keypair_priv_filepath)) diff --git a/snaps/test_suite_builder.py b/snaps/test_suite_builder.py index 6747b91..3f8f5c5 100644 --- a/snaps/test_suite_builder.py +++ b/snaps/test_suite_builder.py @@ -16,6 +16,7 @@ import logging import unittest +from snaps.config.tests.keypair_tests import KeypairConfigUnitTests from snaps.config.tests.flavor_tests import FlavorConfigUnitTests import snaps.config.tests.image_tests as image_tests import snaps.openstack.tests.create_image_tests as creator_tests @@ -146,6 +147,8 @@ def add_unit_tests(suite): FlavorSettingsUnitTests)) suite.addTest(unittest.TestLoader().loadTestsFromTestCase( FlavorDomainObjectTests)) + suite.addTest(unittest.TestLoader().loadTestsFromTestCase( + KeypairConfigUnitTests)) suite.addTest(unittest.TestLoader().loadTestsFromTestCase( KeypairSettingsUnitTests)) suite.addTest(unittest.TestLoader().loadTestsFromTestCase( -- 2.16.6