Beispiel #1
0
def add_configuration(request, id_environment):

    context = dict()

    try:

        auth = AuthSession(request.session)
        client = auth.get_clientFactory()
        net_type_list = client.create_tipo_rede().listar()

        form = IpConfigForm(net_type_list, request.POST or None)

        # Get user auth
        auth = AuthSession(request.session)
        environment_client = auth.get_clientFactory().create_ambiente()

        environment_dict = environment_client.buscar_por_id(id_environment)
        environment = environment_dict.get('ambiente')

        context["form"] = form
        context["action"] = reverse(
            'environment.configuration.add', args=[id_environment])
        context["environment"] = environment

        if form.is_valid():
            network = form.cleaned_data['network_validate']
            prefix = form.cleaned_data['prefix']
            ip_version = form.cleaned_data['ip_version']
            network_type = form.cleaned_data['net_type']

            environment_client.configuration_save(
                id_environment, network, prefix, ip_version, network_type)
            messages.add_message(request, messages.SUCCESS, environment_messages.get(
                "success_configuration_insert"))
            context["form"] = IpConfigForm(net_type_list)

    except AmbienteNaoExisteError as e:
        messages.add_message(request, messages.ERROR, e)
        return redirect('environment.list')

    except InvalidParameterError as e:
        messages.add_message(request, messages.ERROR, e)

    except NetworkAPIClientError as e:
        logger.error(e)
        messages.add_message(request, messages.ERROR, e)

    except BaseException as e:
        logger.error(e)
        messages.add_message(request, messages.ERROR, e)

    return render_to_response(templates.ENVIRONMENT_CIDR, context,
                              context_instance=RequestContext(request))
Beispiel #2
0
def add_configuration(request, id_environment):
    try:

        auth = AuthSession(request.session)
        client = auth.get_clientFactory()
        net_type_list = client.create_tipo_rede().listar()

        context = dict()

        form = IpConfigForm(net_type_list, request.POST or None)

        # Get user auth
        auth = AuthSession(request.session)
        environment_client = auth.get_clientFactory().create_ambiente()

        environment_dict = environment_client.buscar_por_id(id_environment)
        environment = environment_dict.get('ambiente')

        context["form"] = form
        context["action"] = reverse('environment.configuration.add',
                                    args=[id_environment])
        context["environment"] = environment

        if form.is_valid():
            network = form.cleaned_data['network_validate']
            prefix = form.cleaned_data['prefix']
            ip_version = form.cleaned_data['ip_version']
            network_type = form.cleaned_data['net_type']

            environment_client.configuration_save(id_environment, network,
                                                  prefix, ip_version,
                                                  network_type)
            messages.add_message(
                request, messages.SUCCESS,
                environment_messages.get("success_configuration_insert"))
            context["form"] = IpConfigForm(net_type_list)

    except AmbienteNaoExisteError, e:
        messages.add_message(request, messages.ERROR, e)
        return redirect('environment.list')
def add_configuration(request, id_environment):
    try:

        auth = AuthSession(request.session)
        client = auth.get_clientFactory()
        net_type_list = client.create_tipo_rede().listar()

        context = dict()

        form = IpConfigForm(net_type_list, request.POST or None)

        # Get user auth
        auth = AuthSession(request.session)
        environment_client = auth.get_clientFactory().create_ambiente()

        environment_dict = environment_client.buscar_por_id(id_environment)
        environment = environment_dict.get('ambiente')

        context["form"] = form
        context["action"] = reverse(
            'environment.configuration.add', args=[id_environment])
        context["environment"] = environment

        if form.is_valid():
            network = form.cleaned_data['network_validate']
            prefix = form.cleaned_data['prefix']
            ip_version = form.cleaned_data['ip_version']
            network_type = form.cleaned_data['net_type']

            environment_client.configuration_save(
                id_environment, network, prefix, ip_version, network_type)
            messages.add_message(request, messages.SUCCESS, environment_messages.get(
                "success_configuration_insert"))
            context["form"] = IpConfigForm(net_type_list)

    except AmbienteNaoExisteError, e:
        messages.add_message(request, messages.ERROR, e)
        return redirect('environment.list')
def allocate_cidr(request, id_environment):

    context = dict()

    try:

        auth = AuthSession(request.session)
        client = auth.get_clientFactory()

        net_type_list = client.create_tipo_rede().listar()
        form = IpConfigForm(net_type_list, request.POST or None)

        environment = client.create_api_environment().get(
            [id_environment]).get('environments')[0]

        context["form"] = form
        context["action"] = reverse('environment.configuration.add',
                                    args=[id_environment])
        context["environment"] = environment

        if request.method == 'POST':
            ip_version = request.POST.get('ip_version')

            if str(ip_version) == 'cidr_auto':
                v4 = request.POST.get('v4_auto')
                v6 = request.POST.get('v6_auto')
                network_type = request.POST.get('net_type')
                cidr = list()

                if int(v4):
                    prefix_v4 = request.POST.get('prefixv4')
                    if not prefix_v4:
                        raise Exception(
                            "Informe a máscara da subnet da redev4.")

                    cidrv4 = dict(ip_version='v4',
                                  network_type=network_type,
                                  subnet_mask=prefix_v4,
                                  environment=int(id_environment))
                    cidr.append(cidrv4)
                if int(v6):
                    prefix_v6 = request.POST.get('prefixv6')
                    if not prefix_v6:
                        raise Exception(
                            "Informe a máscara da subnet da redev6.")
                    cidrv6 = dict(ip_version='v6',
                                  network_type=network_type,
                                  subnet_mask=str(prefix_v6),
                                  environment=int(id_environment))
                    cidr.append(cidrv6)

                if not cidr:
                    raise Exception("Escolha uma das opções abaixo.")

                client.create_api_environment_cidr().post(cidr)
                messages.add_message(
                    request, messages.SUCCESS,
                    environment_messages.get("success_configuration_insert"))

            elif form.is_valid():
                network = form.cleaned_data['network_validate']
                network_type = form.cleaned_data['net_type']
                prefix = form.cleaned_data['prefix']

                cidr = dict(ip_version=ip_version,
                            network_type=int(network_type),
                            subnet_mask=str(prefix),
                            network=network,
                            environment=int(id_environment))

                client.create_api_environment_cidr().post([cidr])
                messages.add_message(
                    request, messages.SUCCESS,
                    environment_messages.get("success_configuration_insert"))

            else:
                messages.add_message(
                    request, messages.ERROR,
                    environment_messages.get("invalid_parameters"))

            context["form"] = IpConfigForm(net_type_list)

    except AmbienteNaoExisteError as e:
        messages.add_message(request, messages.ERROR, e)
        return redirect('environment.list')

    except InvalidParameterError as e:
        messages.add_message(request, messages.ERROR, e)

    except NetworkAPIClientError as e:
        logger.error(e)
        messages.add_message(request, messages.ERROR, e)
    except Exception as e:
        logger.error(e)
        messages.add_message(request, messages.ERROR, e)

    return render_to_response(templates.ENVIRONMENT_CIDR,
                              context,
                              context_instance=RequestContext(request))
def insert_ambiente(request):
    try:
        lists = dict()
        config_forms = list()

        # Get User
        auth = AuthSession(request.session)
        client = auth.get_clientFactory()

        # Get all needs from NetworkAPI
        env_logic = client.create_ambiente_logico().listar()
        division_dc = client.create_divisao_dc().listar()
        group_l3 = client.create_grupo_l3().listar()
        filters = client.create_filter().list_all()
        try:
            templates = get_templates(auth.get_user(), True)
        except GITCommandError, e:
            logger.error(e)
            messages.add_message(request, messages.ERROR, e)
            templates = {
                'ipv4': list(),
                'ipv6': list()
            }

        ipv4 = templates.get("ipv4")
        ipv6 = templates.get("ipv6")
        envs = client.create_ambiente().listar().get('ambiente')
        vrfs = client.create_api_vrf().search()['vrfs']
        # Forms
        lists['ambiente'] = AmbienteForm(
            env_logic, division_dc, group_l3, filters, ipv4, ipv6, envs, vrfs)
        lists['divisaodc_form'] = DivisaoDCForm()
        lists['grupol3_form'] = Grupol3Form()
        lists['ambientelogico_form'] = AmbienteLogicoForm()

        lists['action'] = reverse("environment.form")

        # If form was submited
        if request.method == 'POST':

            # Set data in form
            ambiente_form = AmbienteForm(
                env_logic, division_dc, group_l3, filters, ipv4, ipv6, envs, vrfs, request.POST)

            # Return data to form in case of error
            lists['ambiente'] = ambiente_form

            # Validate
            if ambiente_form.is_valid():

                divisao_dc = ambiente_form.cleaned_data['divisao']
                ambiente_logico = ambiente_form.cleaned_data['ambiente_logico']
                grupo_l3 = ambiente_form.cleaned_data['grupol3']
                filter_ = ambiente_form.cleaned_data['filter']
                link = ambiente_form.cleaned_data['link']
                acl_path = ambiente_form.cleaned_data['acl_path']
                vrf = ambiente_form.cleaned_data['vrf']
                if str(vrf) == str(None):
                    vrf = None
                father_environment = ambiente_form.cleaned_data['father_environment']

                ipv4_template = ambiente_form.cleaned_data.get(
                    'ipv4_template', None)
                ipv6_template = ambiente_form.cleaned_data.get(
                    'ipv6_template', None)

                min_num_vlan_1 = ambiente_form.cleaned_data.get(
                    'min_num_vlan_1', None)
                max_num_vlan_1 = ambiente_form.cleaned_data.get(
                    'max_num_vlan_1', None)
                min_num_vlan_2 = ambiente_form.cleaned_data.get(
                    'min_num_vlan_2', None)
                max_num_vlan_2 = ambiente_form.cleaned_data.get(
                    'max_num_vlan_2', None)

                vrf_internal = dict(ambiente_form.fields['vrf'].choices)[int(vrf)]

                # Business
                dict_env = {
                    "id": None,
                    "grupo_l3": int(grupo_l3),
                    "ambiente_logico": int(ambiente_logico),
                    "divisao_dc": int(divisao_dc),
                    "filter": int(filter_) if filter_ else None,
                    "acl_path": acl_path,
                    "ipv4_template": ipv4_template,
                    "ipv6_template": ipv6_template,
                    "link": link,
                    "min_num_vlan_1": min_num_vlan_1,
                    "max_num_vlan_1": max_num_vlan_1,
                    "min_num_vlan_2": min_num_vlan_2,
                    "max_num_vlan_2": max_num_vlan_2,
                    "default_vrf": int(vrf),
                    "father_environment": int(father_environment) if father_environment else None,
                    'vrf': vrf_internal
                }
                client.create_api_environment().create_environment(dict_env)
                messages.add_message(
                    request, messages.SUCCESS, environment_messages.get("success_insert"))

                return redirect('environment.list')

            else:
                # If invalid, send all error messages in fields
                lists['ambiente'] = ambiente_form

        auth = AuthSession(request.session)
        client = auth.get_clientFactory()
        net_type_list = client.create_tipo_rede().listar()

        config_forms.append(IpConfigForm(net_type_list))

        lists['config_forms'] = config_forms