1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-01-05 09:17:54 +03:00

Improving openstack, to allow more than one endpoint of a kind, and iterate over them in case of error

This commit is contained in:
Adolfo Gómez García 2024-03-07 23:20:00 +01:00
parent 14745e7f1b
commit ac116697f4
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
5 changed files with 593 additions and 302 deletions

View File

@ -53,7 +53,6 @@ class Cache:
misses = 0
_owner: str
_bowner: bytes
@staticmethod
def _basic_serialize(value: typing.Any) -> str:
@ -68,18 +67,17 @@ class Cache:
def __init__(self, owner: typing.Union[str, bytes]):
self._owner = owner.decode('utf-8') if isinstance(owner, bytes) else owner
self._bowner = self._owner.encode('utf8')
def __get_key(self, key: typing.Union[str, bytes]) -> str:
def _get_key(self, key: typing.Union[str, bytes]) -> str:
if isinstance(key, str):
key = key.encode('utf8')
return hash_key(self._bowner + key)
return hash_key(self._owner.encode() + key)
def get(self, skey: typing.Union[str, bytes], default: typing.Any = None) -> typing.Any:
now = sql_datetime()
# logger.debug('Requesting key "%s" for cache "%s"', skey, self._owner)
try:
key = self.__get_key(skey)
key = self._get_key(skey)
# logger.debug('Key: %s', key)
c: DBCache = DBCache.objects.get(owner=self._owner, pk=key)
# If expired
@ -133,7 +131,7 @@ class Cache:
"""
# logger.debug('Removing key "%s" for uService "%s"' % (skey, self._owner))
try:
key = self.__get_key(skey)
key = self._get_key(skey)
DBCache.objects.get(pk=key).delete() # @UndefinedVariable
return True
except DBCache.DoesNotExist: # @UndefinedVariable
@ -159,7 +157,7 @@ class Cache:
# logger.debug('Saving key "%s" for cache "%s"' % (skey, self._owner,))
if validity is None:
validity = consts.cache.DEFAULT_CACHE_TIMEOUT
key = self.__get_key(skey)
key = self._get_key(skey)
strValue = Cache._serializer(value)
now = sql_datetime()
# Remove existing if any and create a new one
@ -200,7 +198,7 @@ class Cache:
def refresh(self, skey: typing.Union[str, bytes]) -> None:
# logger.debug('Refreshing key "%s" for cache "%s"' % (skey, self._owner,))
try:
key = self.__get_key(skey)
key = self._get_key(skey)
c = DBCache.objects.get(pk=key)
c.created = sql_datetime()
c.save()

View File

@ -71,17 +71,17 @@ def get_resources(
'''
api, nameFromSubnets = getApi(parameters)
zones = [gui.choice_item(z, z) for z in api.listAvailabilityZones()]
zones = [gui.choice_item(z, z) for z in api.list_availability_zones()]
networks = [
gui.choice_item(z['id'], z['name'])
for z in api.listNetworks(nameFromSubnets=nameFromSubnets)
for z in api.list_networks(nameFromSubnets=nameFromSubnets)
]
flavors = [gui.choice_item(z['id'], z['name']) for z in api.listFlavors()]
flavors = [gui.choice_item(z['id'], z['name']) for z in api.list_flavors()]
securityGroups = [
gui.choice_item(z['id'], z['name']) for z in api.listSecurityGroups()
gui.choice_item(z['id'], z['name']) for z in api.list_security_groups()
]
volumeTypes = [gui.choice_item('-', _('None'))] + [
gui.choice_item(t['id'], t['name']) for t in api.listVolumeTypes()
gui.choice_item(t['id'], t['name']) for t in api.list_volume_types()
]
data: types.ui.CallbackResultType = [
@ -105,7 +105,7 @@ def get_volumes(
# Source volumes are all available for us
# volumes = [gui.choice_item(v['id'], v['name']) for v in api.listVolumes() if v['name'] != '' and v['availability_zone'] == parameters['availabilityZone']]
volumes = [
gui.choice_item(v['id'], v['name']) for v in api.listVolumes() if v['name'] != ''
gui.choice_item(v['id'], v['name']) for v in api.list_volumes() if v['name'] != ''
]
data: types.ui.CallbackResultType = [

View File

@ -50,7 +50,7 @@ if typing.TYPE_CHECKING:
logger = logging.getLogger(__name__)
INTERFACE_VALUES: list[types.ui.ChoiceItem] = [
INTERFACE_VALUES: typing.Final[list[types.ui.ChoiceItem]] = [
gui.choice_item('public', 'public'),
gui.choice_item('private', 'private'),
gui.choice_item('admin', 'admin'),

View File

@ -236,7 +236,7 @@ class OpenStackLiveService(services.Service):
gui.choice_item(parentCurrent.region.value, parentCurrent.region.value)
]
else:
regions = [gui.choice_item(r['id'], r['id']) for r in api.listRegions()]
regions = [gui.choice_item(r['id'], r['id']) for r in api.list_regions()]
self.region.set_choices(regions)
@ -245,7 +245,7 @@ class OpenStackLiveService(services.Service):
gui.choice_item(parentCurrent.tenant.value, parentCurrent.tenant.value)
]
else:
tenants = [gui.choice_item(t['id'], t['name']) for t in api.listProjects()]
tenants = [gui.choice_item(t['id'], t['name']) for t in api.list_projects()]
self.project.set_choices(tenants)
# So we can instantiate parent to get API
@ -340,7 +340,7 @@ class OpenStackLiveService(services.Service):
VERIFY_RESIZE. System is awaiting confirmation that the server is operational after a move or resize.
SHUTOFF. The server was powered down by the user, either through the OpenStack Compute API or from within the server. For example, the user issued a shutdown -h command from within the server. If the OpenStack Compute manager detects that the VM was powered down, it transitions the server to the SHUTOFF status.
"""
server = self.api.getServer(machineId)
server = self.api.get_server(machineId)
if server['status'] in ('ERROR', 'DELETED'):
logger.warning(
'Got server status %s for %s: %s',
@ -422,7 +422,7 @@ class OpenStackLiveService(services.Service):
"""
Gets the mac address of first nic of the machine
"""
net = self.api.getServer(machineid)['addresses']
net = self.api.get_server(machineid)['addresses']
vals = next(iter(net.values()))[
0
] # Returns "any" mac address of any interface. We just need only one interface info