浏览代码

improved delete a firewall #38

Roberto Berto 5 年之前
父节点
当前提交
4e7e436a46

+ 0 - 0
vycenter/filters/__init__.py


+ 12 - 0
vycenter/filters/vycontrol_filters.py

@@ -0,0 +1,12 @@
+from django.template.defaultfilters import register
+
+
+@register.filter
+def routepack(value): 
+    """Pack a route into a string"""
+    return str(value).replace("/","!")
+
+@register.filter
+def routeunpack(value): 
+    """UnpPack a route into a string"""
+    return str(value).replace("!","/")

+ 6 - 30
vycenter/firewall/templates/firewall/list.html

@@ -10,6 +10,11 @@
 {% block content %}
 {% block content %}
 
 
 
 
+<p class="margin-topbottom">
+    <a href="{% url 'firewall:firewall-create' %}">Create new firewall</a>
+</p>
+
+
 
 
 {% if firewall_all %}
 {% if firewall_all %}
     <table border="1" width="100%">
     <table border="1" width="100%">
@@ -22,7 +27,7 @@
                 <td><a href="{% url 'firewall:show' ifkey %}">{{ ifkey }}</a></td>
                 <td><a href="{% url 'firewall:show' ifkey %}">{{ ifkey }}</a></td>
                 <td>{{ ifvalue.description }}</td>
                 <td>{{ ifvalue.description }}</td>
                 <td>{{ ifvalue.default_action }}</td>
                 <td>{{ ifvalue.default_action }}</td>
-                <td>delete</td>
+                <td><a href="{% url 'firewall:firewall-remove' ifkey %}">remove</a></td>
             </tr>
             </tr>
         {% endfor %}
         {% endfor %}
         
         
@@ -34,35 +39,6 @@
 {% endif %}
 {% endif %}
 
 
 
 
-<div class="separe-form">
-    <h2>Create new firewall</h2>
-
-    <form action="{% url 'firewall:firewall-create' %}" method="post">
-        {% csrf_token %}
-        
-        <p>
-            <label for="alias">name</label><br>
-            <input type="text" name="name" id="name" value="{{ name }}" size="30">
-        </p>
-
-        <p>
-            <label for="alias">description</label><br>
-            <input type="text" name="description" id="description" value="{{ description }}" size="60">
-        </p>    
-        
-        <p>
-            <label for="hostname">default action</label><br>
-            <input type="radio" name="action" id="action" value="accept"> accept
-            <input type="radio" name="action" id="action" value="drop"> drop
-            <input type="radio" name="action" id="action" value="reject"> reject        
-        </p>
-
-        
-        
-        <input type="submit" value="Add Firewall">
-        </form>
-</div>
-    
 
 
 
 
 {% endblock %}
 {% endblock %}

+ 2 - 0
vycenter/firewall/urls.py

@@ -5,10 +5,12 @@ from . import views
 app_name = 'firewall'
 app_name = 'firewall'
 
 
 
 
+
 urlpatterns = [
 urlpatterns = [
     path('', views.index, name='firewall-list'),
     path('', views.index, name='firewall-list'),
     path('show/<str:firewall_name>', views.show, name='show'),
     path('show/<str:firewall_name>', views.show, name='show'),
     path('firewall-create', views.create, name='firewall-create'),
     path('firewall-create', views.create, name='firewall-create'),
+    path('firewall-remove/<str:firewall_name>', views.firewall_remove, name='firewall-remove'),
     path('addrule/<str:firewall_name>', views.addrule, name='addrule'),
     path('addrule/<str:firewall_name>', views.addrule, name='addrule'),
     path('editrule/<str:firewall_name>/<str:firewall_rulenumber>', views.editrule, name='editrule'),
     path('editrule/<str:firewall_name>/<str:firewall_rulenumber>', views.editrule, name='editrule'),
     
     

+ 41 - 22
vycenter/firewall/views.py

@@ -6,6 +6,7 @@ from django.conf import settings
 from django.urls import reverse
 from django.urls import reverse
 
 
 
 
+
 import vyos
 import vyos
 
 
 
 
@@ -71,27 +72,6 @@ def create(request):
     return HttpResponse(template.render(context, request))
     return HttpResponse(template.render(context, request))
 
 
 
 
-def show(request, firewall_name):
-    if not request.user.is_authenticated:
-        return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
-        
-    #interfaces = vyos.get_interfaces()
-    all_instances = vyos.instance_getall()
-    hostname_default = vyos.get_hostname_prefered(request)
-
-    firewall = vyos.get_firewall(hostname_default, firewall_name)
-    
-
-    template = loader.get_template('firewall/show.html')
-    context = { 
-        #'interfaces': interfaces,
-        'instances': all_instances,
-        'hostname_default': hostname_default,
-        'firewall':  firewall,
-        'firewall_name': firewall_name,
-    }   
-    return HttpResponse(template.render(context, request))
-
 
 
 
 
 def addrule(request, firewall_name):
 def addrule(request, firewall_name):
@@ -131,7 +111,7 @@ def addrule(request, firewall_name):
 
 
     if changed == True:
     if changed == True:
         return redirect('firewall:show', firewall_name)
         return redirect('firewall:show', firewall_name)
-
+        
 
 
     template = loader.get_template('firewall/show.html')
     template = loader.get_template('firewall/show.html')
     context = { 
     context = { 
@@ -200,3 +180,42 @@ def editrule(request, firewall_name, firewall_rulenumber):
     return HttpResponse(template.render(context, request))
     return HttpResponse(template.render(context, request))
 
 
 
 
+
+
+
+
+def show(request, firewall_name):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
+        
+    #interfaces = vyos.get_interfaces()
+    all_instances = vyos.instance_getall()
+    hostname_default = vyos.get_hostname_prefered(request)
+
+    firewall = vyos.get_firewall(hostname_default, firewall_name)
+    
+
+    template = loader.get_template('firewall/show.html')
+    context = { 
+        #'interfaces': interfaces,
+        'instances': all_instances,
+        'hostname_default': hostname_default,
+        'firewall':  firewall,
+        'firewall_name': firewall_name,
+    }   
+    return HttpResponse(template.render(context, request))
+
+
+
+
+def firewall_remove(request, firewall_name):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
+        
+    #interfaces = vyos.get_interfaces()
+    all_instances = vyos.instance_getall()
+    hostname_default = vyos.get_hostname_prefered(request)
+
+    firewall = vyos.delete_firewall(hostname_default, firewall_name)
+    
+    return redirect('firewall:firewall-list')

+ 2 - 2
vycenter/s/main.css

@@ -9,7 +9,7 @@ body {
     color: black;
     color: black;
 }
 }
 .menu { 
 .menu { 
-    background-color: #ccc;
+    background-color: #888;
     padding: 5px;
     padding: 5px;
 }
 }
 
 
@@ -17,7 +17,7 @@ body {
   color: #000;
   color: #000;
 }    
 }    
 .content { 
 .content { 
-    background-color: rgb(120, 120, 120);
+    background-color: #ccc;
     padding: 0 10px 0 10px;
     padding: 0 10px 0 10px;
 }
 }
 .content a {
 .content a {

+ 1 - 1
vycenter/static/templates/static/static.html → vycenter/static/templates/static/add.html

@@ -14,7 +14,7 @@
 
 
 <h2>Create new static route</h2>
 <h2>Create new static route</h2>
 
 
-<form action="{% url 'static:static' %}" method="post">
+<form action="{% url 'static:static-add  %}" method="post">
     {% csrf_token %}
     {% csrf_token %}
     
     
     <p>
     <p>

+ 4 - 3
vycenter/static/templates/static/list.html

@@ -1,3 +1,5 @@
+
+
 {% extends "base.html" %}
 {% extends "base.html" %}
 
 
 {% block header_title %}Static Routes{% endblock %}
 {% block header_title %}Static Routes{% endblock %}
@@ -12,7 +14,7 @@
 
 
 
 
 <p class="margin-topbottom">
 <p class="margin-topbottom">
-    <a href="{% url 'static:static' %}">Create new static route</a>
+    <a href="{% url 'static:static-add' %}">Create new static route</a>
 </p>
 </p>
 
 
 
 
@@ -29,10 +31,9 @@
         <td>{{ x.route }}</td>
         <td>{{ x.route }}</td>
         <td>
         <td>
             {% for y in x.nexthop %}
             {% for y in x.nexthop %}
-                {{ y }}
+                {{ y }}  <a href=" url 'static:static-remove' route=x.route|routepack nexthop=y ">delete</a></td>
             {% endfor %}
             {% endfor %}
         </td>
         </td>
-
     </tr>
     </tr>
 
 
     {% endfor %}
     {% endfor %}

+ 4 - 2
vycenter/static/urls.py

@@ -6,8 +6,10 @@ app_name = 'static'
 
 
 
 
 urlpatterns = [
 urlpatterns = [
-    path('', views.index, name='static-list'),
-    path('static', views.static, name='static'),
+    path('', views.static_list, name='static-list'),
+    path('remove/<str:route>/<str:nexthop>', views.static_remove, name='static-remove'),
+    path('add', views.static_add, name='static-add'),
+
 
 
 ]
 ]
 
 

+ 24 - 3
vycenter/static/views.py

@@ -6,10 +6,11 @@ from django.conf import settings
 from django.urls import reverse
 from django.urls import reverse
 
 
 import vyos
 import vyos
+from filters.vycontrol_filters import routeunpack
 
 
 
 
 
 
-def index(request):
+def static_list(request):
     if not request.user.is_authenticated:
     if not request.user.is_authenticated:
         return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
         return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
         
         
@@ -33,7 +34,7 @@ def index(request):
 
 
 
 
 
 
-def static(request):
+def static_add(request):
     if not request.user.is_authenticated:
     if not request.user.is_authenticated:
         return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
         return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
         
         
@@ -54,7 +55,7 @@ def static(request):
 
 
     ippath = vyos.ip_route(hostname_default)
     ippath = vyos.ip_route(hostname_default)
 
 
-    template = loader.get_template('static/static.html')
+    template = loader.get_template('static/add.html')
     context = { 
     context = { 
         'instances': all_instances,
         'instances': all_instances,
         'hostname_default': hostname_default,
         'hostname_default': hostname_default,
@@ -63,3 +64,23 @@ def static(request):
     }   
     }   
     return HttpResponse(template.render(context, request))
     return HttpResponse(template.render(context, request))
 
 
+
+def static_remove(request, route, nexthop):
+    if not request.user.is_authenticated:
+        return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
+        
+    all_instances = vyos.instance_getall()
+    hostname_default = vyos.get_hostname_prefered(request)
+    static_list = vyos.get_route_static(hostname_default)
+
+    print(route)
+    print(routeunpack(route))
+
+
+    if route and nexthop:
+        return1 = vyos.delete_route_static(hostname_default, routeunpack(route), nexthop)
+
+
+
+    return redirect('static:static-list')
+

+ 4 - 1
vycenter/vycenter/settings.py

@@ -95,7 +95,10 @@ TEMPLATES = [
                 'django.contrib.auth.context_processors.auth',
                 'django.contrib.auth.context_processors.auth',
                 'django.contrib.messages.context_processors.messages',
                 'django.contrib.messages.context_processors.messages',
             ],
             ],
-        },
+            'libraries':{
+                'vycontrol_filters.py': 'filters.vycontrol_filters',
+            },
+        }        
     },
     },
 ]
 ]
 
 

+ 1 - 1
vycenter/vycenter/templates/base.html

@@ -8,7 +8,7 @@
 
 
     <!-- Bootstrap CSS -->
     <!-- Bootstrap CSS -->
     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
-    <link rel="stylesheet" href="{% static "main.css" %}?v3">
+    <link rel="stylesheet" href="{% static "main.css" %}?v7">
 
 
     <title>{% block header_title %}{% endblock %} - VyControl</title>
     <title>{% block header_title %}{% endblock %} - VyControl</title>
   </head>
   </head>

+ 13 - 3
vycenter/vyos.py

@@ -32,7 +32,6 @@ def get_url_show(hostname):
     url = get_url(hostname) + '/show'
     url = get_url(hostname) + '/show'
     return url
     return url
 
 
-
 def get_url_retrieve(hostname):
 def get_url_retrieve(hostname):
     url = get_url(hostname) + '/retrieve'        
     url = get_url(hostname) + '/retrieve'        
     return url
     return url
@@ -86,7 +85,6 @@ def api(type, hostname, cmd):
 def api_get(hostname, cmd):
 def api_get(hostname, cmd):
     return api('retrieve', hostname, cmd)
     return api('retrieve', hostname, cmd)
 
 
-
 def api_show(hostname, cmd):
 def api_show(hostname, cmd):
     return api('show', hostname, cmd)    
     return api('show', hostname, cmd)    
 
 
@@ -203,7 +201,19 @@ def set_route_static(hostname, subnet, nexthop):
     cmd = {"op": "set", "path": ["protocols","static","route", subnet, "next-hop", nexthop]}
     cmd = {"op": "set", "path": ["protocols","static","route", subnet, "next-hop", nexthop]}
 
 
     result1 = api_set(hostname, cmd)
     result1 = api_set(hostname, cmd)
-    return result1    
+    return result1  
+
+def delete_route_static(hostname, subnet, nexthop):
+    cmd = {"op": "delete", "path": ["protocols","static","route", subnet, "next-hop", nexthop]}
+
+    result1 = api_set(hostname, cmd)
+    return result1  
+
+def delete_firewall(hostname, name):
+    cmd = {"op": "delete", "path": ["firewall","name", name]}
+
+    result1 = api_set(hostname, cmd)
+    return result1            
 
 
 
 
 def ip_route(hostname):
 def ip_route(hostname):