本文整理汇总了Python中networkapi.interface.models.Interface.get_by_pk方法的典型用法代码示例。如果您正苦于以下问题:Python Interface.get_by_pk方法的具体用法?Python Interface.get_by_pk怎么用?Python Interface.get_by_pk使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkapi.interface.models.Interface
的用法示例。
在下文中一共展示了Interface.get_by_pk方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: verificar_nome_channel
# 需要导入模块: from networkapi.interface.models import Interface [as 别名]
# 或者: from networkapi.interface.models.Interface import get_by_pk [as 别名]
def verificar_nome_channel(nome, interfaces):
interface = Interface()
channels = PortChannel.objects.filter(nome=nome)
channels_id = []
for ch in channels:
channels_id.append(int(ch.id))
if channels_id:
for var in interfaces:
if not var == "" and not var == None:
interface_id = int(var)
interface_id = interface.get_by_pk(interface_id)
equip_id = interface_id.equipamento.id
equip_interfaces = interface.search(equip_id)
for i in equip_interfaces:
try:
sw = i.get_switch_and_router_interface_from_host_interface(i.protegida)
except:
sw = None
pass
if sw is not None:
if sw.channel is not None:
if sw.channel.id in channels_id:
raise exceptions.InterfaceException(u"O nome do port channel ja foi utilizado no equipamento")
示例2: _dissociate_interfaces_from_channel
# 需要导入模块: from networkapi.interface.models import Interface [as 别名]
# 或者: from networkapi.interface.models.Interface import get_by_pk [as 别名]
def _dissociate_interfaces_from_channel(self, ids_list, ids_interface):
ids_interface = [int(x) for x in ids_interface]
dissociate = set(ids_list) - set(ids_interface)
for item in dissociate:
item = Interface.get_by_pk(int(item))
item.channel = None
item.save()
示例3: delete
# 需要导入模块: from networkapi.interface.models import Interface [as 别名]
# 或者: from networkapi.interface.models.Interface import get_by_pk [as 别名]
def delete(self, request, *args, **kwargs):
"""URL: api/interface/disconnect/(?P<id_interface_1>\d+)/(?P<id_interface_2>\d+)/"""
try:
log.info('API_Disconnect')
data = dict()
id_interface_1 = kwargs.get('id_interface_1')
id_interface_2 = kwargs.get('id_interface_2')
interface_1 = Interface.get_by_pk(int(id_interface_1))
interface_2 = Interface.get_by_pk(int(id_interface_2))
with distributedlock(LOCK_INTERFACE % id_interface_1):
if interface_1.channel or interface_2.channel:
raise exceptions.InterfaceException(
'Interface está em um Port Channel')
if interface_1.ligacao_front_id == interface_2.id:
interface_1.ligacao_front = None
if interface_2.ligacao_front_id == interface_1.id:
interface_2.ligacao_front = None
else:
interface_2.ligacao_back = None
elif interface_1.ligacao_back_id == interface_2.id:
interface_1.ligacao_back = None
if interface_2.ligacao_back_id == interface_1.id:
interface_2.ligacao_back = None
else:
interface_2.ligacao_front = None
elif not interface_1.ligacao_front_id and not interface_1.ligacao_back_id:
raise exceptions.InterfaceException(
'Interface id %s não connectada' % interface_1)
interface_1.save()
interface_2.save()
return Response(data, status=status.HTTP_200_OK)
except exceptions.InterfaceException, exception:
raise exception
示例4: _update_interfaces_from_http_put
# 需要导入模块: from networkapi.interface.models import Interface [as 别名]
# 或者: from networkapi.interface.models.Interface import get_by_pk [as 别名]
def _update_interfaces_from_http_put(self, ids_interface, int_type,
vlan_nativa, envs_vlans):
# update interfaces
if type(ids_interface) is not list:
i = ids_interface
ids_interface = []
ids_interface.append(i)
ifaces_on_channel = []
for iface_id in ids_interface:
iface = Interface.get_by_pk(int(iface_id))
self._update_interfaces_from_a_channel(iface, vlan_nativa,
ifaces_on_channel,
int_type)
interface_sw = Interface.get_by_pk(int(iface))
interface_server = Interface.get_by_pk(
interface_sw.ligacao_front.id)
front = None
if interface_server.ligacao_front.id is not None:
front = interface_server.ligacao_front.id
back = None
if interface_server.ligacao_back.id is not None:
back = interface_server.ligacao_back.id
Interface.update(
user,
interface_server.id,
interface=interface_server.interface,
protegida=interface_server.protegida,
descricao=interface_server.descricao,
ligacao_front_id=front,
ligacao_back_id=back,
tipo=int_type,
vlan_nativa=int(vlan_nativa)
)
示例5: generate_and_deploy_interface_config_sync
# 需要导入模块: from networkapi.interface.models import Interface [as 别名]
# 或者: from networkapi.interface.models.Interface import get_by_pk [as 别名]
def generate_and_deploy_interface_config_sync(user, id_interface):
if not is_valid_int_greater_zero_param(id_interface):
raise exceptions.InvalidIdInterfaceException()
interface = Interface.get_by_pk(id_interface)
interfaces = [interface]
file_to_deploy = _generate_config_file(interfaces)
#TODO Deploy config file
lockvar = LOCK_INTERFACE_DEPLOY_CONFIG % (interface.equipamento.id)
status_deploy = deploy_config_in_equipment_synchronous(file_to_deploy, interface.equipamento, lockvar)
return status_deploy
示例6: handle_post
# 需要导入模块: from networkapi.interface.models import Interface [as 别名]
# 或者: from networkapi.interface.models.Interface import get_by_pk [as 别名]
def handle_post(self, request, user, *args, **kwargs):
"""Treat requests POST to add Rack.
URL: interface/associar-ambiente/
"""
try:
self.log.info("Associa interface aos ambientes")
# User permission
if not has_perm(user, AdminPermission.EQUIPMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
raise UserNotAuthorizedError(None)
# Load XML data
xml_map, attrs_map = loads(request.raw_post_data)
# XML data format
networkapi_map = xml_map.get('networkapi')
if networkapi_map is None:
return self.response_error(3, u'There is no value to the networkapi tag of XML request.')
interface_map = networkapi_map.get('interface')
if interface_map is None:
return self.response_error(3, u'There is no value to the interface tag of XML request.')
# Get XML data
env = interface_map.get('ambiente')
interface = interface_map.get('interface')
amb_int = EnvironmentInterface()
interfaces = Interface()
amb = Ambiente()
amb_int.interface = interfaces.get_by_pk(int(interface))
amb_int.ambiente = amb.get_by_pk(int(env))
amb_int.create(user)
amb_int_map = dict()
amb_int_map['interface_ambiente'] = amb_int
return self.response(dumps_networkapi(amb_int_map))
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例7: handle_delete
# 需要导入模块: from networkapi.interface.models import Interface [as 别名]
# 或者: from networkapi.interface.models.Interface import get_by_pk [as 别名]
def handle_delete(self, request, user, *args, **kwargs):
"""Trata uma requisição DELETE para excluir uma interface
URL: /interface/<id_interface>/
"""
# Get request data and check permission
try:
# Valid Interface ID
id_interface = kwargs.get("id_interface")
if not is_valid_int_greater_zero_param(id_interface):
self.log.error(u"The id_interface parameter is not a valid value: %s.", id_interface)
raise InvalidValueError(None, "id_interface", id_interface)
# Get interface and equipment to check permission
interface = Interface.get_by_pk(id_interface)
id_equipamento = interface.equipamento_id
# Check permission
if not has_perm(
user,
AdminPermission.EQUIPMENT_MANAGEMENT,
AdminPermission.WRITE_OPERATION,
None,
id_equipamento,
AdminPermission.EQUIP_WRITE_OPERATION,
):
return self.not_authorized()
with distributedlock(LOCK_INTERFACE % id_interface):
# Remove interface
Interface.remove(user, id_interface)
return self.response(dumps_networkapi({}))
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例8: available_channel_number
# 需要导入模块: from networkapi.interface.models import Interface [as 别名]
# 或者: from networkapi.interface.models.Interface import get_by_pk [as 别名]
def available_channel_number(channel_name, interface_ids):
log.info("available channel")
interface_obj = Interface()
for interface_id in interface_ids:
interface = interface_obj.get_by_pk(interface_id)
if not interface:
raise Exception("Do not exist interface with id: %s" % interface_id)
equipment_id = interface.equipamento.id
if not interface.ligacao_front:
raise Exception("Interface is not connected.")
front_equipment_id = interface.ligacao_front.equipamento.id
if Interface.objects.filter(equipamento=equipment_id, channel__nome=channel_name):
raise Exception("Channel name already exist on the equipment: %s" % channel_name)
if Interface.objects.filter(equipamento=front_equipment_id, channel__nome=channel_name):
raise Exception("Channel name already exist on the equipment: %s" % channel_name)
示例9: handle_put
# 需要导入模块: from networkapi.interface.models import Interface [as 别名]
# 或者: from networkapi.interface.models.Interface import get_by_pk [as 别名]
def handle_put(self, request, user, *args, **kwargs):
"""Trata uma requisição PUT para alterar informações de uma interface.
URL: /interface/<id_interface>/
"""
# Get request data and check permission
try:
# Valid Interface ID
id_interface = kwargs.get('id_interface')
if not is_valid_int_greater_zero_param(id_interface):
self.log.error(
u'The id_interface parameter is not a valid value: %s.', id_interface)
raise InvalidValueError(None, 'id_interface', id_interface)
# Get interface and equipment to check permission
interface = Interface.get_by_pk(id_interface)
id_equipamento = interface.equipamento_id
# Check permission
if not has_perm(user,
AdminPermission.EQUIPMENT_MANAGEMENT,
AdminPermission.WRITE_OPERATION,
None,
id_equipamento,
AdminPermission.EQUIP_WRITE_OPERATION):
return self.not_authorized()
# Get XML data
xml_map, attrs_map = loads(request.raw_post_data)
networkapi_map = xml_map.get('networkapi')
if networkapi_map is None:
return self.response_error(3, u'There is no networkapi tag in XML request.')
interface_map = networkapi_map.get('interface')
if interface_map is None:
return self.response_error(3, u'There is no interface tag in XML request.')
# Valid name value
nome = interface_map.get('nome')
if not is_valid_string_minsize(nome, 1) or not is_valid_string_maxsize(nome, 20):
self.log.error(u'Parameter nome is invalid. Value: %s', nome)
raise InvalidValueError(None, 'nome', nome)
# Valid protegida value
protegida = interface_map.get('protegida')
if not is_valid_boolean_param(protegida):
self.log.error(
u'Parameter protegida is invalid. Value: %s', protegida)
raise InvalidValueError(None, 'protegida', protegida)
else:
protegida = convert_string_or_int_to_boolean(protegida)
# Valid descricao value
descricao = interface_map.get('descricao')
if descricao is not None:
if not is_valid_string_minsize(descricao, 3) or not is_valid_string_maxsize(descricao, 200):
self.log.error(
u'Parameter descricao is invalid. Value: %s', descricao)
raise InvalidValueError(None, 'descricao', descricao)
# Valid "id_ligacao_front" value
id_ligacao_front = interface_map.get('id_ligacao_front')
if id_ligacao_front is not None:
if not is_valid_int_greater_zero_param(id_ligacao_front):
self.log.error(
u'The id_ligacao_front parameter is not a valid value: %s.', id_ligacao_front)
raise InvalidValueError(
None, 'id_ligacao_front', id_ligacao_front)
else:
id_ligacao_front = int(id_ligacao_front)
# Valid "id_ligacao_back" value
id_ligacao_back = interface_map.get('id_ligacao_back')
if id_ligacao_back is not None:
if not is_valid_int_greater_zero_param(id_ligacao_back):
self.log.error(
u'The id_ligacao_back parameter is not a valid value: %s.', id_ligacao_back)
raise InvalidValueError(
None, 'id_ligacao_back', id_ligacao_back)
else:
id_ligacao_back = int(id_ligacao_back)
tipo = interface_map.get('tipo')
tipo = TipoInterface.get_by_name(tipo)
vlan = interface_map.get('vlan')
with distributedlock(LOCK_INTERFACE % id_interface):
# Update interface
Interface.update(user,
id_interface,
interface=nome,
protegida=protegida,
descricao=descricao,
ligacao_front_id=id_ligacao_front,
ligacao_back_id=id_ligacao_back,
tipo=tipo,
#.........这里部分代码省略.........
示例10: handle_get
# 需要导入模块: from networkapi.interface.models import Interface [as 别名]
# 或者: from networkapi.interface.models.Interface import get_by_pk [as 别名]
def handle_get(self, request, user, *args, **kwargs):
"""Treat GET requests to list interface by ID or by channel
URL: interface/<id_interface>/get/
interface/get-by-channel/<channel_name>/
interface/get/<channel_name>/<id_equip>
"""
try:
self.log.info("GET Interface")
# User permission
if not has_perm(user, AdminPermission.EQUIPMENT_MANAGEMENT, AdminPermission.READ_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
raise UserNotAuthorizedError(None)
# Get id_interface param
interface = kwargs.get('id_interface')
channel = kwargs.get('channel_name')
equipamento = kwargs.get('id_equipamento')
equipInterface_list = []
interface_list = []
# Valid Interface ID
if interface is not None:
if not is_valid_int_greater_zero_param(interface):
self.log.error(u'The id_interface parameter is not a valid value: %s.', interface)
raise InvalidValueError(None, 'id_interface', interface)
id_interface = interface
# Checks if interface exists in database
interface = Interface.get_by_pk(id_interface)
int_map = get_new_interface_map(interface)
# Return interface map
return self.response(dumps_networkapi({'interface': int_map}))
if channel is not None:
if equipamento is None:
interfaces = Interface.objects.all().filter(channel__nome=channel)
for interf in interfaces:
equipInterface_list.append(get_new_interface_map(interf))
return self.response(dumps_networkapi({'interfaces': equipInterface_list}))
else:
interfaces = Interface.objects.all().filter(channel__nome=channel)
for interf in interfaces:
if interf.equipamento.id==int(equipamento):
int_server = interf.get_server_switch_or_router_interface_from_host_interface()
equipamento = int_server.equipamento.id
interfaces_equip = Interface.objects.all().filter(equipamento__id=int(equipamento))
for interf in interfaces_equip:
try:
i = interf.get_switch_and_router_interface_from_host_interface()
interface_list.append(get_new_interface_map(i))
except:
pass
return self.response(dumps_networkapi({'interfaces': interface_list}))
return self.response(dumps_networkapi({}))
except UserNotAuthorizedError:
return self.not_authorized()
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例11: handle_post
# 需要导入模块: from networkapi.interface.models import Interface [as 别名]
# 或者: from networkapi.interface.models.Interface import get_by_pk [as 别名]
def handle_post(self, request, user, *args, **kwargs):
"""Treat requests POST to add Rack.
URL: channel/inserir/
"""
try:
self.log.info("Inserir novo Channel")
# User permission
if not has_perm(user, AdminPermission.EQUIPMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION):
self.log.error(u'User does not have permission to perform the operation.')
raise UserNotAuthorizedError(None)
# Load XML data
xml_map, attrs_map = loads(request.raw_post_data)
# XML data format
networkapi_map = xml_map.get('networkapi')
if networkapi_map is None:
return self.response_error(3, u'There is no value to the networkapi tag of XML request.')
channel_map = networkapi_map.get('channel')
if channel_map is None:
return self.response_error(3, u'There is no value to the channel tag of XML request.')
# Get XML data
interfaces = channel_map.get('interfaces')
nome = channel_map.get('nome')
lacp = channel_map.get('lacp')
int_type = channel_map.get('int_type')
vlans = channel_map.get('vlan')
envs = channel_map.get('envs')
port_channel = PortChannel()
interface = Interface()
amb = Ambiente()
cont = []
interfaces = str(interfaces).split('-')
interface_id = None
# verifica a vlan_nativa
vlan = vlans.get('vlan_nativa')
if vlan is not None:
if int(vlan) < 1 or int(vlan) > 4096:
raise InvalidValueError(None, "Vlan Nativa", "Range valido: 1 - 4096.")
if int(vlan) < 1 or 3967 < int(vlan) < 4048 or int(vlan)==4096:
raise InvalidValueError(None, "Vlan Nativa" ,"Range reservado: 3968-4047;4094.")
# verifica se o nome do port channel já existe no equipamento
channels = PortChannel.objects.filter(nome=nome)
channels_id = []
for ch in channels:
channels_id.append(int(ch.id))
if channels_id:
for var in interfaces:
if not var=="" and not var==None:
interface_id = int(var)
interface_id = interface.get_by_pk(interface_id)
equip_id = interface_id.equipamento.id
equip_interfaces = interface.search(equip_id)
for i in equip_interfaces:
try:
sw = i.get_switch_and_router_interface_from_host_interface(i.protegida)
except:
sw = None
pass
if sw.channel is not None:
if sw.channel.id in channels_id:
raise InterfaceError("O nome do port channel ja foi utilizado no equipamento")
#cria o port channel
port_channel.nome = str(nome)
port_channel.lacp = convert_string_or_int_to_boolean(lacp)
port_channel.create(user)
int_type = TipoInterface.get_by_name(str(int_type))
for var in interfaces:
if not var=="" and not var==None:
interf = interface.get_by_pk(int(var))
try:
sw_router = interf.get_switch_and_router_interface_from_host_interface(interf.protegida)
except:
raise InterfaceError("Interface não conectada")
if sw_router.channel is not None:
raise InterfaceError("Interface %s já está em um Channel" % sw_router.interface)
if cont is []:
cont.append(int(sw_router.equipamento.id))
elif not sw_router.equipamento.id in cont:
cont.append(int(sw_router.equipamento.id))
if len(cont) > 2:
raise InterfaceError("Mais de dois equipamentos foram selecionados")
if sw_router.ligacao_front is not None:
ligacao_front_id = sw_router.ligacao_front.id
else:
#.........这里部分代码省略.........
示例12: handle_put
# 需要导入模块: from networkapi.interface.models import Interface [as 别名]
# 或者: from networkapi.interface.models.Interface import get_by_pk [as 别名]
def handle_put(self, request, user, *args, **kwargs):
"""Treat requests POST to add Rack.
URL: channel/editar/
"""
try:
self.log.info("Editar Channel")
# User permission
if not has_perm(user, AdminPermission.EQUIPMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION):
self.log.error(u'User does not have permission to perform the operation.')
raise UserNotAuthorizedError(None)
# Load XML data
xml_map, attrs_map = loads(request.raw_post_data)
# XML data format
networkapi_map = xml_map.get('networkapi')
if networkapi_map is None:
return self.response_error(3, u'There is no value to the networkapi tag of XML request.')
channel_map = networkapi_map.get('channel')
if channel_map is None:
return self.response_error(3, u'There is no value to the channel tag of XML request.')
# Get XML data
id_channel = channel_map.get('id_channel')
nome = channel_map.get('nome')
if not is_valid_int_greater_zero_param(nome):
raise InvalidValueError(None, "Numero do Channel", "Deve ser um numero inteiro.")
lacp = channel_map.get('lacp')
int_type = channel_map.get('int_type')
vlans = channel_map.get('vlan')
envs = channel_map.get('envs')
ids_interface = channel_map.get('ids_interface')
if ids_interface is None:
raise InterfaceError("Nenhuma interface selecionada")
# verifica a vlan_nativa
vlan = vlans.get('vlan_nativa')
if vlan is not None:
if int(vlan) < 1 or int(vlan) > 4096:
raise InvalidValueError(None, "Vlan Nativa", "Range valido: 1 - 4096.")
if int(vlan) < 1 or 3967 < int(vlan) < 4048 or int(vlan)==4096:
raise InvalidValueError(None, "Vlan Nativa" ,"Range reservado: 3968-4047;4094.")
port_channel = PortChannel()
interface = Interface()
amb = Ambiente()
# verifica se o nome do port channel está sendo usado no equipamento
channels = PortChannel.objects.filter(nome=nome)
channels_id = []
for ch in channels:
channels_id.append(int(ch.id))
if len(channels_id)>1:
if type(ids_interface) is list:
for var in ids_interface:
if not var=="" and not var==None:
interface_id = int(var)
else:
interface_id = int(ids_interface)
interface_id = interface.get_by_pk(interface_id)
equip_id = interface_id.equipamento.id
equip_interfaces = interface.search(equip_id)
for i in equip_interfaces:
try:
sw = i.get_switch_and_router_interface_from_host_interface(i.protegida)
except:
sw = None
pass
if sw is not None:
if sw.channel is not None:
if sw.channel.id in channels_id and sw.channel.id is not id_channel:
raise InterfaceError("O nome do port channel ja foi utilizado no equipamento")
#buscar interfaces do channel
interfaces = Interface.objects.all().filter(channel__id=id_channel)
ids_list = []
for i in interfaces:
ids_list.append(i.id)
ids_list = [ int(y) for y in ids_list ]
if type(ids_interface) is list:
ids_interface = [ int(x) for x in ids_interface ]
desassociar = set(ids_list) - set(ids_interface)
for item in desassociar:
item = interface.get_by_pk(int(item))
item.channel = None
item.save()
else:
if ids_interface is not None:
ids_interface = int(ids_interface)
if ids_interface is not None:
for item in ids_list:
item = interface.get_by_pk(int(item))
item.channel = None
item.save()
#.........这里部分代码省略.........
示例13: handle_delete
# 需要导入模块: from networkapi.interface.models import Interface [as 别名]
# 或者: from networkapi.interface.models.Interface import get_by_pk [as 别名]
def handle_delete(self, request, user, *args, **kwargs):
"""Trata uma requisição DELETE para excluir um port channel
URL: /channel/delete/<channel_name>/<interface_id>
"""
try:
self.log.info("Delete Channel")
# User permission
if not has_perm(user, AdminPermission.EQUIPMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION):
self.log.error(u'User does not have permission to perform the operation.')
raise UserNotAuthorizedError(None)
interface_id = kwargs.get('channel_name')
interface = Interface.get_by_pk(int(interface_id))
equip_list = []
try:
interface.channel.id
channel = interface.channel
except:
channel = interface.ligacao_front.channel
pass
try:
interfaces = Interface.objects.all().filter(channel__id=channel.id)
except:
return self.response(dumps_networkapi({}))
for i in interfaces:
equip_list.append(i.equipamento.id)
equip_list = set(equip_list)
equip_dict = dict()
for e in equip_list:
equip_dict[str(e)] = interfaces.filter(equipamento__id=e)
tipo = TipoInterface()
tipo = tipo.get_by_name("access")
for e in equip_dict:
for i in equip_dict.get(e):
try:
front = i.ligacao_front.id
except:
front = None
pass
try:
back = i.ligacao_back.id
except:
back = None
pass
i.update(user,
i.id,
interface=i.interface,
protegida=i.protegida,
descricao=i.descricao,
ligacao_front_id=front,
ligacao_back_id=back,
tipo=tipo,
vlan_nativa="1")
status = api_interface_facade.delete_channel(user, e, equip_dict.get(e), channel)
channel.delete(user)
return self.response(dumps_networkapi({}))
except api_interface_exceptions.InterfaceException, e:
return api_interface_exceptions.InterfaceException(e)
示例14: handle_delete
# 需要导入模块: from networkapi.interface.models import Interface [as 别名]
# 或者: from networkapi.interface.models.Interface import get_by_pk [as 别名]
def handle_delete(self, request, user, *args, **kwargs):
"""Treat DELETE requests to remove the connection of two interfaces by front or back
URL: interface/<id_interface>/<back_or_front>/
"""
try:
self.log.info("Disconnect")
# Valid Interface ID
id_interface = kwargs.get('id_interface')
if not is_valid_int_greater_zero_param(id_interface):
self.log.error(
u'The id_interface parameter is not a valid value: %s.', id_interface)
raise InvalidValueError(None, 'id_interface', id_interface)
# Valid back or front param
back_or_front = kwargs.get('back_or_front')
if not is_valid_zero_one_param(back_or_front):
self.log.error(
u'The back_or_front parameter is not a valid value: %s.', back_or_front)
raise InvalidValueError(None, 'back_or_front', back_or_front)
else:
back_or_front = int(back_or_front)
# User permission equip 1
if not has_perm(user, AdminPermission.EQUIPMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
return self.not_authorized()
# Checks if interface exists in database
interface_1 = Interface.get_by_pk(id_interface)
with distributedlock(LOCK_INTERFACE % id_interface):
# Is valid back or front connection
if back_or_front:
try:
interface_2 = Interface.get_by_pk(
interface_1.ligacao_front_id)
except InterfaceNotFoundError:
raise InterfaceInvalidBackFrontError(
None, "Interface two has no connection with front of Interface one")
else:
try:
interface_2 = Interface.get_by_pk(
interface_1.ligacao_back_id)
except InterfaceNotFoundError:
raise InterfaceInvalidBackFrontError(
None, "Interface two has no connection with back of Interface one")
if interface_2.ligacao_front_id == interface_1.id:
back_or_front_2 = 1
elif interface_2.ligacao_back_id == interface_1.id:
back_or_front_2 = 0
else:
raise InterfaceInvalidBackFrontError(
None, "Interface one has no connection with front or back of Interface two")
# Business Rules
# Remove Interface one connection
if back_or_front:
interface_1.ligacao_front = None
else:
interface_1.ligacao_back = None
# Remove Interface two connection
if back_or_front_2:
interface_2.ligacao_front = None
else:
interface_2.ligacao_back = None
# Save
interface_1.save()
interface_2.save()
#sai do channel
if interface_1.channel is not None:
self.log.info("channel")
id_channel = interface_1.channel.id
interface_1.channel = None
interface_1.save()
interfaces = Interface.objects.all().filter(channel__id=id_channel)
if not len(interfaces) > 0:
self.log.info("len "+str(len(interfaces)))
PortChannel.delete()
# Return None for success
return self.response(dumps_networkapi({}))
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例15: handle_post
# 需要导入模块: from networkapi.interface.models import Interface [as 别名]
# 或者: from networkapi.interface.models.Interface import get_by_pk [as 别名]
def handle_post(self, request, user, *args, **kwargs):
"""Treat requests POST to add Rack.
URL: channel/inserir/
"""
try:
self.log.info("Inserir novo Channel")
# User permission
if not has_perm(user, AdminPermission.EQUIPMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
raise UserNotAuthorizedError(None)
# Load XML data
xml_map, attrs_map = loads(request.raw_post_data)
# XML data format
networkapi_map = xml_map.get('networkapi')
if networkapi_map is None:
return self.response_error(3, u'There is no value to the networkapi tag of XML request.')
channel_map = networkapi_map.get('channel')
if channel_map is None:
return self.response_error(3, u'There is no value to the channel tag of XML request.')
# Get XML data
interfaces = channel_map.get('interfaces')
nome = channel_map.get('nome')
lacp = channel_map.get('lacp')
int_type = channel_map.get('int_type')
vlan = channel_map.get('vlan')
envs = channel_map.get('envs')
port_channel = PortChannel()
interface = Interface()
amb = Ambiente()
cont = []
port_channel.nome = str(nome)
port_channel.lacp = convert_string_or_int_to_boolean(lacp)
port_channel.create(user)
interfaces = str(interfaces).split('-')
int_type = TipoInterface.get_by_name(str(int_type))
for var in interfaces:
if not var=="" and not var==None:
interf = interface.get_by_pk(int(var))
try:
sw_router = interf.get_switch_and_router_interface_from_host_interface(interf.protegida)
except:
raise InterfaceError("Interface não conectada")
if sw_router.channel is not None:
raise InterfaceError("Interface %s já está em um Channel" % sw_router.interface)
for i in interface.search(sw_router.equipamento.id):
if i.channel is not None:
raise InterfaceError("Equipamento %s já possui um Channel" % sw_router.equipamento.nome)
if cont is []:
cont.append(int(sw_router.equipamento.id))
elif not sw_router.equipamento.id in cont:
cont.append(int(sw_router.equipamento.id))
if len(cont) > 2:
raise InterfaceError("Mais de dois equipamentos foram selecionados")
if sw_router.ligacao_front is not None:
ligacao_front_id = sw_router.ligacao_front.id
else:
ligacao_front_id = None
if sw_router.ligacao_back is not None:
ligacao_back_id = sw_router.ligacao_back.id
else:
ligacao_back_id = None
Interface.update(user,
sw_router.id,
interface=sw_router.interface,
protegida=sw_router.protegida,
descricao=sw_router.descricao,
ligacao_front_id=ligacao_front_id,
ligacao_back_id=ligacao_back_id,
tipo=int_type,
vlan_nativa=vlan,
channel=port_channel)
if "trunk" in int_type.tipo:
interface_list = EnvironmentInterface.objects.all().filter(interface=sw_router.id)
for int_env in interface_list:
int_env.delete(user)
if envs is not None:
if not type(envs)==unicode:
for env in envs:
amb_int = EnvironmentInterface()
amb_int.interface = sw_router
amb_int.ambiente = amb.get_by_pk(int(env))
amb_int.create(user)
else:
#.........这里部分代码省略.........