From 0db15d99a5382dcfe6d641c867d15872d0dbf61c Mon Sep 17 00:00:00 2001
From: modulos <modulos@protonmail.com>
Date: Thu, 1 Jun 2017 19:08:38 +0200
Subject: [PATCH] Add exceptions, merge for ssh key

The replace variable of the add_public_key function is now a merge
variable in order to append new keys to a user
---
 opennebula_api/exceptions.py |  9 +++++++++
 opennebula_api/models.py     |  6 ++++--
 opennebula_api/tests.py      | 17 +++++++++++++++++
 3 files changed, 30 insertions(+), 2 deletions(-)
 create mode 100644 opennebula_api/exceptions.py

diff --git a/opennebula_api/exceptions.py b/opennebula_api/exceptions.py
new file mode 100644
index 00000000..2fa15b43
--- /dev/null
+++ b/opennebula_api/exceptions.py
@@ -0,0 +1,9 @@
+
+class KeyExistsError(Exception):
+    pass
+
+class UserExistsError(Exception):
+    pass
+
+class UserCredentialError(Exception):
+    pass
diff --git a/opennebula_api/models.py b/opennebula_api/models.py
index 423013e4..052291c1 100644
--- a/opennebula_api/models.py
+++ b/opennebula_api/models.py
@@ -9,6 +9,7 @@ from django.conf import settings
 from django.utils.functional import cached_property
 
 from utils.models import CustomUser
+from .exceptions import KeyExistsError, UserExistsError, UserCredentialError
 
 logger = logging.getLogger(__name__)
 
@@ -439,7 +440,7 @@ class OpenNebulaManager():
             new_password
         )
 
-    def add_public_key(self, user, public_key='', replace=True):
+    def add_public_key(self, user, public_key='', merge=False):
         """ 
 
         Args: 
@@ -464,8 +465,9 @@ class OpenNebulaManager():
             open_user = self._get_user(user)
             try:
                 old_key = open_user.template.ssh_public_key 
-                if not replace:
+                if not merge:
                     raise KeyExistsError()
+                public_key += '\n{key}'.format(key=old_key)
 
             except AttributeError:
                 pass
diff --git a/opennebula_api/tests.py b/opennebula_api/tests.py
index bd744a66..0d6ed4eb 100644
--- a/opennebula_api/tests.py
+++ b/opennebula_api/tests.py
@@ -81,6 +81,23 @@ class OpenNebulaManagerTestCases(TestCase):
 
         self.assertEqual(user_public_key, public_key)
         
+    def test_append_public_key_to_user(self):
+        """ Test the manager can append a new public key to an user """
+        self.manager.create_user(self.user)
+        user = self.manager._get_user(self.user)
+        public_key = 'test'
+        self.manager.add_public_key(self.user, public_key)
+        # Fetch new user information from opennebula
+        user.info()
+        old_public_key = user.template.ssh_public_key
+        self.manager.add_public_key(self.user, public_key, merge=True)
+        user.info()
+        new_public_key = user.template.ssh_public_key
+        # Cleanup 
+        user.delete()
+
+        self.assertEqual(new_public_key, '{}\n{}'.format(old_public_key,
+            public_key))
 
     def test_requires_ssh_key_for_new_vm(self):
         """Test the opennebula manager requires the user to have a ssh key when