Created domain object for keypairs. 33/37033/3
authorspisarski <s.pisarski@cablelabs.com>
Thu, 6 Jul 2017 20:03:54 +0000 (14:03 -0600)
committerspisarski <s.pisarski@cablelabs.com>
Fri, 7 Jul 2017 14:19:06 +0000 (08:19 -0600)
OpenStack implementation details were leaking out into the
keypair creator.

JIRA: SNAPS-112

Change-Id: Idd22ca9f8ebec7b21c337ca003e01169efec34cb
Signed-off-by: spisarski <s.pisarski@cablelabs.com>
snaps/domain/keypair.py [new file with mode: 0644]
snaps/domain/test/keypair_tests.py [new file with mode: 0644]
snaps/openstack/utils/nova_utils.py
snaps/test_suite_builder.py

diff --git a/snaps/domain/keypair.py b/snaps/domain/keypair.py
new file mode 100644 (file)
index 0000000..2865125
--- /dev/null
@@ -0,0 +1,34 @@
+# 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.
+
+
+class Keypair:
+    """
+    SNAPS domain object for Keypairs. Should contain attributes that
+    are shared amongst cloud providers
+    """
+    def __init__(self, name, id, public_key):
+        """
+        Constructor
+        :param name: the keypair's name
+        :param id: the keypair's id
+        """
+        self.name = name
+        self.id = id
+        self.public_key = public_key
+
+    def __eq__(self, other):
+        return (self.name == other.name and self.id == other.id and
+                self.public_key == other.public_key)
diff --git a/snaps/domain/test/keypair_tests.py b/snaps/domain/test/keypair_tests.py
new file mode 100644 (file)
index 0000000..93f99ff
--- /dev/null
@@ -0,0 +1,35 @@
+# 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.domain.keypair import Keypair
+
+
+class KeypairDomainObjectTests(unittest.TestCase):
+    """
+    Tests the construction of the snaps.domain.test.Keypair class
+    """
+
+    def test_construction_positional(self):
+        keypair = Keypair('foo', '123-456', 'foo-bar')
+        self.assertEqual('foo', keypair.name)
+        self.assertEqual('123-456', keypair.id)
+        self.assertEqual('foo-bar', keypair.public_key)
+
+    def test_construction_named(self):
+        keypair = Keypair(public_key='foo-bar', id='123-456', name='foo')
+        self.assertEqual('foo', keypair.name)
+        self.assertEqual('123-456', keypair.id)
+        self.assertEqual('foo-bar', keypair.public_key)
index d6f9a19..db9a666 100644 (file)
@@ -23,6 +23,7 @@ from novaclient.client import Client
 from novaclient.exceptions import NotFound
 
 from snaps.domain.flavor import Flavor
+from snaps.domain.keypair import Keypair
 from snaps.domain.vm_inst import VmInst
 from snaps.openstack.utils import keystone_utils, glance_utils, neutron_utils
 
@@ -224,7 +225,8 @@ def upload_keypair(nova, name, key):
     :return: the keypair object
     """
     logger.info('Creating keypair with name - ' + name)
-    return nova.keypairs.create(name=name, public_key=key.decode('utf-8'))
+    os_kp = nova.keypairs.create(name=name, public_key=key.decode('utf-8'))
+    return Keypair(name=os_kp.name, id=os_kp.id, public_key=os_kp.public_key)
 
 
 def keypair_exists(nova, keypair_obj):
@@ -235,7 +237,8 @@ def keypair_exists(nova, keypair_obj):
     :return: the keypair object or None if not found
     """
     try:
-        return nova.keypairs.get(keypair_obj)
+        os_kp = nova.keypairs.get(keypair_obj)
+        return Keypair(name=os_kp.name, id=os_kp.id, public_key=os_kp.public_key)
     except:
         return None
 
@@ -251,7 +254,8 @@ def get_keypair_by_name(nova, name):
 
     for keypair in keypairs:
         if keypair.name == name:
-            return keypair
+            return Keypair(name=keypair.name, id=keypair.id,
+                           public_key=keypair.public_key)
 
     return None
 
@@ -260,10 +264,10 @@ def delete_keypair(nova, key):
     """
     Deletes a keypair object from OpenStack
     :param nova: the Nova client
-    :param key: the keypair object to delete
+    :param key: the SNAPS-OO keypair domain object to delete
     """
     logger.debug('Deleting keypair - ' + key.name)
-    nova.keypairs.delete(key)
+    nova.keypairs.delete(key.id)
 
 
 def get_nova_availability_zones(nova):
index eb6e65a..e3389a9 100644 (file)
@@ -18,6 +18,7 @@ import unittest
 
 from snaps.domain.test.flavor_tests import FlavorDomainObjectTests
 from snaps.domain.test.image_tests import ImageDomainObjectTests
+from snaps.domain.test.keypair_tests import KeypairDomainObjectTests
 from snaps.domain.test.stack_tests import StackDomainObjectTests
 from snaps.domain.test.vm_inst_tests import (VmInstDomainObjectTests,
                                              FloatingIpDomainObjectTests)
@@ -99,6 +100,8 @@ def add_unit_tests(suite):
         FlavorDomainObjectTests))
     suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
         KeypairSettingsUnitTests))
+    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
+        KeypairDomainObjectTests))
     suite.addTest(unittest.TestLoader().loadTestsFromTestCase(
         UserSettingsUnitTests))
     suite.addTest(unittest.TestLoader().loadTestsFromTestCase(