本文整理汇总了Python中networkapi.ambiente.models.EnvironmentVip.get_by_pk方法的典型用法代码示例。如果您正苦于以下问题:Python EnvironmentVip.get_by_pk方法的具体用法?Python EnvironmentVip.get_by_pk怎么用?Python EnvironmentVip.get_by_pk使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkapi.ambiente.models.EnvironmentVip
的用法示例。
在下文中一共展示了EnvironmentVip.get_by_pk方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_delete
# 需要导入模块: from networkapi.ambiente.models import EnvironmentVip [as 别名]
# 或者: from networkapi.ambiente.models.EnvironmentVip import get_by_pk [as 别名]
def handle_delete(self, request, user, *args, **kwargs):
"""
Handles DELETE requests to create a relationship of Environment with EnvironmentVip.
URL: environment/<environment_id>/environmentvip/<environment_vip_id>/
"""
self.log.info("Remove a relationship of Environment with EnvironmentVip")
try:
# Commons Validations
# User permission
if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION):
self.log.error(u"User does not have permission to perform the operation.")
raise UserNotAuthorizedError(None)
# Valid Environment
environment_id = kwargs.get("environment_id")
if not is_valid_int_greater_zero_param(environment_id):
self.log.error(u"The environment_id parameter is not a valid value: %s.", environment_id)
raise InvalidValueError(None, "environment_id", environment_id)
# Valid EnvironmentVip ID
environment_vip_id = kwargs.get("environment_vip_id")
if not is_valid_int_greater_zero_param(environment_vip_id):
self.log.error(u"The id_environment_vip parameter is not a valid value: %s.", environment_vip_id)
raise InvalidValueError(None, "environment_vip_id", environment_vip_id)
# Business Validations
# Existing Environment ID
environment = Ambiente.get_by_pk(environment_id)
# Existing EnvironmentVip ID
environment_vip = EnvironmentVip.get_by_pk(environment_vip_id)
# Business Rules
environment_environment_vip = EnvironmentEnvironmentVip().get_by_environment_environment_vip(
environment.id, environment_vip.id
)
server_pool_list = EnvironmentEnvironmentVip.get_server_pool_member_by_environment_environment_vip(
environment_environment_vip
)
# Valid integraty between environment/environmentvip related with reals
# if exists reals fot this environment then raise a exception
if server_pool_list:
raise EnvironmentEnvironmentServerPoolLinked({"environment": environment.name})
# Delete
environment_environment_vip.delete()
# Return nothing
return self.response(dumps_networkapi({}))
except UserNotAuthorizedError:
return self.not_authorized()
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
开发者ID:marcelometal,项目名称:GloboNetworkAPI,代码行数:61,代码来源:EnvironmentEnvironmentVipAssociationResource.py
示例2: handle_delete
# 需要导入模块: from networkapi.ambiente.models import EnvironmentVip [as 别名]
# 或者: from networkapi.ambiente.models.EnvironmentVip import get_by_pk [as 别名]
def handle_delete(self, request, user, *args, **kwargs):
"""
Handles DELETE requests to remove a relationship of OptionVip with EnvironmentVip.
URL: optionvip/<id_option_vip>/environmentvip/<id_environment_vip>/
"""
self.log.info("Remove a relationship of OptionVip with EnvironmentVip")
try:
# Commons Validations
# User permission
if not has_perm(user, AdminPermission.OPTION_VIP, AdminPermission.WRITE_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
raise UserNotAuthorizedError(None)
# Valid OptionVip ID
option_vip_id = kwargs.get('id_option_vip')
if not is_valid_int_greater_zero_param(option_vip_id):
self.log.error(
u'The id_option_vip parameter is not a valid value: %s.', option_vip_id)
raise InvalidValueError(None, 'id_option_vip', option_vip_id)
# Valid EnvironmentVip ID
environment_vip_id = kwargs.get('id_environment_vip')
if not is_valid_int_greater_zero_param(environment_vip_id):
self.log.error(
u'The id_environment_vip parameter is not a valid value: %s.', environment_vip_id)
raise InvalidValueError(
None, 'id_environment_vip', environment_vip_id)
# Business Validations
# Existing OptionVip ID
option_vip = OptionVip.get_by_pk(option_vip_id)
# Existing EnvironmentVip ID
environment_vip = EnvironmentVip.get_by_pk(environment_vip_id)
# Business Rules
# Find
opt_vip_env_vip = OptionVipEnvironmentVip().get_by_option_environment(
option_vip.id, environment_vip.id)
# Delete
opt_vip_env_vip.delete()
# Return nothing
return self.response(dumps_networkapi({}))
except UserNotAuthorizedError:
return self.not_authorized()
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例3: handle_get
# 需要导入模块: from networkapi.ambiente.models import EnvironmentVip [as 别名]
# 或者: from networkapi.ambiente.models.EnvironmentVip import get_by_pk [as 别名]
def handle_get(self, request, user, *args, **kwargs):
"""Treat requests GET to list all the VIPs related to Environment VIP.
URL: environmentvip/<id_environment_vip>/vip/all'
"""
try:
self.log.info(
"GET to list all the VIPs related to Environment VIP")
# User permission
if not has_perm(user, AdminPermission.ENVIRONMENT_VIP, AdminPermission.READ_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
raise UserNotAuthorizedError(None)
# Get data
id_environment_vip = kwargs.get('id_environment_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)
# Find Request VIP - IPv4 by ID Environment
vips_ipv4 = RequisicaoVips.objects.filter(
ip__networkipv4__ambient_vip__id=environment_vip.id)
# Find Request VIP - IPv6 by ID Environment
vips_ipv6 = RequisicaoVips.objects.filter(
ipv6__networkipv6__ambient_vip__id=environment_vip.id)
vips = {}
for vips_ip in [vips_ipv4, vips_ipv6]:
for vip in vips_ip:
v = {}
v = vip.variables_to_map()
v['id'] = vip.id
v['validado'] = vip.validado
v['vip_criado'] = vip.vip_criado
v['id_ip'] = vip.ip_id
v['id_ipv6'] = vip.ipv6_id
v['id_healthcheck_expect'] = vip.healthcheck_expect_id
vips['vip_%s' % (vip.id)] = v
return self.response(dumps_networkapi(vips))
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例4: handle_put
# 需要导入模块: from networkapi.ambiente.models import EnvironmentVip [as 别名]
# 或者: from networkapi.ambiente.models.EnvironmentVip import get_by_pk [as 别名]
def handle_put(self, request, user, *args, **kwargs):
"""Treat requests PUT to change Environment VIP.
URL: environmentvip/<id_environment_vip>/
"""
try:
self.log.info('Change Environment VIP')
id_environment_vip = kwargs.get('id_environment_vip')
# User permission
if not has_perm(user, AdminPermission.ENVIRONMENT_VIP, 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.')
environmentvip_map = networkapi_map.get('environment_vip')
if environmentvip_map is None:
return self.response_error(3, u'There is no value to the environment_vip tag of XML request.')
# 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)
with distributedlock(LOCK_ENVIRONMENT_VIP % id_environment_vip):
# Valid Environment Vip
environment_vip.valid_environment_vip(environmentvip_map)
try:
# Update Environment Vip
environment_vip.save()
except Exception, e:
self.log.error(u'Failed to update the environment vip.')
raise EnvironmentVipError(
e, u'Failed to update the environment vip')
return self.response(dumps_networkapi({}))
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例5: handle_delete
# 需要导入模块: from networkapi.ambiente.models import EnvironmentVip [as 别名]
# 或者: from networkapi.ambiente.models.EnvironmentVip import get_by_pk [as 别名]
def handle_delete(self, request, user, *args, **kwargs):
"""Treat requests PUT to delete Environment VIP.
URL: environmentvip/<id_environment_vip>/
"""
try:
self.log.info('Delete Environment VIP')
id_environment_vip = kwargs.get('id_environment_vip')
# User permission
if not has_perm(user, AdminPermission.ENVIRONMENT_VIP, AdminPermission.WRITE_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
raise UserNotAuthorizedError(None)
# 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)
with distributedlock(LOCK_ENVIRONMENT_VIP % id_environment_vip):
# Find networkIPv4 by Environment VIP to check if is greater
# than zero
if len(NetworkIPv4.objects.filter(ambient_vip=environment_vip.id)) > 0:
return self.response_error(284)
# Find networkIPv6 by Environment VIP to check if is greater
# than zero
if len(NetworkIPv6.objects.filter(ambient_vip=environment_vip.id)) > 0:
return self.response_error(285)
try:
# Delete Environment Vip
environment_vip.delete()
except Exception, e:
self.log.error(u'Failed to delete the environment vip.')
raise EnvironmentVipError(
e, u'Failed to delete the environment vip')
return self.response(dumps_networkapi({}))
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例6: handle_get
# 需要导入模块: from networkapi.ambiente.models import EnvironmentVip [as 别名]
# 或者: from networkapi.ambiente.models.EnvironmentVip import get_by_pk [as 别名]
def handle_get(self, request, user, *args, **kwargs):
"""Treat requests GET to list all the Environment by Environment Vip.
URL: environment/environmentvip/<environment_vip_id>'
"""
try:
self.log.info(
'GET to list all the Environment by Environment Vip.')
# User permission
if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT, AdminPermission.READ_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
raise UserNotAuthorizedError(None)
environment_vip_id = kwargs.get('environment_vip_id')
# Valid Environment VIP ID
if not is_valid_int_greater_zero_param(environment_vip_id):
self.log.error(
u'The id_environment_vip parameter is not a valid value: %s.', environment_vip_id)
raise InvalidValueError(
None, 'environment_vip_id', environment_vip_id)
# Find Environment VIP by ID to check if it exist
environment_vip = EnvironmentVip.get_by_pk(environment_vip_id)
environment_related_list = []
for env_env_vip in environment_vip.environmentenvironmentvip_set.all():
environment_map = {}
environment_map['environment_id'] = env_env_vip.environment.id
environment_map[
'environment_vip_id'] = env_env_vip.environment_vip.id
environment_map[
'environment'] = env_env_vip.environment.grupo_l3.nome
environment_map[
'ambiente_logico_name'] = env_env_vip.environment.ambiente_logico.nome
environment_map[
'divisao_dc_name'] = env_env_vip.environment.divisao_dc.nome
environment_related_list.append(environment_map)
return self.response(dumps_networkapi({'environment_related_list': environment_related_list}))
except UserNotAuthorizedError:
return self.not_authorized()
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例7: handle_get
# 需要导入模块: from networkapi.ambiente.models import EnvironmentVip [as 别名]
# 或者: from networkapi.ambiente.models.EnvironmentVip import get_by_pk [as 别名]
def handle_get(self, request, user, *args, **kwargs):
"""Treat requests GET to list all traffic return of the Option VIP by Environment Vip.
URL: environment-vip/get/trafficreturn/<id_evip>
"""
try:
self.log.info("GET to list all the Option VIP by Environment Vip - traffic return.")
# User permission
if not has_perm(user, AdminPermission.OPTION_VIP, 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')
# 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)
#self.log.info(str(environment_vip))
ovips = OptionVip.get_all_trafficreturn(environment_vip.id)
#self.log.info(str(ovips))
ovip_dict = dict()
ovip_list = []
for ovip in ovips:
ovip_dict['trafficreturn_opt'] = ovip.nome_opcao_txt
ovip_list.append(ovip_dict)
ovip_dict = dict()
self.log.info(str(ovip_list))
return self.response(dumps_networkapi({'trafficreturn_opt': ovip_list}))
except UserNotAuthorizedError:
return self.not_authorized()
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例8: handle_get
# 需要导入模块: from networkapi.ambiente.models import EnvironmentVip [as 别名]
# 或者: from networkapi.ambiente.models.EnvironmentVip import get_by_pk [as 别名]
def handle_get(self, request, user, *args, **kwargs):
"""Treat requests GET to list all the Option VIP by Environment Vip.
URL: optionvip/environmentvip/<id_environment_vip>'
"""
try:
self.log.info("GET to list all the Option VIP by Environment Vip.")
# User permission
if not has_perm(user, AdminPermission.OPTION_VIP, 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_environment_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)
ovips = []
for env in environment_vip.optionvipenvironmentvip_set.all():
ovips.append(model_to_dict(env.option))
return self.response(dumps_networkapi({'option_vip': ovips}))
except UserNotAuthorizedError:
return self.not_authorized()
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例9: handle_post
# 需要导入模块: from networkapi.ambiente.models import EnvironmentVip [as 别名]
# 或者: from networkapi.ambiente.models.EnvironmentVip import get_by_pk [as 别名]
def handle_post(self, request, user, *args, **kwargs):
"""Handles GET requests get an IP6 available for vip_request by evip_id.
URL: ip/availableip6/vip/id_evip
"""
self.log.info('Get an IP6 available for vip_request')
try:
# User permission
if not has_perm(user, AdminPermission.IPS, AdminPermission.WRITE_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
return self.not_authorized()
# Load XML data
xml_map, attrs_map = loads(request.raw_post_data)
# XML data format
networkapi_map = xml_map.get('networkapi')
ip_map = networkapi_map.get('ip_map')
# Get XML data
id_evip = ip_map.get('id_evip')
name = ip_map.get('name')
if not is_valid_int_greater_zero_param(id_evip):
self.log.error(
u'Parameter id_evip is invalid. Value: %s.', id_evip)
raise InvalidValueError(None, 'id_evip', id_evip)
# Business Rules
evip = EnvironmentVip.get_by_pk(id_evip)
with distributedlock(LOCK_GET_IPV6_AVAILABLE % id_evip):
ipv6 = Ipv6()
len_network = len(evip.networkipv6_set.all())
if len_network <= 0:
raise NetworkNotInEvip(
None, 'Não há rede no ambiente vip fornecido')
raise_not_found_balanceamento = False
cont_network = 0
cont_balanceador_not_found = 0
for net in evip.networkipv6_set.all():
balanceador_found_flag = False
cont_network = cont_network + 1
list_ips_equips = list()
try:
ip_available = ipv6.get_available_ip6(net.id)
ip_new = Ipv6()
ip_available = ip_available.split(':')
ip_new.block1 = ip_available[0]
ip_new.block2 = ip_available[1]
ip_new.block3 = ip_available[2]
ip_new.block4 = ip_available[3]
ip_new.block5 = ip_available[4]
ip_new.block6 = ip_available[5]
ip_new.block7 = ip_available[6]
ip_new.block8 = ip_available[7]
ip_new.description = name
for env_equipment in net.vlan.ambiente.equipamentoambiente_set.all():
equipment = env_equipment.equipamento
if equipment.tipo_equipamento == TipoEquipamento.get_tipo_balanceador():
if equipment.id not in list_ips_equips:
list_ips_equips.append(equipment.id)
if ip_new.id is None:
ip_new.save_ipv6(
equipment.id, user, net)
else:
new_ip_equip = Ipv6Equipament()
new_ip_equip.ip = ip_new
new_ip_equip.equipamento = equipment
new_ip_equip.save()
balanceador_found_flag = True
if not balanceador_found_flag:
cont_balanceador_not_found = cont_balanceador_not_found + \
1
else:
break
if cont_balanceador_not_found == len_network:
raise_not_found_balanceamento = True
raise IpNotAvailableError(None, 'Não há ipv6 disponivel para as redes associadas com o '
'Ambiente Vip: %s - %s - %s, pois não existe equipamentos '
'do Tipo Balanceador nessas redes.'
% (evip.finalidade_txt, evip.cliente_txt, evip.ambiente_p44_txt))
#.........这里部分代码省略.........
示例10: handle_post
# 需要导入模块: from networkapi.ambiente.models import EnvironmentVip [as 别名]
# 或者: from networkapi.ambiente.models.EnvironmentVip import get_by_pk [as 别名]
def handle_post(self, request, user, *args, **kwargs):
'''Handles GET requests get an IP4 available for vip_request by evip_id.
URL: ip/availableip6/vip/id_evip/
'''
self.log.info('Get an IP4 available for vip_request')
try:
# User permission
if not has_perm(user, AdminPermission.IPS, AdminPermission.WRITE_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
return self.not_authorized()
# Load XML data
xml_map, attrs_map = loads(request.raw_post_data)
# XML data format
networkapi_map = xml_map.get('networkapi')
ip_map = networkapi_map.get('ip_map')
# Get XML data
id_evip = ip_map.get('id_evip')
name = ip_map.get('name')
if not is_valid_int_greater_zero_param(id_evip):
self.log.error(
u'Parameter id_evip is invalid. Value: %s.', id_evip)
raise InvalidValueError(None, 'id_evip', id_evip)
# Business Rules
evip = EnvironmentVip.get_by_pk(id_evip)
ipv4 = Ip()
len_network = len(evip.networkipv4_set.all())
raise_not_found_balanceamento = False
if (len_network <= 0):
raise NetworkNotInEvip(
None, 'Não há rede no ambiente vip fornecido')
cont_network = 0
cont_balanceador_not_found = 0
for net in evip.networkipv4_set.all():
balanceador_found_flag = False
cont_network = cont_network + 1
list_ips_equips = list()
try:
ip_available = ipv4.get_available_ip(net.id)
ip_new = Ip()
ip_available = ip_available.exploded
ip_available = ip_available.split(".")
ip_new.oct1 = ip_available[0]
ip_new.oct2 = ip_available[1]
ip_new.oct3 = ip_available[2]
ip_new.oct4 = ip_available[3]
ip_new.descricao = name
for env_equipment in net.vlan.ambiente.equipamentoambiente_set.all():
equipment = env_equipment.equipamento
if equipment.tipo_equipamento == TipoEquipamento.get_tipo_balanceador():
if equipment.id not in list_ips_equips:
list_ips_equips.append(equipment.id)
if ip_new.id is None:
ip_new.save_ipv4(equipment.id, user, net)
else:
new_ip_equip = IpEquipamento()
new_ip_equip.ip = ip_new
new_ip_equip.equipamento = equipment
new_ip_equip.save(user)
balanceador_found_flag = True
if not balanceador_found_flag:
cont_balanceador_not_found = cont_balanceador_not_found + \
1
else:
break
if cont_balanceador_not_found == len_network:
raise_not_found_balanceamento = True
raise IpNotAvailableError(None, "Não há ipv4 disponivel para as redes associdas com o Ambiente Vip: %s - %s - %s, pois não existe equipamentos do Tipo Balanceador nessas redes." % (
evip.finalidade_txt, evip.cliente_txt, evip.ambiente_p44_txt))
except (IpNotAvailableError, IpRangeAlreadyAssociation), e:
cont_balanceador_not_found = cont_balanceador_not_found + 1
if raise_not_found_balanceamento:
raise IpNotAvailableError(None, e.message)
elif len_network == cont_network:
raise IpNotAvailableError(None, "Não há ipv4 disponivel para as redes associdas com o Ambiente Vip: %s - %s - %s" % (
#.........这里部分代码省略.........
示例11: handle_get
# 需要导入模块: from networkapi.ambiente.models import EnvironmentVip [as 别名]
# 或者: from networkapi.ambiente.models.EnvironmentVip 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: handle_put
# 需要导入模块: from networkapi.ambiente.models import EnvironmentVip [as 别名]
# 或者: from networkapi.ambiente.models.EnvironmentVip import get_by_pk [as 别名]
def handle_put(self, request, user, *args, **kwargs):
"""
Handles PUT requests to create a relationship of OptionVip with EnvironmentVip.
URL: optionvip/<id_option_vip>/environmentvip/<id_environment_vip>/
"""
self.log.info("Create a relationship of OptionVip with EnvironmentVip")
try:
# Commons Validations
# User permission
if not has_perm(user, AdminPermission.OPTION_VIP, AdminPermission.WRITE_OPERATION):
self.log.error(u"User does not have permission to perform the operation.")
raise UserNotAuthorizedError(None)
# Valid OptionVip ID
option_vip_id = kwargs.get("id_option_vip")
if not is_valid_int_greater_zero_param(option_vip_id):
self.log.error(u"The id_option_vip parameter is not a valid value: %s.", option_vip_id)
raise InvalidValueError(None, "id_option_vip", option_vip_id)
# Valid EnvironmentVip ID
environment_vip_id = kwargs.get("id_environment_vip")
if not is_valid_int_greater_zero_param(environment_vip_id):
self.log.error(u"The id_environment_vip parameter is not a valid value: %s.", environment_vip_id)
raise InvalidValueError(None, "id_environment_vip", environment_vip_id)
# Business Validations
# Existing OptionVip ID
option_vip = OptionVip.get_by_pk(option_vip_id)
# Existing EnvironmentVip ID
environment_vip = EnvironmentVip.get_by_pk(environment_vip_id)
with distributedlock(LOCK_ENVIRONMENT_VIP % environment_vip_id):
# Business Rules
# Set new values
opt_vip_env_vip = OptionVipEnvironmentVip()
opt_vip_env_vip.option = option_vip
opt_vip_env_vip.environment = environment_vip
# Existing OptionVipEnvironmentVip
opt_vip_env_vip.validate()
# Persist
opt_vip_env_vip.save(user)
# Return XML
opt_vip_env_vip_map = dict()
opt_vip_env_vip_map["opcoesvip_ambiente_xref"] = model_to_dict(opt_vip_env_vip, fields=["id"])
return self.response(dumps_networkapi(opt_vip_env_vip_map))
except UserNotAuthorizedError:
return self.not_authorized()
except InvalidValueError, e:
return self.response_error(269, e.param, e.value)
示例13: handle_post
# 需要导入模块: from networkapi.ambiente.models import EnvironmentVip [as 别名]
# 或者: from networkapi.ambiente.models.EnvironmentVip import get_by_pk [as 别名]
def handle_post(self, request, user, *args, **kwargs):
'''Handles POST requests to check an IPv4 or Ipv6 for vip request.
URL: ip/checkvipip/
'''
self.log.info('Check a Ipv4 or Ipv6 for Vip')
try:
# 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)
ip_map = networkapi_map.get('ip_map')
if ip_map is None:
msg = u'There is no value to the ip tag of XML request.'
self.log.error(msg)
return self.response_error(3, msg)
# Get XML data
ip = ip_map.get('ip')
id_evip = ip_map.get('id_evip')
# User permission
if not has_perm(user, AdminPermission.IPS, AdminPermission.READ_OPERATION):
self.log.error(
u'User does not have permission to perform the operation.')
return self.not_authorized()
# Valid ip id
if ip is None:
self.log.error(u'Parameter ip is invalid. Value: %s.', ip)
raise InvalidValueError(None, 'ip', ip)
# Valid evip id
if not is_valid_int_greater_zero_param(id_evip):
raise InvalidValueError(None, 'id_evip', id_evip)
# Business Rules
evip = EnvironmentVip.get_by_pk(id_evip)
ip_list = ip.split(".")
if len(ip_list) == 1:
if not is_valid_ipv6(ip):
self.log.error(u'Parameter ip is invalid. Value: %s.', ip)
raise InvalidValueError(None, 'ip', ip)
if len(evip.networkipv6_set.all()) <= 0:
raise NetworkNotInEvip(
'IPv6', 'Não há rede no ambiente vip fornecido')
ip_list = ip.split(":")
ip_checked = Ipv6.get_by_octs_and_environment_vip(ip_list[0], ip_list[1], ip_list[
2], ip_list[3], ip_list[4], ip_list[5], ip_list[6], ip_list[7], id_evip)
ip_ok = False
for ip_equip in ip_checked.ipv6equipament_set.all():
if ip_equip.equipamento.tipo_equipamento == TipoEquipamento.get_tipo_balanceador():
ip_ok = True
break
if not ip_ok:
raise IpNotAvailableError(
None, "Ipv6 indisponível para o Ambiente Vip: %s, pois não existe equipamento do Tipo Balanceador relacionado a este Ip." % evip.show_environment_vip())
else:
if not is_valid_ipv4(ip):
self.log.error(u'Parameter ip is invalid. Value: %s.', ip)
raise InvalidValueError(None, 'ip', ip)
if len(evip.networkipv4_set.all()) <= 0:
raise NetworkNotInEvip(
'IPv4', 'Não há rede no ambiente vip fornecido')
ip_checked = Ip.get_by_octs_and_environment_vip(
ip_list[0], ip_list[1], ip_list[2], ip_list[3], id_evip)
ip_ok = False
for ip_equip in ip_checked.ipequipamento_set.all():
if ip_equip.equipamento.tipo_equipamento == TipoEquipamento.get_tipo_balanceador():
ip_ok = True
break
#.........这里部分代码省略.........
示例14: handle_post
# 需要导入模块: from networkapi.ambiente.models import EnvironmentVip [as 别名]
# 或者: from networkapi.ambiente.models.EnvironmentVip import get_by_pk [as 别名]
def handle_post(self, request, user, *args, **kwargs):
'''Handles POST requests to edit an Network.
URL: network/edit/
'''
self.log.info('Edit an Network')
try:
# 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)
net_map = networkapi_map.get('net')
if net_map is None:
msg = u'There is no value to the ip tag of XML request.'
self.log.error(msg)
return self.response_error(3, msg)
# Get XML data
id_network = net_map.get('id_network')
ip_type = net_map.get('ip_type')
id_net_type = net_map.get('id_net_type')
id_env_vip = net_map.get('id_env_vip')
# Valid id_network
if not is_valid_int_greater_zero_param(id_network):
self.log.error(
u'Parameter id_network is invalid. Value: %s.', id_network)
raise InvalidValueError(None, 'id_network', id_network)
# Valid ip_type
if not is_valid_int_param(ip_type):
self.log.error(
u'Parameter ip_type is invalid. Value: %s.', ip_type)
raise InvalidValueError(None, 'ip_type', ip_type)
list_choice = [0, 1]
# Valid ip_type choice
if int(ip_type) not in list_choice:
self.log.error(
u'Parameter ip_type is invalid. Value: %s.', ip_type)
raise InvalidValueError(None, 'ip_type', ip_type)
# Valid id_net_type
if not is_valid_int_greater_zero_param(id_net_type):
self.log.error(
u'Parameter id_net_type is invalid. Value: %s.', id_net_type)
raise InvalidValueError(None, 'id_net_type', id_net_type)
# Valid id_env_vip
if id_env_vip is not None:
if not is_valid_int_greater_zero_param(id_env_vip):
self.log.error(
u'Parameter id_env_vip is invalid. Value: %s.', id_env_vip)
raise InvalidValueError(None, 'id_env_vip', id_env_vip)
# User permission
if not has_perm(user, AdminPermission.VLAN_MANAGEMENT, AdminPermission.WRITE_OPERATION):
raise UserNotAuthorizedError(
None, u'User does not have permission to perform the operation.')
# Business Rules
if (id_env_vip is not None):
id_env_vip = EnvironmentVip.get_by_pk(id_env_vip)
id_net_type = TipoRede.get_by_pk(id_net_type)
# New network_tyoe
# EDIT NETWORK IP4
if int(ip_type) == 0:
net = NetworkIPv4.get_by_pk(id_network)
with distributedlock(LOCK_NETWORK_IPV4 % id_network):
if id_env_vip is not None:
if net.ambient_vip is None or net.ambient_vip.id != id_env_vip.id:
network = IPNetwork(
'%d.%d.%d.%d/%d' % (net.oct1, net.oct2, net.oct3, net.oct4, net.block))
# Find all networks related to environment vip
nets = NetworkIPv4.objects.select_related().filter(
ambient_vip__id=id_env_vip.id)
# Cast to API class
networks = set([IPv4Network(
'%d.%d.%d.%d/%d' % (net_ip.oct1, net_ip.oct2, net_ip.oct3, net_ip.oct4, net_ip.block)) for net_ip in nets])
# If there is already a network with the same ip
# range as related the environment vip
if network in networks:
raise NetworkIpAddressNotAvailableError(
#.........这里部分代码省略.........
示例15: handle_post
# 需要导入模块: from networkapi.ambiente.models import EnvironmentVip [as 别名]
# 或者: from networkapi.ambiente.models.EnvironmentVip import get_by_pk [as 别名]
def handle_post(self, request, user, *args, **kwargs):
"""Treat POST requests to add new Network
URL: network/add/
"""
try:
# Commons Validations
# User permission
if not has_perm(user, AdminPermission.VLAN_MANAGEMENT, 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)
network_map = networkapi_map.get('network')
if network_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
network = network_map.get('network')
id_vlan = network_map.get('id_vlan')
network_type = network_map.get('id_network_type')
environment_vip = network_map.get('id_environment_vip')
# Valid Network
try:
net = IPNetwork(network)
except ValueError, e:
raise InvalidValueError(None, 'network', network)
# VLAN
# Valid vlan ID
if not is_valid_int_greater_zero_param(id_vlan):
raise InvalidValueError(None, 'id_vlan', id_vlan)
# Find vlan by ID to check if it exist
vlan = Vlan().get_by_pk(id_vlan)
# Network Type
# Valid network_type ID
if not is_valid_int_greater_zero_param(network_type):
raise InvalidValueError(None, 'id_network_type', network_type)
# Find network_type by ID to check if it exist
net_type = TipoRede.get_by_pk(network_type)
# Environment Vip
if environment_vip is not None:
# Valid environment_vip ID
if not is_valid_int_greater_zero_param(environment_vip):
raise InvalidValueError(
None, 'id_environment_vip', environment_vip)
evips = EnvironmentVip.objects.all()
evip_list = EnvironmentVip.available_evips(
EnvironmentVip(), evips, int(id_vlan))
# Check if the chose environment is in the same environment
if any(int(environment_vip) == item['id'] for item in evip_list):
# Find Environment VIP by ID to check if it exist
env_vip = EnvironmentVip.get_by_pk(environment_vip)
else:
raise InvalidValueError(
None, 'id_environment_vip', environment_vip)
else:
env_vip = None
# Check unchecked exception
blocks, network, version = break_network(network)
expl = split(
net.network.exploded, "." if version == IP_VERSION.IPv4[0] else ":")
expl.append(str(net.prefixlen))
if blocks != expl:
raise InvalidValueError(None, 'rede', network)
# Business Rules
#.........这里部分代码省略.........