Skip to content

Commit

Permalink
Merge branch 'integration_dev_master'
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquebonadio-zz committed May 7, 2015
2 parents 34c4722 + cbfd24b commit 5f65121
Show file tree
Hide file tree
Showing 15 changed files with 2,547 additions and 2,373 deletions.
2 changes: 1 addition & 1 deletion docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ In order to use the pre-configured VM you need to have `vagrant <https://www.vag

After that, go to the directory you want to install and do::

git clone https://github.com/globocom/GloboNetworkAPI-WebUI
git clone https://github.com/globocom/GloboNetworkAPI
cd GloboNetworkAPI-WebUI
vagrant plugin install vagrant-omnibus
vagrant up
Expand Down
9 changes: 9 additions & 0 deletions networkapi/api_vip_request/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@
class VipRequestDoesNotExistException(APIException):
status_code = status.HTTP_404_NOT_FOUND
default_detail = 'Vip Request Does Not Exist.'


class EnvironmentVipDoesNotExistException(APIException):
status_code = status.HTTP_404_NOT_FOUND
default_detail = 'Environment Vip Does Not Exist.'

class InvalidIdVipRequestException(APIException):
status_code = status.HTTP_400_BAD_REQUEST
default_detail = 'Invalid id for Vip Request.'
203 changes: 203 additions & 0 deletions networkapi/api_vip_request/facade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
# -*- coding:utf-8 -*-

"""
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from django.forms import model_to_dict

from networkapi.distributedlock import distributedlock, LOCK_VIP
from networkapi.log import Log
from networkapi.api_vip_request.serializers import RequestVipSerializer, VipPortToPoolSerializer
from networkapi.requisicaovips.models import RequisicaoVips, VipPortToPool, ServerPool
from networkapi.util import is_valid_int_greater_zero_param, convert_boolean_to_int
from networkapi.api_vip_request import exceptions
from networkapi.api_rest import exceptions as api_exceptions


log = Log(__name__)


def get_by_pk(pk):
"""
Get Vip Request By Pk
:param pk: Identifier For Vip Request
:return: Dict
"""

if not is_valid_int_greater_zero_param(pk):
raise exceptions.InvalidIdVipRequestException()

vip_request = RequisicaoVips.objects.get(id=pk)

data = vip_request.variables_to_map()
data['id'] = vip_request.id
data['validado'] = convert_boolean_to_int(vip_request.validado)
data['vip_criado'] = convert_boolean_to_int(vip_request.vip_criado)
data['id_ip'] = vip_request.ip_id
data['id_ipv6'] = vip_request.ipv6_id
data['id_healthcheck_expect'] = vip_request.healthcheck_expect_id
data['l7_filter'] = vip_request.l7_filter
data['rule_id'] = vip_request.rule_id

pools = []

vip_to_ports_query = VipPortToPool.objects.filter(requisicao_vip=vip_request)

for vip_port in vip_to_ports_query:

pools_members = []

server_pool = vip_port.server_pool
pool_raw = model_to_dict(server_pool)
pool_raw["port_vip"] = vip_port.port_vip
pool_raw["port_vip_id"] = vip_port.id

for pool_member in server_pool.serverpoolmember_set.all():
pools_member_raw = model_to_dict(pool_member)
ipv4 = pool_member.ip
ipv6 = pool_member.ipv6
ip_equipment_set = ipv4 and ipv4.ipequipamento_set or ipv6 and ipv6.ipv6equipament_set
ip_equipment_obj = ip_equipment_set.select_related().uniqueResult()
healthcheck_type = pool_member.healthcheck and pool_member.healthcheck.healthcheck_type or ''
pools_member_raw['healthcheck'] = {'healthcheck_type': healthcheck_type}
pools_member_raw['equipment_name'] = ip_equipment_obj.equipamento.nome
ip_formatted = ip_equipment_obj.ip.ip_formated

if ipv4:
pools_member_raw["ip"] = {'ip_formated': ip_formatted}
else:
pools_member_raw["ipv6"] = {'ip_formated': ip_formatted}

pools_members.append(pools_member_raw)

pool_raw['server_pool_members'] = pools_members
pools.append(pool_raw)

data["pools"] = pools

return data


def save(request):
"""
Save Vip Request
:param request: Request
:return: Data Serialized Post Save
"""

data = request.DATA
user = request.user

req_vip_serializer = RequestVipSerializer(
data=data
)

if not req_vip_serializer.is_valid():
log.error(req_vip_serializer.errors)
raise api_exceptions.ValidationException()

obj_req_vip = req_vip_serializer.object

obj_req_vip.filter_valid = True
obj_req_vip.validado = False
set_l7_filter_for_vip(obj_req_vip)
obj_req_vip.set_new_variables(data)
obj_req_vip.save(user)

for v_port in obj_req_vip.vip_ports_to_pools:
v_port.requisicao_vip = obj_req_vip
v_port.save(user)

return req_vip_serializer.data


def update(request, pk):
"""
Update Vip Request
:param request:
:param pk: Identifier Vip Request
:return: Data Serialized Post Update
"""

data = request.DATA
user = request.user

if not is_valid_int_greater_zero_param(pk):
raise exceptions.InvalidIdVipRequestException()

vip_ports = data.get("vip_ports_to_pools")

req_vip_serializer = RequestVipSerializer(
data=data
)

if not req_vip_serializer.is_valid():
log.error(req_vip_serializer.errors)
raise api_exceptions.ValidationException()

RequisicaoVips.objects.get(pk=pk)

with distributedlock(LOCK_VIP % pk):

obj_req_vip = req_vip_serializer.object
obj_req_vip.id = pk
obj_req_vip.filter_valid = True
obj_req_vip.validado = False
set_l7_filter_for_vip(obj_req_vip)
obj_req_vip.set_new_variables(data)
obj_req_vip.save(user)

vip_port_serializer = VipPortToPoolSerializer(data=vip_ports, many=True)

if not vip_port_serializer.is_valid():
raise api_exceptions.ValidationException("Invalid Port Vip To Pool")

vip_port_to_pool_pks = [port['id'] for port in vip_ports if port.get('id')]

vip_port_to_pool_to_remove = VipPortToPool.objects.filter(
requisicao_vip=obj_req_vip
).exclude(
id__in=vip_port_to_pool_pks
)

for v_port_to_del in vip_port_to_pool_to_remove:
v_port_to_del.delete(user)

for vip_port in vip_ports:
vip_port_obj = VipPortToPool()
vip_port_obj.id = vip_port.get('id')
vip_port_obj.server_pool = ServerPool(id=vip_port.get('server_pool'))
vip_port_obj.port_vip = vip_port.get('port_vip')
vip_port_obj.requisicao_vip = obj_req_vip
vip_port_obj.save(user)

return req_vip_serializer.data


def set_l7_filter_for_vip(obj_req_vip):

if obj_req_vip.rule:
obj_req_vip.l7_filter = '\n'.join(
obj_req_vip.rule.rulecontent_set.all().values_list(
'content',
flat=True
)
)
17 changes: 15 additions & 2 deletions networkapi/api_vip_request/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,21 @@ class Meta:
'port_vip',
)

def validate_port_vip(self, attrs, attr_name):

class RequesVipSerializer(serializers.ModelSerializer):
try:
port_vip = int(attrs.get(attr_name, 0))

if port_vip > 65535 or 1 > port_vip:
raise serializers.ValidationError(u'The port number must be between 1 and 65535.')

return attrs

except ValueError:
raise serializers.ValidationError(u'The port must be a number.')


class RequestVipSerializer(serializers.ModelSerializer):

ip = serializers.PrimaryKeyRelatedField(
many=False,
Expand Down Expand Up @@ -73,7 +86,7 @@ class RequesVipSerializer(serializers.ModelSerializer):

vip_ports_to_pools = VipPortToPoolSerializer(
many=True,
required=False
required=True
)

finalidade = serializers.CharField(
Expand Down
1 change: 1 addition & 0 deletions networkapi/api_vip_request/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
url(r'^vip/request/add/pools/$', 'add_pools'),
url(r'^vip/request/delete/(?P<delete_pools>\d+)/$', 'delete'),
url(r'^vip/list/environment/by/environment/vip/(?P<environment_vip_id>\d+)/$', 'list_environment_by_environment_vip'),
url(r'^vip/request/get/(?P<pk>\d+)/$', 'get_by_pk'),
)
Loading

0 comments on commit 5f65121

Please sign in to comment.