Cleanup and add tests
This commit is contained in:
parent
9a7df541c5
commit
0b5cf2c057
2 changed files with 192 additions and 107 deletions
|
|
@ -2,12 +2,14 @@ import oca
|
|||
import socket
|
||||
import logging
|
||||
|
||||
from oca.pool import WrongNameError
|
||||
from oca.exceptions import OpenNebulaException
|
||||
|
||||
from django.conf import settings
|
||||
from django.utils.functional import cached_property
|
||||
|
||||
from oca.pool import WrongNameError
|
||||
from oca.exceptions import OpenNebulaException
|
||||
from utils.models import CustomUser
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
|
|
@ -35,10 +37,33 @@ class OpenNebulaManager():
|
|||
)
|
||||
except:
|
||||
pass
|
||||
def _get_client(self, user):
|
||||
"""Get a opennebula client object for a CustomUser object
|
||||
|
||||
Args:
|
||||
user (CustomUser): dynamicweb CustomUser object
|
||||
|
||||
Returns:
|
||||
oca.Client: Opennebula client object
|
||||
|
||||
Raise:
|
||||
ConnectionError: If the connection to the opennebula server can't be
|
||||
established
|
||||
"""
|
||||
return oca.Client("{0}:{1}".format(
|
||||
user.email,
|
||||
user.password),
|
||||
"{protocol}://{domain}:{port}{endpoint}".format(
|
||||
protocol=settings.OPENNEBULA_PROTOCOL,
|
||||
domain=settings.OPENNEBULA_DOMAIN,
|
||||
port=settings.OPENNEBULA_PORT,
|
||||
endpoint=settings.OPENNEBULA_ENDPOINT
|
||||
))
|
||||
|
||||
def _get_opennebula_client(self, username, password):
|
||||
return oca.Client("{0}:{1}".format(
|
||||
username,
|
||||
|
||||
password),
|
||||
"{protocol}://{domain}:{port}{endpoint}".format(
|
||||
protocol=settings.OPENNEBULA_PROTOCOL,
|
||||
|
|
@ -47,6 +72,69 @@ class OpenNebulaManager():
|
|||
endpoint=settings.OPENNEBULA_ENDPOINT
|
||||
))
|
||||
|
||||
def _get_user(self, user):
|
||||
"""Get the corresponding opennebula user for a CustomUser object
|
||||
|
||||
Args:
|
||||
user (CustomUser): dynamicweb CustomUser object
|
||||
|
||||
Returns:
|
||||
oca.User: Opennebula user object
|
||||
|
||||
Raise:
|
||||
WrongNameError: If no openebula user with this credentials exists
|
||||
ConnectionError: If the connection to the opennebula server can't be
|
||||
established
|
||||
"""
|
||||
user_pool = self._get_user_pool()
|
||||
return user_pool.get_by_name(user.email)
|
||||
|
||||
def create_user(self, user: CustomUser):
|
||||
"""Create a new opennebula user or a corresponding CustomUser object
|
||||
|
||||
|
||||
Args:
|
||||
user (CustomUser): dynamicweb CustomUser object
|
||||
|
||||
Returns:
|
||||
int: Return the opennebula user id
|
||||
|
||||
Raises:
|
||||
ConnectionError: If the connection to the opennebula server can't be
|
||||
established
|
||||
UserExistsError: If a user with this credeintals already exits on the
|
||||
server
|
||||
UserCredentialError: If a user with this email exists but the
|
||||
password is worng
|
||||
|
||||
"""
|
||||
try:
|
||||
self._get_user(user)
|
||||
try:
|
||||
self._get_client(self, user)
|
||||
logger.debug('User already exists')
|
||||
raise UserExistsError()
|
||||
except OpenNebulaException as err:
|
||||
logger.error('OpenNebulaException error: {0}'.format(err))
|
||||
logger.debug('User exists but password is wrong')
|
||||
raise UserCredentialError()
|
||||
|
||||
except WrongNameError:
|
||||
user_id = self.oneadmin_client.call(oca.User.METHODS['allocate'],
|
||||
user.email, user.password, 'core')
|
||||
logger.debug('Created a user for CustomObject: {user} with user id = {u_id}',
|
||||
user=user,
|
||||
u_id=user_id
|
||||
)
|
||||
return user_id
|
||||
except ConnectionRefusedError:
|
||||
logger.error('Could not connect to host: {host} via protocol {protocol}'.format(
|
||||
host=settings.OPENNEBULA_DOMAIN,
|
||||
protocol=settings.OPENNEBULA_PROTOCOL)
|
||||
)
|
||||
raise ConnectionRefusedError
|
||||
|
||||
|
||||
def _get_or_create_user(self, email, password):
|
||||
try:
|
||||
user_pool = self._get_user_pool()
|
||||
|
|
@ -77,7 +165,7 @@ class OpenNebulaManager():
|
|||
host=settings.OPENNEBULA_DOMAIN,
|
||||
protocol=settings.OPENNEBULA_PROTOCOL)
|
||||
)
|
||||
raise ConnectionRefusedError
|
||||
raise
|
||||
return user_pool
|
||||
|
||||
def _get_vm_pool(self):
|
||||
|
|
@ -350,3 +438,42 @@ class OpenNebulaManager():
|
|||
self.opennebula_user.id,
|
||||
new_password
|
||||
)
|
||||
|
||||
def add_public_key(self, user, public_key='', replace=True):
|
||||
"""
|
||||
|
||||
Args:
|
||||
user (CustomUser): Dynamicweb user
|
||||
public_key (string): Public key to add to the user
|
||||
replace (bool): Optional if True the new public key replaces the old
|
||||
|
||||
Raises:
|
||||
KeyExistsError: If replace is False and the user already has a
|
||||
public key
|
||||
WrongNameError: If no openebula user with this credentials exists
|
||||
ConnectionError: If the connection to the opennebula server can't be
|
||||
established
|
||||
|
||||
Returns:
|
||||
True if public_key was added
|
||||
|
||||
"""
|
||||
# TODO: Check if we can remove this first try because we basically just
|
||||
# raise the possible Errors
|
||||
try:
|
||||
open_user = self._get_user(user)
|
||||
try:
|
||||
old_key = open_user.template.ssh_public_key
|
||||
if not replace:
|
||||
raise KeyExistsError()
|
||||
|
||||
except AttributeError:
|
||||
pass
|
||||
self.oneadmin_client.call('user.update', open_user.id,
|
||||
'<CONTEXT><SSH_PUBLIC_KEY>{key}</SSH_PUBLIC_KEY></CONTEXT>'.format(key=public_key))
|
||||
return True
|
||||
except WrongNameError:
|
||||
raise
|
||||
|
||||
except ConnectionError:
|
||||
raise
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue