def delete_wan(request): """Delete WAN rules""" if request.method == 'POST': actions = ActionsForm(request.POST) if actions.is_valid(): wans = request.POST.getlist('wans') for wan_id in wans: WAN.objects.get(id=wan_id).delete() _log_action(wan_id, attrs={'type': 'wan', 'action': 'delete'}) else: print(actions.errors) return HttpResponseRedirect('/wan/add')
def delete_instance_type(request): """Delete instance type view""" if request.method == 'POST': actions = ActionsForm(request.POST) if actions.is_valid(): instance_types = request.POST.getlist('instance_type') for instance_type_id in instance_types: InstanceType.objects.get(id=instance_type_id).delete() _log_action(instance_type_id, attrs={ 'type': 'instance_type', 'action': 'delete' }) else: print(actions.errors) return HttpResponseRedirect('/instance_type/add')
def add_instance_type(request): """Add Instance Type View""" if request.method == 'POST': add_instance_type = AddInstanceTypeForm(request.POST) if add_instance_type.is_valid(): instance_type = add_instance_type.save(commit=False) instance_type.save() _log_action(instance_type, attrs={ 'type': 'instance_type', 'action': 'create' }) return HttpResponseRedirect('/instance_type/add') else: print(add_instance_type.errors) else: add_instance_type = AddInstanceTypeForm() instance_types = InstanceType.objects.all() actions_form = ActionsForm() return render( request, "add_instance_type.html", { "add_instance_type": add_instance_type, "instance_types": instance_types, "actions_form": actions_form })
def delete_region(request): """Delete region view""" if request.method == 'POST': actions = ActionsForm(request.POST) if actions.is_valid(): regions = request.POST.getlist('region') for region_id in regions: Region.objects.get(id=region_id).delete() _log_action(region_id, attrs={ 'type': 'region', 'action': 'delete' }) else: print(actions.errors) return HttpResponseRedirect('/region/add')
def add_region(request): """Add Region View""" if request.method == 'POST': add_region = AddRegionForm(request.POST) if add_region.is_valid(): region = add_region.save(commit=False) region.save() _log_action(region, attrs={'type': 'region', 'action': 'create'}) return HttpResponseRedirect('/region/add') else: print(add_region.errors) else: add_region = AddRegionForm() regions = Region.objects.all() hosts = Host.objects.all() hosts_form = HostForm() actions_form = ActionsForm() return render( request, "add_region.html", { "hosts_form": hosts_form, "hosts": hosts, "add_region": add_region, "regions": regions, "actions_form": actions_form })
def delete_rule_group(request): """Delete Deployment Group View""" if request.method == 'POST': actions = ActionsForm(request.POST) if actions.is_valid(): rule_groups = request.POST.getlist('rule_group') for rule_group_id in rule_groups: rule_group = RuleGroup.objects.get(id=rule_group_id) rule_group.rule.all().delete() rule_group.delete() _log_action(rule_group_id, attrs={ 'type': 'rule group', 'action': 'delete' }) else: print(actions.errors) return HttpResponseRedirect('/rule_group/add')
def add_rule_group(request): """Add Deployment Group View""" if request.method == 'POST': add_rule_group = AddRuleGroupForm(request.POST) add_rule = AddRuleForm(request.POST) if add_rule_group.is_valid(): rule_group = add_rule_group.save() selected_hosts = request.POST.getlist('hosts') if selected_hosts: for src_host in selected_hosts: for dst_host in selected_hosts: if src_host != dst_host: rule = Rule() rule.host = Host.objects.get(id=src_host) rule.target_host = Host.objects.get(id=dst_host) rule.target_region = rule.target_host.region if rule.host.instance_type: rule.bandwidth = rule.host.instance_type.bandwidth rule.latency = rule.host.instance_type.latency rule.interface = rule.host.interface rule.bw_rate = rule.host.instance_type.bw_rate rule.packet_loss = rule.host.instance_type.packet_loss rule.packet_corruption_rate = rule.host.instance_type.packet_corruption_rate rule.latency_time_unit = rule.host.instance_type.latency_time_unit rule.save() rule_group.rule.add(rule) selected_rules = request.POST.getlist('rules') if selected_rules: for rule_id in selected_rules: rule = Rule.objects.get(id=rule_id) rule_group.rule.add(rule) rule_group.save() _log_action(rule_group.name, attrs={ 'type': 'rule group', 'action': 'create' }) return HttpResponseRedirect('/') else: print(add_rule_group.errors) else: add_rule_group = AddRuleGroupForm() add_rule = AddRuleForm() rules = Rule.objects.all() hosts = Host.objects.exclude(region__isnull=True) hosts_form = ConfigureHostForm() actions_form = ActionsForm() return render( request, "add_rule_group.html", { "add_rule": add_rule, "add_rule_group": add_rule_group, "rules": rules, "hosts": hosts, "hosts_form": hosts_form, "actions_form": actions_form })
def list_deployment_groups(request): """List Deployment Groups""" rule_group = RuleGroup.objects.all() add_rule_group = AddRuleGroupForm() actions_form = ActionsForm() return render( request, "list_deployment_groups.html", { "add_rule_group": add_rule_group, "actions_form": actions_form, "rule_group": rule_group })
def delete_rule(request): """Delete rules view""" if request.method == 'POST': actions = ActionsForm(request.POST) if actions.is_valid(): rules = request.POST.getlist('rules') for rule_id in rules: rule = Rule.objects.get(id=rule_id) inventory = AnsibleInventory() hosts = inventory.unique_hosts_data global_options = inventory.global_options host_options = inventory.DEFAULT_OPTIONS.copy() host_options.update(global_options[0]) host_vars = [ host["vars"] for host in hosts if host["name"] == rule.host.name ] host_options.update(host_vars[0]) command = generate_tc_command(rule, deactivate=True) _log_action(rule_id, attrs={ 'type': 'rule', 'action': 'delete' }) status = AnsibleDeploy(options=host_options).deploy( command, rule.host.name, facts='no') rule.delete() all_host_rules = Rule.objects.filter(host__name=rule.host.name) for host_rule in all_host_rules: deploy_the_rest_of_the_rules = generate_tc_command( host_rule, deactivate=True) # TODO: log status = AnsibleDeploy(options=host_options).deploy( deploy_the_rest_of_the_rules, host_rule.host.name, facts='no') else: print(actions.errors) return HttpResponseRedirect('/rule/add')
def list_host_rules(request, region_name=None, host_name=None): """List the Designated Rules of the Host""" hosts = Host.objects.filter(name=host_name) rules = Rule.objects.filter(host__name=host_name) hosts_form = HostForm() add_rule = AddRuleForm() actions_form = ActionsForm() return render( request, "list_host_rules.html", { "host_name": host_name, "actions_form": actions_form, "add_rule": add_rule, "rules": rules, "region_name": region_name, "hosts_form": hosts_form, "hosts": hosts })
def list_all_rules(request, rule_group_name=None): """List all Rules""" rules = Rule.objects.all() hosts = Host.objects.all() hosts_form = HostForm() if rule_group_name: rules = RuleGroup.objects.filter(name=rule_group_name)[0].rule.all() add_rule = AddRuleForm() actions_form = ActionsForm() return render( request, "list_all_rules.html", { "actions_form": actions_form, "add_rule": add_rule, "rules": rules, "hosts_form": hosts_form, "hosts": hosts })
def add_wan(request): """Add Wide Area Network View""" if request.method == 'POST': add_wan = AddWANForm(request.POST) if add_wan.is_valid(): wan = add_wan.save(commit=False) wan.save() _log_action(wan, attrs={'type': 'wan', 'action': 'create'}) return HttpResponseRedirect('/wan/add') else: print(add_wan.errors) else: add_wan = AddWANForm() wans = WAN.objects.all() actions_form = ActionsForm() return render(request, "add_wan.html", { "add_wan": add_wan, "wans": wans, "actions_form": actions_form })
def add_rule(request): """Add Rule View""" if request.is_ajax(): region_id = request.GET.get('region') if not region_id: hosts = Host.objects.all().values('name') else: hosts = Host.objects.filter(region__id=region_id).values('name') return HttpResponse(json.dumps(list(hosts)), content_type="application/json") if request.method == 'POST': add_rule = AddRuleForm(request.POST) if add_rule.is_valid(): data = add_rule.cleaned_data rule = add_rule.save(commit=False) rule.host = data["host"] rule.traffic_type = int(data["traffic_type"] or 0) if data["instance_type"]: rule.host.instance_type = data["instance_type"] rule.host.save() if rule.host.instance_type: rule.bandwidth = rule.host.instance_type.bandwidth rule.bw_rate = rule.host.instance_type.bw_rate rule.packet_loss = rule.host.instance_type.packet_loss rule.packet_corruption_rate = rule.host.instance_type.packet_corruption_rate rule.latency = rule.host.instance_type.latency rule.latency_time_unit = rule.host.instance_type.latency_time_unit rule.save() _log_action(rule.id, attrs={'type': 'rule', 'action': 'create'}) return HttpResponseRedirect('/rule_group/add') else: print(add_rule.errors) else: add_rule = AddRuleForm() actions_form = ActionsForm() rules = Rule.objects.all() return render(request, "add_rule.html", { "add_rule": add_rule, "rules": rules, "actions_form": actions_form })
def panel(request): """Main Dashboard & Deployment operations""" if request.method == 'POST': add_rule_group = AddRuleGroupForm(request.POST) selected_rule_groups = request.POST.getlist('rule_group') if selected_rule_groups: deploy(request, selected_rule_groups) else: add_rule_group = AddRuleGroupForm() regions = Region.objects.all() rule_group = RuleGroup.objects.all() add_region = AddRegionForm() actions_form = ActionsForm() return render( request, "panel.html", { "add_region": add_region, "regions": regions, "rule_group": rule_group, "add_rule_group": add_rule_group, "actions_form": actions_form })