def edit(request, id_environment): try: lists = dict() # 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() configurations_prefix = client.create_ambiente().configuration_list_all( id_environment) lists['configurations_prefix'] = configurations_prefix.get( 'lists_configuration') 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'] try: env = client.create_api_environment().get_environment(id_environment) env = env.get("environments")[0] except NetworkAPIClientError, e: logger.error(e) messages.add_message(request, messages.ERROR, e) return redirect('environment.list')
def insert_ambiente_logico(request): # If form was submited if request.method == 'POST': try: lists = dict() # Get User auth = AuthSession(request.session) client = auth.get_clientFactory() lists['ambientelogico_form'] = AmbienteLogicoForm() # Set data in form ambiente_logico_form = AmbienteLogicoForm(request.POST) id_env = request.POST['id_env'] # Validate if ambiente_logico_form.is_valid(): nome_ambiente_logico = ambiente_logico_form.cleaned_data[ 'nome'] # Business client.create_ambiente_logico().inserir( nome_ambiente_logico.upper()) messages.add_message( request, messages.SUCCESS, environment_messages.get("ambiente_log_sucess")) else: # If invalid, send all error messages in fields lists['ambientelogico_form'] = ambiente_logico_form except NetworkAPIClientError, e: logger.error(e) lists['ambientelogico_form'] = ambiente_logico_form messages.add_message(request, messages.ERROR, e) try: # 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 env_form = AmbienteForm( env_logic, division_dc, group_l3, filters, ipv4, ipv6, envs, vrfs) div_form = DivisaoDCForm() gro_form = Grupol3Form() action = reverse("environment.form") # If id_env is set, still in edit if id_env: env = client.create_ambiente().buscar_por_id(id_env) env = env.get("ambiente") # Set Environment data initial = {"id_env": env.get("id"), "divisao": env.get("id_divisao"), "ambiente_logico": env.get("id_ambiente_logico"), "grupol3": env.get("id_grupo_l3"), "filter": env.get("id_filter"), "link": env.get("link")} env_form = AmbienteForm( env_logic, division_dc, group_l3, filters, ipv4, ipv6, envs, vrfs, initial=initial) div_form = DivisaoDCForm(initial={"id_env": id_env}) gro_form = Grupol3Form(initial={"id_env": id_env}) action = reverse("environment.edit", args=[id_env]) lists['ambiente'] = env_form lists['divisaodc_form'] = div_form lists['grupol3_form'] = gro_form lists['action'] = action return render_to_response(ENVIRONMENT_FORM, lists, 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
def edit(request, id_environment): lists = dict() try: # 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() cidr = client.create_api_environment_cidr().\ get_by_env(env_id=[id_environment]) lists['configurations_prefix'] = cidr.get('cidr') try: templates = get_templates(auth.get_user(), True) except GITCommandError as 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'] try: env = client.create_api_environment().get_environment(id_environment) env = env.get("environments")[0] except NetworkAPIClientError as e: logger.error(e) messages.add_message(request, messages.ERROR, e) return redirect('environment.list') lists['id_environment'] = env.get("id") # Set Environment data initial = { "id_env": env.get("id"), "divisao": env.get("divisao_dc"), "ambiente_logico": env.get("ambiente_logico"), "grupol3": env.get("grupo_l3"), "filter": env.get("filter"), "acl_path": env.get("acl_path"), "ipv4_template": env.get("ipv4_template"), "ipv6_template": env.get("ipv6_template"), "min_num_vlan_1": env.get("min_num_vlan_1"), "max_num_vlan_1": env.get("max_num_vlan_1"), "min_num_vlan_2": env.get("min_num_vlan_2"), "max_num_vlan_2": env.get("max_num_vlan_2"), 'link': env.get('link'), 'father_environment': env.get('father_environment'), 'vrf': env.get('default_vrf'), 'vxlan': 1 if env.get('vxlan') else 0 } env_form = AmbienteForm( env_logic, division_dc, group_l3, filters, ipv4, ipv6, envs, vrfs, initial=initial) # Forms lists['ambiente'] = env_form lists['vxlan'] = env.get('vxlan', False) lists['divisaodc_form'] = DivisaoDCForm( initial={"id_env": id_environment} ) lists['grupol3_form'] = Grupol3Form(initial={"id_env": id_environment}) lists['ambientelogico_form'] = AmbienteLogicoForm( initial={"id_env": id_environment}) lists['action'] = reverse("environment.edit", args=[id_environment]) # 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 vxlan = True if request.POST.get('is_vxlan') else False # Validate if ambiente_form.is_valid(): id_env = ambiente_form.cleaned_data['id_env'] 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'] vrf = ambiente_form.cleaned_data['vrf'] acl_path = ambiente_form.cleaned_data['acl_path'] 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": int(id_env), "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, 'vxlan': vxlan } client.create_api_environment().update_environment(dict_env, id_env) messages.add_message( request, messages.SUCCESS, environment_messages.get("success_edit")) return redirect('environment.list') else: # If invalid, send all error messages in fields lists['ambiente'] = ambiente_form except NetworkAPIClientError as e: logger.error(e) messages.add_message(request, messages.ERROR, e) return render_to_response(ENVIRONMENT_FORM, lists, context_instance=RequestContext(request))