Added support for openstack showing subnets

This commit is contained in:
Adolfo Gómez García 2020-06-05 14:51:31 +02:00
parent 7b623bd986
commit 8d9b8d9234
5 changed files with 45 additions and 12 deletions

View File

@ -214,7 +214,7 @@ class Config:
try:
cfg = DBConfig.objects.filter(section=section, key=key)[0] # @UndefinedVariable
if checkType and cfg.field_type in (Config.READ_FIELD, Config.HIDDEN_FIELD):
return False# Skip non writable elements
return False # Skip non writable elements
if cfg.crypt is True:
value = cryptoManager().encrypt(value)

View File

@ -39,7 +39,7 @@ from . import openStack
logger = logging.getLogger(__name__)
def getApi(parameters: typing.Dict[str, str]) -> openStack.Client:
def getApi(parameters: typing.Dict[str, str]) -> typing.Tuple[openStack.Client, bool]:
from .provider_legacy import ProviderLegacy
from .provider import OpenStackProvider
from uds.core.environment import Environment
@ -53,17 +53,22 @@ def getApi(parameters: typing.Dict[str, str]) -> openStack.Client:
provider.unserialize(parameters['ov'])
return provider.api(parameters['project'], parameters['region'])
if isinstance(provider, OpenStackProvider):
useSubnetsName = provider.useSubnetsName.isTrue()
else:
useSubnetsName = False
return (provider.api(parameters['project'], parameters['region']), useSubnetsName)
def getResources(parameters: typing.Dict[str, str]) -> typing.List[typing.Dict[str, typing.Any]]:
'''
This helper is designed as a callback for Project Selector
'''
api = getApi(parameters)
api, nameFromSubnets = getApi(parameters)
zones = [gui.choiceItem(z, z) for z in api.listAvailabilityZones()]
networks = [gui.choiceItem(z['id'], z['name']) for z in api.listNetworks()]
networks = [gui.choiceItem(z['id'], z['name']) for z in api.listNetworks(nameFromSubnets=nameFromSubnets)]
flavors = [gui.choiceItem(z['id'], z['name']) for z in api.listFlavors()]
securityGroups = [gui.choiceItem(z['id'], z['name']) for z in api.listSecurityGroups()]
volumeTypes = [gui.choiceItem('-', _('None'))] + [gui.choiceItem(t['id'], t['name']) for t in api.listVolumeTypes()]
@ -82,7 +87,7 @@ def getVolumes(parameters: typing.Dict[str, str]) -> typing.List[typing.Dict[str
'''
This helper is designed as a callback for Zone Selector
'''
api = getApi(parameters)
api, _ = getApi(parameters)
# Source volumes are all available for us
# volumes = [gui.choiceItem(v['id'], v['name']) for v in api.listVolumes() if v['name'] != '' and v['availability_zone'] == parameters['availabilityZone']]
volumes = [gui.choiceItem(v['id'], v['name']) for v in api.listVolumes() if v['name'] != '']

View File

@ -358,14 +358,37 @@ class Client: # pylint: disable=too-many-public-methods
)
@authProjectRequired
def listNetworks(self) -> typing.Iterable[typing.Any]:
return getRecurringUrlJson(
def listNetworks(self, nameFromSubnets=False) -> typing.Iterable[typing.Any]:
nets = getRecurringUrlJson(
self._getEndpointFor('network') + '/v2.0/networks',
headers=self._requestHeaders(),
key='networks',
errMsg='List Networks',
timeout=self._timeout
)
if not nameFromSubnets:
yield from nets
else:
# Get and cache subnets names
subnetNames = { s['id']: s['name'] for s in self.listSubnets() }
for net in nets:
name = ','.join(subnetNames[i] for i in net['subnets'] if i in subnetNames)
net['old_name'] = net['name']
if name:
net['name'] = name
yield net
@authProjectRequired
def listSubnets(self) -> typing.Iterable[typing.Any]:
return getRecurringUrlJson(
self._getEndpointFor('network') + '/v2.0/subnets',
headers=self._requestHeaders(),
key='subnets',
errMsg='List Subnets',
timeout=self._timeout
)
@authProjectRequired
def listPorts(self, networkId: typing.Optional[str] = None, ownerId: typing.Optional[str] = None) -> typing.Iterable[typing.Any]:

View File

@ -110,6 +110,8 @@ class OpenStackProvider(ServiceProvider):
tenant = gui.TextField(length=64, label=_('Project Id'), order=6, tooltip=_('Project (tenant) for this provider. Set only if required by server.'), required=False, defvalue='', tab=gui.ADVANCED_TAB)
region = gui.TextField(length=64, label=_('Region'), order=7, tooltip=_('Region for this provider. Set only if required by server.'), required=False, defvalue='', tab=gui.ADVANCED_TAB)
useSubnetsName = gui.CheckBoxField(label=_('Subnets names'), order=8, tooltip=_('If checked, the name of the subnets will be used instead of the names of networks'), defvalue=gui.FALSE, tab=gui.ADVANCED_TAB)
legacy = False
# Own variables

View File

@ -68,7 +68,7 @@ class IPMachinesService(IPServiceBase):
ipList = gui.EditableList(label=_('List of servers'), tooltip=_('List of servers available for this service'))
port = gui.NumericField(length=5, label=_('Check Port'), defvalue='0', order=2, tooltip=_('If non zero, only hosts responding to connection on that port will be served.'), required=True, tab=gui.ADVANCED_TAB)
skipTimeOnFailure = gui.NumericField(length=6, label=_('Skip time'), defvalue='60', order=2, tooltip=_('If a host fails to check, skip it for this time (in minutes).'), minValue=0, required=True, tab=gui.ADVANCED_TAB)
skipTimeOnFailure = gui.NumericField(length=6, label=_('Skip time'), defvalue='15', order=2, tooltip=_('If a host fails to check, skip it for this time (in minutes).'), minValue=0, required=True, tab=gui.ADVANCED_TAB)
# Description of service
typeName = _('Static Multiple IP')
@ -90,6 +90,7 @@ class IPMachinesService(IPServiceBase):
_ips: typing.List[str] = []
_token: str = ''
_port: int = 0
_skipTimeOnFailure: int = 0
def initialize(self, values: 'Module.ValuesType') -> None:
if values is None:
@ -110,11 +111,11 @@ class IPMachinesService(IPServiceBase):
def valuesDict(self) -> gui.ValuesDictType:
ips = (i.split('~')[0] for i in self._ips)
return {'ipList': gui.convertToList(ips), 'token': self._token, 'port': str(self._port)}
return {'ipList': gui.convertToList(ips), 'token': self._token, 'port': str(self._port), 'skipTimeOnFailure': self._skipTimeOnFailure }
def marshal(self) -> bytes:
self.storage.saveData('ips', pickle.dumps(self._ips))
return b'\0'.join([b'v3', self._token.encode(), str(self._port).encode()])
return b'\0'.join([b'v4', self._token.encode(), str(self._port).encode(), str(self._skipTimeOnFailure).encode()])
def unmarshal(self, data: bytes) -> None:
values: typing.List[bytes] = data.split(b'\0')
@ -128,8 +129,10 @@ class IPMachinesService(IPServiceBase):
self._ips = []
if values[0] != b'v1':
self._token = values[1].decode()
if values[0] == b'v3':
if values[0] in (b'v3', b'v4'):
self._port = int(values[2].decode())
if values[0] == b'v4':
self._skipTimeOnFailure = int(values[3].decode())
def getUnassignedMachine(self) -> typing.Optional[str]:
# Search first unassigned machine