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
This commit is contained in:
		
					parent
					
						
							
								ca63355914
							
						
					
				
			
			
				commit
				
					
						0db15d99a5
					
				
			
		
					 3 changed files with 30 additions and 2 deletions
				
			
		
							
								
								
									
										9
									
								
								opennebula_api/exceptions.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								opennebula_api/exceptions.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,9 @@ | |||
| 
 | ||||
| class KeyExistsError(Exception): | ||||
|     pass | ||||
| 
 | ||||
| class UserExistsError(Exception): | ||||
|     pass | ||||
| 
 | ||||
| class UserCredentialError(Exception): | ||||
|     pass | ||||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue