Procházet zdrojové kódy

basic firewall list

Roberto Berto před 5 roky
rodič
revize
5f51d07352

+ 0 - 0
vycenter/firewall/__init__.py


+ 3 - 0
vycenter/firewall/admin.py

@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.

+ 5 - 0
vycenter/firewall/apps.py

@@ -0,0 +1,5 @@
+from django.apps import AppConfig
+
+
+class FirewallConfig(AppConfig):
+    name = 'firewall'

+ 0 - 0
vycenter/firewall/migrations/__init__.py


+ 3 - 0
vycenter/firewall/models.py

@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.

+ 36 - 0
vycenter/firewall/templates/firewall/list.html

@@ -0,0 +1,36 @@
+{% extends "base.html" %}
+
+{% block header_title %}Firewall Dashboard{% endblock %}
+{% block section_title %}Firewall Dashboard{% endblock %}
+
+{% block debug %}
+{{ firewall_all }}
+{% endblock %}
+
+{% block content %}
+
+
+
+{% if firewall_all %}
+    <table border="1" width="100%">
+    <tr><th>name</th><th>description</th><th>action</th></tr>
+
+    {% for key, value in firewall_all.items %}
+        <tr>
+        {% for ifkey, ifvalue in value.items %}
+            <td><a href="{% url 'firewall:show' ifkey %}">{{ ifkey }}</a></td>
+            <td>{{ ifvalue.description }}</td>
+            <td>delete</td>
+        {% endfor %}
+        </tr>
+    {% endfor %}
+
+    </table>
+{% else %}
+    <p>No interfaces.</p>
+{% endif %}
+
+{% endblock %}
+
+
+

+ 36 - 0
vycenter/firewall/templates/firewall/show.html

@@ -0,0 +1,36 @@
+{% extends "base.html" %}
+
+{% block header_title %}Firewall Dashboard{% endblock %}
+{% block section_title %}Firewall Dashboard{% endblock %}
+
+{% block debug %}
+{{ firewall_all }}
+{% endblock %}
+
+{% block content %}
+
+
+
+{% if firewall_all %}
+    <table border="1" width="100%">
+    <tr><th>name</th><th>description</th><th>action</th></tr>
+
+    {% for key, value in firewall_all.items %}
+        <tr>
+        {% for ifkey, ifvalue in value.items %}
+            <td><a href="{% url 'firewall:show' ifkey %}">{{ ifkey }}</a></td>
+            <td>{{ ifvalue.description }}</td>
+            <td>delete</td>
+        {% endfor %}
+        </tr>
+    {% endfor %}
+
+    </table>
+{% else %}
+    <p>No interfaces.</p>
+{% endif %}
+
+{% endblock %}
+
+
+

+ 3 - 0
vycenter/firewall/tests.py

@@ -0,0 +1,3 @@
+from django.test import TestCase
+
+# Create your tests here.

+ 13 - 0
vycenter/firewall/urls.py

@@ -0,0 +1,13 @@
+from django.urls import path
+
+from . import views
+
+app_name = 'firewall'
+
+
+urlpatterns = [
+    path('', views.index, name='firewall-list'),
+    path('show/<str:name>', views.show, name='show'),
+]
+
+

+ 45 - 0
vycenter/firewall/views.py

@@ -0,0 +1,45 @@
+from django.shortcuts import render
+from django.http import HttpResponse
+from django.template import loader
+from django.shortcuts import redirect
+
+
+import vyos
+
+
+
+def index(request):
+    #interfaces = vyos.get_interfaces()
+    all_instances = vyos.instance_getall()
+    hostname_default = vyos.get_hostname_prefered(request)
+
+    firewall_all = vyos.get_firewall_all(hostname_default)
+
+    template = loader.get_template('firewall/list.html')
+    context = { 
+        #'interfaces': interfaces,
+        'instances': all_instances,
+        'hostname_default': hostname_default,
+        'firewall_all':  firewall_all
+    }   
+    return HttpResponse(template.render(context, request))
+
+
+
+def show(request, name):
+    #interfaces = vyos.get_interfaces()
+    all_instances = vyos.instance_getall()
+    hostname_default = vyos.get_hostname_prefered(request)
+
+    firewall_all = vyos.get_firewall_all(hostname_default)
+
+    template = loader.get_template('firewall/show.html')
+    context = { 
+        #'interfaces': interfaces,
+        'instances': all_instances,
+        'hostname_default': hostname_default,
+        'firewall_all':  firewall_all
+    }   
+    return HttpResponse(template.render(context, request))
+
+

+ 1 - 0
vycenter/vycenter/settings.py

@@ -39,6 +39,7 @@ INSTALLED_APPS = [
     'django.contrib.staticfiles',
     'instance.apps.InstanceConfig',
     'config.apps.ConfigConfig',
+    'firewall.apps.FirewallConfig',
 ]
 
 MIDDLEWARE = [

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

@@ -56,6 +56,21 @@
         margin-right: 10px;
     }
     
+    #debug {
+        background-color:palegoldenrod;
+        margin-top: 20px;
+    }
+    #debug h3 {
+        font-size: 14px;
+        padding-top: 10px;
+        padding-bottom: 10px;
+        margin: 0;
+    }  
+    #debug pre {
+      font-size: 12px;
+      padding-top: 0;   
+      margin: 0;  
+    }
     </style>
   </head>
   <body >
@@ -103,11 +118,12 @@
 
     <ol>
     <li><a href="/instance/">Dashboard</a></li>
+    <li><a href="{% url 'firewall:firewall-list' %}">Firewall</a></li>
+
     <li>Static Routing</li>
     <li>IPSEC</li>
     <li>BGP</li>
     <li>OSPF</li>
-    <li>Firewall</li>
     <li>NAT</li>
     </ol>
 
@@ -133,6 +149,12 @@
 </div>
 
 
+<div class="container" id="debug">
+  <h3>Debug</h3>
+  <pre>
+  {% block debug %}{% endblock %}
+</pre>
+</div>
 
     <!-- Optional JavaScript -->
     <!-- jQuery first, then Popper.js, then Bootstrap JS -->

+ 1 - 0
vycenter/vycenter/urls.py

@@ -26,5 +26,6 @@ urlpatterns = [
     path('instance/', include('instance.urls')),
     path('config/', include('config.urls')),
     path('dashboard/', include('dashboard.urls')),
+    path('firewall/', include('firewall.urls')),
     path('', views.vycenter_login, name='vycenter-login'),
 ]

+ 37 - 0
vycenter/vyos.py

@@ -200,3 +200,40 @@ def get_interface(interface_type, interface_name, hostname):
 
     return result1['data']
 
+
+
+def get_firewall_all(hostname):
+    cmd = {"op": "showConfig", "path": ["firewall"]}
+
+    print(json.dumps(cmd))
+    post = {'key': get_key(hostname), 'data': json.dumps(cmd)}
+    print(post)
+
+
+    # curl -X POST -F data='{"op": "showConfig", "path": ["interfaces", "dummy"]}' -F key=qwerty http://127.0.0.1:8080/retrieve
+    # {"success": true, "data": " /* So very dummy */\n dummy dum0 {\n     address 192.168.168.1/32\n     address 192.168.168.2/32\n     /* That is a description */\n     description \"Test interface\"\n }\n dummy dum1 {\n     address 203.0.113.76/32\n     address 203.0.113.79/32\n }\n", "error": null}
+
+    try:
+        resp = requests.post(get_url_retrieve(hostname), verify=False, data=post, timeout=15)
+    except requests.exceptions.ConnectionError:
+        return False
+
+    print(resp.status_code)
+    pprint.pprint(resp)
+
+    pprint.pprint(resp.json())
+
+
+    if resp.status_code != 200:
+        # This means something went wrong.
+        #raise ApiError('POST /tasks/ {}'.format(resp.status_code))
+        return "erro"
+    #for todo_item in resp.json():
+        #print('{} {}'.format(todo_item['id'], todo_item['summary']))
+
+    result1 = resp.json()
+    print(result1['data'])
+    #result2 = json.loads(result1['data'])
+    pprint.pprint(result1)
+
+    return result1['data']