本文整理汇总了Python中networkapi.requisicaovips.models.RequisicaoVips.get_by_pk方法的典型用法代码示例。如果您正苦于以下问题:Python RequisicaoVips.get_by_pk方法的具体用法?Python RequisicaoVips.get_by_pk怎么用?Python RequisicaoVips.get_by_pk使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkapi.requisicaovips.models.RequisicaoVips
的用法示例。
在下文中一共展示了RequisicaoVips.get_by_pk方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_get
# 需要导入模块: from networkapi.requisicaovips.models import RequisicaoVips [as 别名]
# 或者: from networkapi.requisicaovips.models.RequisicaoVips import get_by_pk [as 别名]
def handle_get(self, request, user, *args, **kwargs):
"""Trata as requisições de GET para listar uma requisição de VIP.
Filtra a requisição de VIP por chave primária.
URL: vip/id_vip
"""
try:
if kwargs.get('id_vip') is None:
return super(RequisicaoVipsResource, self).handle_get(request, user, *args, **kwargs)
if not has_perm(user,
AdminPermission.VIPS_REQUEST,
AdminPermission.READ_OPERATION):
return self.not_authorized()
# Valid Ip ID
if not is_valid_int_greater_zero_param(kwargs.get('id_vip')):
self.log.error(
u'The id_vip parameter is not a valid value: %s.', kwargs.get('id_vip'))
raise InvalidValueError(None, 'id_vip', kwargs.get('id_vip'))
request_vip = RequisicaoVips.get_by_pk(kwargs.get('id_vip'))
request_vip_map = request_vip.variables_to_map()
""""""
vip_port_list, reals_list, reals_priority, reals_weight = request_vip.get_vips_and_reals(
request_vip.id)
if reals_list:
request_vip_map['reals'] = {'real': reals_list}
request_vip_map['reals_prioritys'] = {
'reals_priority': reals_priority}
request_vip_map['reals_weights'] = {
'reals_weight': reals_weight}
request_vip_map['portas_servicos'] = {'porta': vip_port_list}
""""""
request_vip_map['id'] = request_vip.id
request_vip_map['validado'] = convert_boolean_to_int(
request_vip.validado)
request_vip_map['vip_criado'] = convert_boolean_to_int(
request_vip.vip_criado)
request_vip_map['id_ip'] = request_vip.ip_id
request_vip_map['id_ipv6'] = request_vip.ipv6_id
request_vip_map[
'id_healthcheck_expect'] = request_vip.healthcheck_expect_id
request_vip_map['l7_filter'] = request_vip.l7_filter
request_vip_map['rule_id'] = request_vip.rule_id
return self.response(dumps_networkapi({'vip': request_vip_map}))
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例2: handle_delete
# 需要导入模块: from networkapi.requisicaovips.models import RequisicaoVips [as 别名]
# 或者: from networkapi.requisicaovips.models.RequisicaoVips import get_by_pk [as 别名]
def handle_delete(self, request, user, *args, **kwargs):
"""
Treat DELETE requests to remove a vip request.
Also remove reals related and balancer ips (if this ips isn't used for another vip).
URL: vip/delete/<id_vip>/
"""
try:
vip_id = kwargs.get('id_vip')
keep_ip = bool(request.REQUEST.get('keep_ip', False))
# User permission
if not has_perm(user, AdminPermission.VIPS_REQUEST, AdminPermission.WRITE_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
return self.not_authorized()
# Valid vip ID
if not is_valid_int_greater_zero_param(vip_id):
self.log.error(
u'Parameter id_vip is invalid. Value: %s.', vip_id)
raise InvalidValueError(None, 'id_vip', vip_id)
vip = RequisicaoVips.get_by_pk(vip_id)
if vip.vip_criado:
return self.response_error(370, vip_id)
ipv4 = vip.ip
ipv6 = vip.ipv6
with distributedlock(LOCK_VIP % vip_id):
try:
vip.delete_vips_and_reals(user)
vip.remove(user, vip_id)
# SYNC_VIP
delete_new(vip_id)
if ipv4 and not keep_ip:
if not self.is_ipv4_in_use(ipv4, vip_id):
ipv4.delete()
if ipv6 and not keep_ip:
if not self.is_ipv6_in_use(ipv6, vip_id):
ipv6.delete()
except IpCantRemoveFromServerPool, e:
raise e
except IpCantBeRemovedFromVip, e:
raise e
示例3: handle_put
# 需要导入模块: from networkapi.requisicaovips.models import RequisicaoVips [as 别名]
# 或者: from networkapi.requisicaovips.models.RequisicaoVips import get_by_pk [as 别名]
def handle_put(self, request, user, *args, **kwargs):
""" Handles a PUT request to edit the L7 filter.
URL: vip/<id_vip>/filter/
"""
if not has_perm(user,
AdminPermission.VIP_ALTER_SCRIPT,
AdminPermission.WRITE_OPERATION):
return self.not_authorized()
id_vip = kwargs.get('id_vip')
# 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.')
vip_map = networkapi_map.get('vip')
if vip_map is None:
return self.response_error(3, u'There is no value to the vip tag of XML request.')
vip = RequisicaoVips.get_by_pk(id_vip)
# Get XML data
l7_filter = vip_map['l7_filter']
vip.l7_filter = l7_filter
vip.filter_valid = False
# If the l7_filter is a rule, set filter_valid to TRUE
if vip_map.get('rule_id') is not None:
vip.filter_valid = 1
rule = Rule.objects.get(pk=vip_map.get('rule_id'))
vip.l7_filter = '\n'.join(
rule.rulecontent_set.all().values_list('content', flat=True))
vip.rule = rule
else:
vip.filter_valid = 0
vip.rule = None
vip.save(user)
map = dict()
map['sucesso'] = 'sucesso'
return self.response(dumps_networkapi(map))
示例4: handle_get
# 需要导入模块: from networkapi.requisicaovips.models import RequisicaoVips [as 别名]
# 或者: from networkapi.requisicaovips.models.RequisicaoVips import get_by_pk [as 别名]
def handle_get(self, request, user, *args, **kwargs):
"""Handles get requests to validate Vip Requests by id.
URLs: /vip/validate/<id_vip>/
"""
self.log.info('Validate Vip Request by id')
try:
# Commons Validations
# User permission
if not has_perm(user, AdminPermission.VIP_VALIDATION, AdminPermission.WRITE_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
return self.not_authorized()
# Business Validations
id_vip = kwargs.get('id_vip')
# Valid vip id
if not is_valid_int_greater_zero_param(id_vip):
self.log.error(
u'Parameter id_vip is invalid. Value: %s.', id_vip)
raise InvalidValueError(None, 'id_vip', id_vip)
vip = RequisicaoVips.get_by_pk(id_vip)
with distributedlock(LOCK_VIP % id_vip):
vip.validado = True
vip.save()
return self.response(dumps_networkapi({}))
except RequisicaoVipsNotFoundError:
return self.response_error(152)
except RequisicaoVipsError:
return self.response_error(150, 'Failed to validate vip request.')
except InvalidValueError, e:
self.log.error(
u'Parameter %s is invalid. Value: %s.', e.param, e.value)
return self.response_error(269, e.param, e.value)
示例5: handle_get
# 需要导入模块: from networkapi.requisicaovips.models import RequisicaoVips [as 别名]
# 或者: from networkapi.requisicaovips.models.RequisicaoVips import get_by_pk [as 别名]
def handle_get(self, request, user, *args, **kwargs):
"""Validate L7 filter
URLs: /vip/l7/<id_vip>/validate/
"""
try:
if not has_perm(user,
AdminPermission.VIP_VALIDATION,
AdminPermission.WRITE_OPERATION):
return self.not_authorized()
self.log.info('Validate L7 filter to VIP')
id_vip = kwargs.get('id_vip')
# Valid Vip ID
if not is_valid_int_greater_zero_param(id_vip):
self.log.error(
u'The vip_id parameter is not a valid value: %s.', id_vip)
raise InvalidValueError(None)
vip = RequisicaoVips.get_by_pk(id_vip)
with distributedlock(LOCK_VIP % id_vip):
# Vip must be created
if not vip.vip_criado:
self.log.error(
u'L7 filter can not be changed because VIP has not yet been created.')
raise RequestVipsNotBeenCreatedError(None)
vip.filter_valid = True
vip.save()
map = dict()
map['sucesso'] = 'sucesso'
return self.response(dumps_networkapi(map))
except UserNotAuthorizedError:
return self.not_authorized()
示例6: handle_get
# 需要导入模块: from networkapi.requisicaovips.models import RequisicaoVips [as 别名]
# 或者: from networkapi.requisicaovips.models.RequisicaoVips import get_by_pk [as 别名]
def handle_get(self, request, user, *args, **kwargs):
"""Handles a GET request to return L7 data
URL: vip/l7/<id_vip>/
"""
try:
if not has_perm(user,
AdminPermission.VIPS_REQUEST,
AdminPermission.READ_OPERATION):
return self.not_authorized()
# Valid Ip ID
if not is_valid_int_greater_zero_param(kwargs.get('id_vip')):
self.log.error(
u'The id_vip parameter is not a valid value: %s.', kwargs.get('id_vip'))
raise InvalidValueError(None, 'id_vip', kwargs.get('id_vip'))
request_vip = RequisicaoVips.get_by_pk(kwargs.get('id_vip'))
date = request_vip.applied_l7_datetime
if date:
date = date.strftime('%d/%m/%Y %H:%M:%S')
request_vip_map = dict()
request_vip_map['l7_filter'] = request_vip.l7_filter
request_vip_map['rule'] = request_vip.rule
request_vip_map['filter_applied'] = request_vip.filter_applied
request_vip_map['rule_applied'] = request_vip.rule_applied
request_vip_map['filter_rollback'] = request_vip.filter_rollback
request_vip_map['rule_rollback'] = request_vip.rule_rollback
request_vip_map['applied_l7_datetime'] = date
request_vip_map['filter_valid'] = convert_boolean_to_int(
request_vip.filter_valid)
return self.response(dumps_networkapi({'vip': request_vip_map}))
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例7: handle_put
# 需要导入模块: from networkapi.requisicaovips.models import RequisicaoVips [as 别名]
# 或者: from networkapi.requisicaovips.models.RequisicaoVips import get_by_pk [as 别名]
def handle_put(self, request, user, *args, **kwargs):
"""
Handles PUT requests to change the VIP's real server.
URL: vip/real/edit
"""
self.log.info("Change VIP's real server")
try:
# User permission
if not has_perm(user, AdminPermission.VIP_ALTER_SCRIPT, AdminPermission.WRITE_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
raise UserNotAuthorizedError(None)
# Commons Validations
# Load XML data
xml_map, attrs_map = loads(
request.raw_post_data, ['real', 'reals_weight', 'reals_priority'])
# 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.')
vip_map = networkapi_map.get('vip')
if vip_map is None:
return self.response_error(3, u'There is no value to the vip tag of XML request.')
# Get XML data
vip_id = vip_map.get('vip_id')
alter_priority = vip_map.get('alter_priority')
# Valid VIP ID
if not is_valid_int_greater_zero_param(vip_id):
self.log.error(
u'The vip_id parameter is not a valid value: %s.', vip_id)
raise InvalidValueError(None, 'vip_id', vip_id)
# Valid Alter Priority
if not is_valid_int_greater_equal_zero_param(alter_priority):
alter_priority = 0
# Existing Vip ID
vip = RequisicaoVips.get_by_pk(vip_id)
# Clone vip
vip_old = clone(vip)
server_pools = ServerPool.objects.filter(vipporttopool__requisicao_vip=vip)
server_pools_old = []
server_pools_members_old = []
for sp in server_pools:
server_pools_old.append(sp)
for spm in sp.serverpoolmember_set.all():
server_pools_members_old.append(spm)
# Get variables
variables_map = vip.variables_to_map()
# Valid variables
vip.set_variables(variables_map)
# Get balancing method
vip_map['metodo_bal'] = str(
variables_map.get('metodo_bal')).upper()
with distributedlock(LOCK_VIP % vip_id):
# Valid real names and real ips of real server
if vip_map.get('reals') is not None:
evip = EnvironmentVip.get_by_values(variables_map.get(
'finalidade'), variables_map.get('cliente'), variables_map.get('ambiente'))
for real in vip_map.get('reals').get('real'):
ip_aux_error = real.get('real_ip')
equip_aux_error = real.get('real_name')
if equip_aux_error is not None:
equip = Equipamento.get_by_name(equip_aux_error)
else:
self.log.error(
u'The real_name parameter is not a valid value: None.')
raise InvalidValueError(None, 'real_name', 'None')
# Valid Real
RequisicaoVips.valid_real_server(
ip_aux_error, equip, evip, False)
# Valid reals_prioritys
vip_map, code = vip.valid_values_reals_priority(vip_map)
if code is not None:
return self.response_error(329)
# Valid reals_weight
vip_map, code = vip.valid_values_reals_weight(vip_map)
if code is not None:
#.........这里部分代码省略.........
示例8: __post_virtual_group_vip
# 需要导入模块: from networkapi.requisicaovips.models import RequisicaoVips [as 别名]
# 或者: from networkapi.requisicaovips.models.RequisicaoVips import get_by_pk [as 别名]
#.........这里部分代码省略.........
equip_id = real.get('real_name')
if equip_id is not None:
equip = Equipamento.get_by_name(equip_id)
else:
self.log.error(
u'The real_name parameter is not a valid value: None.')
raise InvalidValueError(None, 'real_name', 'None')
# Valid Real
RequisicaoVips.valid_real_server(
ip_aux_error, equip, evip, False)
vip_map, code = RequisicaoVips().valid_values_reals_priority(
vip_map)
if code is not None:
return self.response_error(code)
vip_map, code = RequisicaoVips().valid_values_reals_weight(
vip_map)
if code is not None:
return self.response_error(code)
# Insere ou atualiza a requisição de VIP
if (id_vip_request_map is not None):
resp_vip_map['requisicao_vip'] = id_vip_request_map
if not is_valid_int_greater_zero_param(id_vip_request_map.get('id')):
self.log.error(
u'The requisicao_vip.id parameter is not a valid value: %s.', id_vip_request_map.get('id'))
raise InvalidValueError(
None, 'requisicao_vip.id', id_vip_request_map.get('id'))
vip_request = RequisicaoVips.get_by_pk(
id_vip_request_map.get('id'))
vip_map['id_ip'] = vip_request.ip_id
if vip_request.validado:
vip_map['validado'] = '1'
else:
vip_map['validado'] = '0'
if vip_request.vip_criado:
vip_map['vip_criado'] = '1'
else:
vip_map['vip_criado'] = '0'
response_vip = update_vip_request(
vip_request.id, vip_map, user)
if (response_vip != 0):
return self.response_error(response_vip)
else:
"""This condition is used to attend a requisite from 'Orquestra',
because in some points the VIP doesn't have cache option and
the value can be 'None'"""
if vip_map['cache'] is None:
vip_map['cache'] = "(nenhum)"
response_vip = insert_vip_request(vip_map, user)
if (response_vip[0] != 0):
if response_vip[0] not in (275, 276, 277):
return self.__treat_response_error(response_vip)
else:
return self.__treat_response_error([response_vip[0]])
resp_vip_map['requisicao_vip'] = {'id': response_vip[1].id}
示例9: handle_put
# 需要导入模块: from networkapi.requisicaovips.models import RequisicaoVips [as 别名]
# 或者: from networkapi.requisicaovips.models.RequisicaoVips import get_by_pk [as 别名]
def handle_put(self, request, user, *args, **kwargs):
"""Treat requests PUT change request VIP.
URLs: /requestvip/<id_vip>/
deprecated:: Use the new rest API
"""
self.log.info("Change request VIP")
try:
vip_id = kwargs.get('id_vip')
# Load XML data
xml_map, attrs_map = loads(
request.raw_post_data, ['real', 'reals_weight', 'reals_priority', 'porta'])
# 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.')
vip_map = networkapi_map.get('vip')
if vip_map is None:
return self.response_error(3, u'There is no value to the vip tag of XML request.')
# User permission
if not has_perm(user, AdminPermission.VIP_ALTER_SCRIPT, AdminPermission.WRITE_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
raise UserNotAuthorizedError(None)
# Valid Vip ID
if not is_valid_int_greater_zero_param(vip_id):
self.log.error(
u'The vip_id parameter is not a valid value: %s.', vip_id)
raise InvalidValueError(None, 'vip_id', vip_id)
# Valid Ipv4 and Ipv6 ID
if (vip_map.get('id_ipv4') is None and vip_map.get('id_ipv6') is None):
self.log.error(
u'The id_ipv4 and id_ipv6 parameter is not a valid value: %s.', vip_map.get('id_ipv4'))
raise InvalidValueError(
None, 'id_ipv4 e id_vip6', vip_map.get('id_ipv4'))
if (vip_map.get('id_ipv4') != None):
if not is_valid_int_greater_zero_param(vip_map.get('id_ipv4')):
self.log.error(
u'The id_ipv4 parameter is not a valid value: %s.', vip_map.get('id_ipv4'))
raise InvalidValueError(
None, 'id_ipv4', vip_map.get('id_ipv4'))
if (vip_map.get('id_ipv6') != None):
if not is_valid_int_greater_zero_param(vip_map.get('id_ipv6')):
self.log.error(
u'The id_ipv6 parameter is not a valid value: %s.', vip_map.get('id_ipv6'))
raise InvalidValueError(
None, 'id_ipv6', vip_map.get('id_ipv6'))
# Valid Vip validated
if not is_valid_boolean_param(vip_map.get('validado')):
self.log.error(
u'The validated parameter is not a valid value: %s.', vip_map.get('validado'))
raise InvalidValueError(
None, 'validated', vip_map.get('validado'))
# Valid Vip vip_created
if not is_valid_boolean_param(vip_map.get('vip_criado')):
self.log.error(
u'The vip_created parameter is not a valid value: %s.', vip_map.get('vip_criado'))
raise InvalidValueError(
None, 'vip_created', vip_map.get('vip_criado'))
# Valid maxcon
if not is_valid_int_greater_equal_zero_param(vip_map.get('maxcon')):
self.log.error(
u'The maxcon parameter is not a valid value: %s.', vip_map.get('maxcon'))
raise InvalidValueError(None, 'maxcon', vip_map.get('maxcon'))
# Existing Vip ID
vip = RequisicaoVips.get_by_pk(vip_id)
with distributedlock(LOCK_VIP % vip_id):
# Valid Vip created
if vip.vip_criado:
self.log.error(
u'The IP of the request for VIP %d can not be changed because the VIP is already created.' % vip.id)
raise RequisicaoVipsAlreadyCreatedError(None)
# Get variables
variables_map = vip.variables_to_map()
# Valid variables
vip.set_variables(variables_map)
evip = EnvironmentVip.get_by_values(variables_map.get(
'finalidade'), variables_map.get('cliente'), variables_map.get('ambiente'))
#.........这里部分代码省略.........
示例10: handle_get
# 需要导入模块: from networkapi.requisicaovips.models import RequisicaoVips [as 别名]
# 或者: from networkapi.requisicaovips.models.RequisicaoVips import get_by_pk [as 别名]
def handle_get(self, request, user, *args, **kwargs):
"""Rollback of the filter
URLs: /vip/l7/<id_vip>/rollback/
"""
self.log.info("Applies the last working filter to VIP")
try:
id_vip = kwargs.get('id_vip')
# User is authorized
if not has_perm(user, AdminPermission.VIP_ALTER_SCRIPT, AdminPermission.WRITE_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
raise UserNotAuthorizedError(None)
# Valid Vip ID
if not is_valid_int_greater_zero_param(id_vip):
self.log.error(
u'The vip_id parameter is not a valid value: %s.', id_vip)
raise InvalidValueError(None)
# Get VIP data
vip = RequisicaoVips.get_by_pk(id_vip)
with distributedlock(LOCK_VIP % id_vip):
# backup do vip
vip_old = clone(vip)
# Vip must be created
if not vip.vip_criado:
self.log.error(
u'Filter can not be applied because VIP has not been created yet.')
raise RequestVipsNotBeenCreatedError(None)
# salva data do rollback, rollback para aplicado, passa o
# aplicado para l7
vip.applied_l7_datetime = datetime.now().strftime(
"%Y-%m-%d %H:%M:%S")
# Set Applied With Rollback
vip.filter_applied = vip_old.filter_rollback
vip.rule_applied = vip_old.rule_rollback
# Set Rollback With Applied
vip.filter_rollback = vip_old.filter_applied
vip.rule_rollback = vip_old.rule_applied
vip.save(user, commit=True)
# roda script
command = 'gerador_vips -i %d --l7_filter_current' % vip.id
code, stdout, stderr = exec_script(command)
# code 0 = executou com sucesso
if code == 0:
success_map = dict()
success_map['codigo'] = '%04d' % code
success_map['descricao'] = {
'stdout': stdout, 'stderr': stderr}
map = dict()
map['sucesso'] = success_map
return self.response(dumps_networkapi(map))
else:
# pega os dados anteriores e os salva no banco
vip_old.save(user, commit=True)
return self.response_error(2, stdout + stderr)
except XMLError, x:
self.log.error(u'Error reading the XML request.')
return self.response_error(3, x)
示例11: handle_get
# 需要导入模块: from networkapi.requisicaovips.models import RequisicaoVips [as 别名]
# 或者: from networkapi.requisicaovips.models.RequisicaoVips import get_by_pk [as 别名]
def handle_get(self, request, user, *args, **kwargs):
"""Treat requests GET to list all rules by Environment Vip.
URL: environment-vip/get/rules/<id_evip>
"""
try:
self.log.info('GET to list all the Rules by Environment Vip.')
# User permission
if not has_perm(user, AdminPermission.VIPS_REQUEST, AdminPermission.READ_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
raise UserNotAuthorizedError(None)
id_environment_vip = kwargs.get('id_evip')
id_vip = kwargs.get('id_vip')
# Valid Environment VIP ID
if not is_valid_int_greater_zero_param(id_environment_vip):
self.log.error(
u'The id_environment_vip parameter is not a valid value: %s.', id_environment_vip)
raise InvalidValueError(
None, 'id_environment_vip', id_environment_vip)
# Find Environment VIP by ID to check if it exist
environment_vip = EnvironmentVip.get_by_pk(id_environment_vip)
envs = list()
for net4 in environment_vip.networkipv4_set.all():
if net4.vlan.ambiente.id not in envs:
envs.append(net4.vlan.ambiente.id)
for net6 in environment_vip.networkipv6_set.all():
if net6.vlan.ambiente.id not in envs:
envs.append(net6.vlan.ambiente.id)
if id_vip:
if not is_valid_int_greater_zero_param(id_vip):
self.log.error(
u'Parameter id_vip is invalid. Value: %s.', id_vip)
raise InvalidValueError(None, 'id_vip', id_vip)
vip = RequisicaoVips.get_by_pk(id_vip)
rules = Rule.objects.filter(environment__id__in=envs).filter(
Q(vip=vip) | Q(vip=None))
else:
rules = Rule.objects.filter(environment__id__in=envs, vip=None)
rules_dict = dict()
rules_list = []
rules_list.append({'id': u'', 'name_rule_opt': u''})
for rule in rules:
rules_dict['name_rule_opt'] = rule.name
rules_dict['id'] = rule.id
rules_list.append(rules_dict)
rules_dict = dict()
return self.response(dumps_networkapi({'name_rule_opt': rules_list}))
except UserNotAuthorizedError:
return self.not_authorized()
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例12: administrate_real
# 需要导入模块: from networkapi.requisicaovips.models import RequisicaoVips [as 别名]
# 或者: from networkapi.requisicaovips.models.RequisicaoVips import get_by_pk [as 别名]
def administrate_real(self, user, vip_id, equip_id, ip_id, operation, network_version, port_vip=None, port_real=None):
# Valid VIP ID
if not is_valid_int_greater_zero_param(vip_id):
self.log.error(
u'The vip_id parameter is not a valid value: %s.', vip_id)
raise InvalidValueError(None, 'vip_id', vip_id)
# Valid Equipament ID
if not is_valid_int_greater_zero_param(equip_id):
self.log.error(
u'The equip_id parameter is not a valid value: %s.', equip_id)
raise InvalidValueError(None, 'equip_id', equip_id)
# Valid IP ID
if not is_valid_int_greater_zero_param(ip_id):
self.log.error(
u'The ip_id parameter is not a valid value: %s.', ip_id)
raise InvalidValueError(None, 'ip_id', ip_id)
# Valid operation
if operation not in ['add', 'del', 'ena', 'dis', 'chk']:
self.log.error(
u'The operation parameter is not a valid value: %s.', operation)
raise InvalidValueError(None, 'operation', operation)
# Valid network version
if network_version not in ['v4', 'v6']:
self.log.error(
u'The network_version parameter is not a valid value: %s.', network_version)
raise InvalidValueError(None, 'network_version', network_version)
# User permission
if (operation == 'chk'):
if not has_perm(user, AdminPermission.VIP_ALTER_SCRIPT, AdminPermission.READ_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
raise UserNotAuthorizedError(None)
else:
if not has_perm(user, AdminPermission.VIP_ALTER_SCRIPT, AdminPermission.WRITE_OPERATION, None, equip_id, AdminPermission.EQUIP_UPDATE_CONFIG_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
raise UserNotAuthorizedError(None)
# new_call = True - New calls for Add/Del/Enable/Disable/Check with new params (Port Vip and Port Real)
# new_call = False = Old calls for compatibility
new_call = False
if port_vip != None and port_real != None:
# Valid ports
if not is_valid_int_greater_zero_param(port_vip):
self.log.error(
u'The port_vip parameter is not a valid value: %s.', port_vip)
raise InvalidValueError(None, 'port_vip', port_vip)
if not is_valid_int_greater_zero_param(port_real):
self.log.error(
u'The port_vip parameter is not a valid value: %s.', port_real)
raise InvalidValueError(None, 'port_real', port_real)
new_call = True
# Find Request VIP by ID to check if it exist
vip = RequisicaoVips.get_by_pk(vip_id)
# Get variables
variables_map = vip.variables_to_map()
# Valid variables
#vip.set_variables(variables_map)
evip = EnvironmentVip.get_by_values(variables_map.get('finalidade'), variables_map.get('cliente'), variables_map.get('ambiente'))
# Valid network_version - IPv4
if network_version == IP_VERSION.IPv4[0]:
# Find IpEquipamento to check if it exist
IpEquip = IpEquipamento().get_by_ip_equipment(ip_id, equip_id)
real_name = IpEquip.equipamento.nome
end_ip = "%s.%s.%s.%s" % (
IpEquip.ip.oct1, IpEquip.ip.oct2, IpEquip.ip.oct3, IpEquip.ip.oct4)
# Valid Real
RequisicaoVips.valid_real_server(end_ip, IpEquip.equipamento, evip, False)
# Valid network_version - IPv6
elif network_version == IP_VERSION.IPv6[0]:
# Find Ipv6Equipament to check if it exist
Ipv6Equip = Ipv6Equipament().get_by_ip_equipment(ip_id, equip_id)
real_name = Ipv6Equip.equipamento.nome
end_ip = "%s:%s:%s:%s:%s:%s:%s:%s" % (Ipv6Equip.ip.block1, Ipv6Equip.ip.block2, Ipv6Equip.ip.block3,
Ipv6Equip.ip.block4, Ipv6Equip.ip.block5, Ipv6Equip.ip.block6, Ipv6Equip.ip.block7, Ipv6Equip.ip.block8)
# Valid Real
RequisicaoVips.valid_real_server(end_ip, Ipv6Equip.equipamento, evip, False)
if (operation == 'chk'):
if IP_VERSION.IPv4[0] == network_version:
if new_call:
#.........这里部分代码省略.........
示例13: handle_post
# 需要导入模块: from networkapi.requisicaovips.models import RequisicaoVips [as 别名]
# 或者: from networkapi.requisicaovips.models.RequisicaoVips import get_by_pk [as 别名]
def handle_post(self, request, user, *args, **kwargs):
'''Treat POST requests to run remove script for vip
URL: vip/remove/
'''
try:
# Commons Validations
# User permission
if not has_perm(user, AdminPermission.VIP_REMOVE_SCRIPT, AdminPermission.WRITE_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
return self.not_authorized()
# Business Validations
# 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:
msg = u'There is no value to the networkapi tag of XML request.'
self.log.error(msg)
return self.response_error(3, msg)
vip_map = networkapi_map.get('vip')
if vip_map is None:
msg = u'There is no value to the vlan tag of XML request.'
self.log.error(msg)
return self.response_error(3, msg)
# Get XML data
vip_id = vip_map.get('id_vip')
# Valid vip ID
if not is_valid_int_greater_zero_param(vip_id):
self.log.error(
u'Parameter id_vip is invalid. Value: %s.', vip_id)
raise InvalidValueError(None, 'id_vip', vip_id)
# Vip must exists in database
vip = RequisicaoVips.get_by_pk(vip_id)
with distributedlock(LOCK_VIP % vip_id):
# Equipment permissions
if vip.ip is not None:
for ip_equipment in vip.ip.ipequipamento_set.all():
if not has_perm(user, AdminPermission.VIP_CREATE_SCRIPT, AdminPermission.WRITE_OPERATION, None, ip_equipment.equipamento_id, AdminPermission.EQUIP_UPDATE_CONFIG_OPERATION):
return self.not_authorized()
if vip.ipv6 is not None:
for ip_equipment in vip.ipv6.ipv6equipament_set.all():
if not has_perm(user, AdminPermission.VIP_CREATE_SCRIPT, AdminPermission.WRITE_OPERATION, None, ip_equipment.equipamento_id, AdminPermission.EQUIP_UPDATE_CONFIG_OPERATION):
return self.not_authorized()
# Must be validated
if not vip.validado:
return self.response_error(191, vip_id)
# Must be created
if not vip.vip_criado:
return self.response_error(322, vip_id)
# Business Rules
# Make command
command = VIP_REMOVE % (vip.id)
# Execute command
code, stdout, stderr = exec_script(command)
if code == 0:
success_map = dict()
success_map['codigo'] = '%04d' % code
success_map['descricao'] = {
'stdout': stdout, 'stderr': stderr}
vip.vip_criado = 0
vip.save()
#Marks the server pool as not created if the
# server pool is not used in another already created vip request
server_pools = ServerPool.objects.filter(vipporttopool__requisicao_vip=vip.id)
for server_pool in server_pools:
#Checks if server pool is still used in another created vip request
server_pools_still_used = VipPortToPool.objects.filter(server_pool=server_pool).exclude(requisicao_vip=vip.id)
vip_with_server_pool_is_created = 0
for server_pool_still_used in server_pools_still_used:
if server_pool_still_used.requisicao_vip.vip_criado:
vip_with_server_pool_is_created = 1
if not vip_with_server_pool_is_created and server_pool.pool_created:
server_pool.pool_created = 0
server_pool.save()
map = dict()
#.........这里部分代码省略.........
示例14: update_vip_request
# 需要导入模块: from networkapi.requisicaovips.models import RequisicaoVips [as 别名]
# 或者: from networkapi.requisicaovips.models.RequisicaoVips import get_by_pk [as 别名]
def update_vip_request(vip_id, vip_map, user):
log = logging.getLogger('update_vip_request')
if not has_perm(user,
AdminPermission.VIPS_REQUEST,
AdminPermission.WRITE_OPERATION):
raise UserNotAuthorizedError(
None, u'Usuário não tem permissão para executar a operação.')
healthcheck_expect_id = vip_map.get('id_healthcheck_expect')
if healthcheck_expect_id is not None:
if not is_valid_int_greater_zero_param(healthcheck_expect_id):
log.error(
u'The healthcheck_expect_id parameter is not a valid value: %s.', healthcheck_expect_id)
raise InvalidValueError(
None, 'healthcheck_expect_id', healthcheck_expect_id)
else:
healthcheck_expect_id = int(healthcheck_expect_id)
ip_id = vip_map.get('id_ip')
if not is_valid_int_greater_zero_param(ip_id):
log.error(u'The ip_id parameter is not a valid value: %s.', ip_id)
raise InvalidValueError(None, 'ip_id', ip_id)
else:
ip_id = int(ip_id)
traffic_id = vip_map.get('trafficreturn')
if not is_valid_int_greater_zero_param(traffic_id):
log.error(u'The traffic_id parameter is not a valid value: %s.', traffic_id)
raise InvalidValueError(None, 'trafficreturn', traffic_id)
else:
traffic_id = int(traffic_id)
validated = vip_map.get('validado')
if validated is None:
return 246
if validated == '0':
validated = False
elif validated == '1':
validated = True
else:
return 244
vip_created = vip_map.get('vip_criado')
if vip_created is None:
return 247
if vip_created == '0':
vip_created = False
elif vip_created == '1':
vip_created = True
else:
return 245
# Valid maxcon
if not is_valid_int_greater_equal_zero_param(vip_map.get('maxcon')):
log.error(
u'The maxcon parameter is not a valid value: %s.', vip_map.get('maxcon'))
raise InvalidValueError(None, 'maxcon', vip_map.get('maxcon'))
code = RequisicaoVips.update(user,
vip_id,
vip_map,
healthcheck_expect_id=healthcheck_expect_id,
ip_id=ip_id,
vip_criado=vip_created,
validado=validated,
traffic_return_id=traffic_id)
if code is not None:
return code
# SYNC_VIP
vip = RequisicaoVips.get_by_pk(vip_id)
old_to_new(vip)
return 0
示例15: __create_vip
# 需要导入模块: from networkapi.requisicaovips.models import RequisicaoVips [as 别名]
# 或者: from networkapi.requisicaovips.models.RequisicaoVips import get_by_pk [as 别名]
def __create_vip(self, vip_id, user):
# Valid vip ID
if not is_valid_int_greater_zero_param(vip_id):
self.log.error(u'Parameter id_vip is invalid. Value: %s.', vip_id)
raise InvalidValueError(None, 'id_vip', vip_id)
with distributedlock(LOCK_VIP % vip_id):
# Vip must exists in database
vip = RequisicaoVips.get_by_pk(vip_id)
# Equipment permissions
if vip.ip is not None:
for ip_equipment in vip.ip.ipequipamento_set.all():
if not has_perm(user, AdminPermission.VIP_CREATE_SCRIPT, AdminPermission.WRITE_OPERATION, None, ip_equipment.equipamento_id, AdminPermission.EQUIP_UPDATE_CONFIG_OPERATION):
return self.not_authorized()
if vip.ipv6 is not None:
for ip_equipment in vip.ipv6.ipv6equipament_set.all():
if not has_perm(user, AdminPermission.VIP_CREATE_SCRIPT, AdminPermission.WRITE_OPERATION, None, ip_equipment.equipamento_id, AdminPermission.EQUIP_UPDATE_CONFIG_OPERATION):
return self.not_authorized()
# Must be validated
if not vip.validado:
return self.response_error(191, vip_id)
# Must be created
if vip.vip_criado:
return self.response_error(192, vip_id)
# Business Rules
# Make command
command = VIP_CREATE % (vip.id)
# Execute command
code, stdout, stderr = exec_script(command)
if code == 0:
success_map = dict()
success_map['codigo'] = '%04d' % code
success_map['descricao'] = {'stdout': stdout, 'stderr': stderr}
vip.rule_applied = vip.rule
vip.filter_applied = vip.l7_filter
vip.l7_filter = None
vip.rule = None
vip.filter_valid = False
vip.vip_criado = 1
vip.save()
# SYNC_VIP
old_to_new(vip)
server_pools = ServerPool.objects.filter(
vipporttopool__requisicao_vip=vip.id)
for server_pool in server_pools:
if not server_pool.pool_created:
server_pool.pool_created = 1
server_pool.save()
map = dict()
map['sucesso'] = success_map
else:
return self.response_error(2, stdout + stderr)
# Return XML
return self.response(dumps_networkapi(map))