Backporting transport limitation by connecting os

This commit is contained in:
Adolfo Gómez García 2016-10-10 08:31:32 +02:00
parent 7892a196ac
commit 49aaaedbe9
3 changed files with 52 additions and 20 deletions

View File

@ -36,6 +36,7 @@ from django.utils.translation import ugettext_lazy as _, ugettext
from uds.models import Transport, Network from uds.models import Transport, Network
from uds.core.transports import factory from uds.core.transports import factory
from uds.core.util import permissions from uds.core.util import permissions
from uds.core.util import OsDetector
from uds.REST.model import ModelHandler from uds.REST.model import ModelHandler
@ -48,14 +49,14 @@ logger = logging.getLogger(__name__)
class Transports(ModelHandler): class Transports(ModelHandler):
model = Transport model = Transport
save_fields = ['name', 'comments', 'tags', 'priority', 'nets_positive'] save_fields = ['name', 'comments', 'tags', 'priority', 'nets_positive', 'allowed_oss']
table_title = _('Current Transports') table_title = _('Current Transports')
table_fields = [ table_fields = [
{'priority': {'title': _('Priority'), 'type': 'numeric', 'width': '6em'}}, {'priority': {'title': _('Priority'), 'type': 'numeric', 'width': '6em'}},
{'name': {'title': _('Name'), 'visible': True, 'type': 'iconType'}}, {'name': {'title': _('Name'), 'visible': True, 'type': 'iconType'}},
{'comments': {'title': _('Comments')}}, {'comments': {'title': _('Comments')}},
{'deployed_count': {'title': _('Used by'), 'type': 'numeric', 'width': '8em'}}, {'allowed_oss': {'title': _('Devices'), 'width': '8em'}},
{'tags': {'title': _('tags'), 'visible': False}}, {'tags': {'title': _('tags'), 'visible': False}},
] ]
@ -65,22 +66,32 @@ class Transports(ModelHandler):
def getGui(self, type_): def getGui(self, type_):
try: try:
return self.addField( return self.addField(
self.addField(self.addDefaultFields(factory().lookup(type_).guiDescription(), ['name', 'comments', 'tags', 'priority']), { self.addField(
'name': 'nets_positive', self.addField(self.addDefaultFields(factory().lookup(type_).guiDescription(), ['name', 'comments', 'tags', 'priority']), {
'value': True, 'name': 'nets_positive',
'label': ugettext('Network access'), 'value': True,
'tooltip': ugettext('If checked, the transport will be enabled for the selected networks.If unchecked, transport will be disabled for selected networks'), 'label': ugettext('Network access'),
'type': 'checkbox', 'tooltip': ugettext('If checked, the transport will be enabled for the selected networks.If unchecked, transport will be disabled for selected networks'),
'order': 100, # At end 'type': 'checkbox',
}), { 'order': 100, # At end
'name': 'networks', }), {
'name': 'networks',
'value': [],
'values': sorted([{'id': x.id, 'text': x.name} for x in Network.objects.all()], key=lambda x: x['text'].lower()), # TODO: We will fix this behavior after current admin client is fully removed
'label': ugettext('Networks'),
'tooltip': ugettext('Networks associated with this transport. If No network selected, will mean "all networks"'),
'type': 'multichoice',
'order': 101
}), {
'name': 'allowed_oss',
'value': [], 'value': [],
'values': sorted([{'id': x.id, 'text': x.name} for x in Network.objects.all()], key=lambda x: x['text'].lower()), # TODO: We will fix this behavior after current admin client is fully removed 'values': sorted([{'id': x, 'text': x} for x in OsDetector.knownOss], key=lambda x: x['text'].lower()), # TODO: We will fix this behavior after current admin client is fully removed
'label': ugettext('Networks'), 'label': ugettext('Allowed Devices'),
'tooltip': ugettext('Networks associated with this transport. If No network selected, will mean "all networks"'), 'tooltip': ugettext('If empty, any kind of device compatible with this transport will be allowed. Else, only devices compatible with selected values will be allowed'),
'type': 'multichoice', 'type': 'multichoice',
'order': 101 'order': 102
}) })
except Exception: except Exception:
self.invalidItemException() self.invalidItemException()
@ -94,12 +105,17 @@ class Transports(ModelHandler):
'priority': item.priority, 'priority': item.priority,
'nets_positive': item.nets_positive, 'nets_positive': item.nets_positive,
'networks': [{'id': n.id} for n in item.networks.all()], 'networks': [{'id': n.id} for n in item.networks.all()],
'allowed_oss': [{'id': x} for x in item.allowed_oss.split(',')] if item.allowed_oss != '' else [],
'deployed_count': item.deployedServices.count(), 'deployed_count': item.deployedServices.count(),
'type': type_.type(), 'type': type_.type(),
'protocol': type_.protocol, 'protocol': type_.protocol,
'permission': permissions.getEffectivePermission(self._user, item) 'permission': permissions.getEffectivePermission(self._user, item)
} }
def beforeSave(self, fields):
fields['allowed_oss'] = ','.join(fields['allowed_oss'])
def afterSave(self, item): def afterSave(self, item):
try: try:
networks = self._params['networks'] networks = self._params['networks']
@ -108,5 +124,13 @@ class Transports(ModelHandler):
return return
if networks is None: if networks is None:
return return
logger.debug('Params: {0}'.format(networks)) logger.debug('Networks: {0}'.format(networks))
item.networks = Network.objects.filter(id__in=networks) item.networks = Network.objects.filter(id__in=networks)
# try:
# oss = ','.join(self._params['allowed_oss'])
# except:
# oss = ''
# logger.debug('Devices: {0}'.format(oss))
# item.allowed_oss = oss
# item.save() # Store correctly the allowed_oss

View File

@ -41,6 +41,14 @@ gui.connectivity.link = (event) ->
# Load osmanager "info" # Load osmanager "info"
gui.methods.typedShow gui.connectivity.transports, selected[0], '#transports-info-placeholder .well', gettext('Error accessing data') gui.methods.typedShow gui.connectivity.transports, selected[0], '#transports-info-placeholder .well', gettext('Error accessing data')
onData: (data) ->
$.each data, (undefined_, value) ->
if value.allowed_oss != ''
value.allowed_oss = (v.id for v in value.allowed_oss).toString()
return
return
buttons: [ buttons: [
"new" "new"
"edit" "edit"

View File

@ -105,7 +105,7 @@ def index(request):
trans = [] trans = []
for t in svr.transports.all().order_by('priority'): for t in svr.transports.all().order_by('priority'):
typeTrans = t.getType() typeTrans = t.getType()
if t.validForIp(request.ip) and typeTrans.supportsOs(os['OS']): if t.validForIp(request.ip) and typeTrans.supportsOs(os['OS']) and t.validForOs(os['OS']):
if typeTrans.ownLink is True: if typeTrans.ownLink is True:
link = reverse('TransportOwnLink', args=('A' + svr.uuid, t.uuid)) link = reverse('TransportOwnLink', args=('A' + svr.uuid, t.uuid))
else: else:
@ -147,7 +147,7 @@ def index(request):
trans = [] trans = []
for t in svr.transports.all().order_by('priority'): for t in svr.transports.all().order_by('priority'):
typeTrans = t.getType() typeTrans = t.getType()
if t.validForIp(request.ip) and typeTrans.supportsOs(os['OS']): if t.validForIp(request.ip) and typeTrans.supportsOs(os['OS']) and t.validForOs(os['OS']):
if typeTrans.ownLink is True: if typeTrans.ownLink is True:
link = reverse('TransportOwnLink', args=('F' + svr.uuid, t.uuid)) link = reverse('TransportOwnLink', args=('F' + svr.uuid, t.uuid))
else: else: