Explorar o código

first API working version

Roberto Berto %!s(int64=5) %!d(string=hai) anos
pai
achega
4d9181aae3

+ 1 - 0
requirements.txt

@@ -2,3 +2,4 @@ asgiref==3.2.7
 Django==3.0.5
 pytz==2019.3
 sqlparse==0.3.1
+requests

BIN=BIN
vygui/.vyos.py.swp


+ 0 - 0
vygui/config/__init__.py


+ 3 - 0
vygui/config/admin.py

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

+ 5 - 0
vygui/config/apps.py

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

+ 0 - 0
vygui/config/migrations/__init__.py


+ 3 - 0
vygui/config/models.py

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

+ 3 - 0
vygui/config/tests.py

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

+ 7 - 0
vygui/config/urls.py

@@ -0,0 +1,7 @@
+from django.urls import path
+
+from . import views
+
+urlpatterns = [
+    path('', views.index, name='index'),
+]

+ 7 - 0
vygui/config/views.py

@@ -0,0 +1,7 @@
+from django.shortcuts import render
+
+from django.http import HttpResponse
+
+
+def index(request):
+    return HttpResponse("Hello, world. You're at the polls index.")

+ 0 - 0
vygui/dashboard/__init__.py


+ 3 - 0
vygui/dashboard/admin.py

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

+ 5 - 0
vygui/dashboard/apps.py

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

+ 0 - 0
vygui/dashboard/migrations/__init__.py


+ 3 - 0
vygui/dashboard/models.py

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

+ 3 - 0
vygui/dashboard/tests.py

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

+ 7 - 0
vygui/dashboard/urls.py

@@ -0,0 +1,7 @@
+from django.urls import path
+
+from . import views
+
+urlpatterns = [
+    path('', views.index, name='index'),
+]

+ 7 - 0
vygui/dashboard/views.py

@@ -0,0 +1,7 @@
+from django.shortcuts import render
+
+from django.http import HttpResponse
+
+
+def index(request):
+    return HttpResponse("Hello, world. You're at the polls index.")

+ 17 - 0
vygui/device/templates/device/index.html

@@ -0,0 +1,17 @@
+{% if interfaces %}
+    <table border="1" width="100%">
+    <tr><th>type</th><th>name</th><th>address</th></tr>
+
+    {% for key, value in interfaces.items %}
+        <tr><td>{{ key }}</td>
+        {% for ifkey, ifvalue in value.items %}
+            <td>{% url 'device-views-interface' key ifkey as url_interface %}
+            <a href="{{ url_interface }}">{{ ifkey }}</a></td><td>{{ ifvalue.address }}</td>
+        {% endfor %}
+        </tr>
+    {% endfor %}
+
+    </table>
+{% else %}
+    <p>No interfaces.</p>
+{% endif %}

+ 18 - 0
vygui/device/templates/device/interface.html

@@ -0,0 +1,18 @@
+{% if interface %}
+    {{ interface }}
+    <table border="1" width="100%">
+    <tr><th>type</th><th>name</th><th>address</th></tr>
+
+    {% for key, value in interface.items %}
+        <tr><td>{{ key }}</td>
+        {% for ifkey, ifvaluekey in value.items %}
+            <td>{% url 'device-views-interface' key ifkey as url_interface %}
+            <a href="{{ url_interface }}">{{ ifkey }}</a></td><td>{{ ifvalue.address }}</td>
+        {% endfor %}
+        </tr>
+    {% endfor %}
+
+    </table>
+{% else %}
+    <p>Invalid interface.</p>
+{% endif %}

+ 8 - 0
vygui/device/urls.py

@@ -0,0 +1,8 @@
+from django.urls import path
+
+from . import views
+
+urlpatterns = [
+    path('', views.index, name='index'),
+    path('interface/<slug:interface_type>/<slug:interface_name>', views.interface, name='device-views-interface'),
+]

+ 22 - 1
vygui/device/views.py

@@ -1,3 +1,24 @@
 from django.shortcuts import render
+from django.http import HttpResponse
+from django.template import loader
+
+import vyos
+
+def index(request):
+    interfaces = vyos.get_interfaces()
+    
+    template = loader.get_template('device/index.html')
+    context = {
+        'interfaces': interfaces,
+    }
+    return HttpResponse(template.render(context, request))
+
+def interface(request, interface_type, interface_name):
+    interface = vyos.get_interface(interface_type, interface_name)
+    
+    template = loader.get_template('device/interface.html')
+    context = { 
+        'interface': interface,
+    }   
+    return HttpResponse(template.render(context, request))
 
-# Create your views here.

+ 0 - 0
vygui/foo/__init__.py


+ 3 - 0
vygui/foo/admin.py

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

+ 5 - 0
vygui/foo/apps.py

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

+ 0 - 0
vygui/foo/migrations/__init__.py


+ 3 - 0
vygui/foo/models.py

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

+ 3 - 0
vygui/foo/tests.py

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

+ 17 - 0
vygui/foo/views.py

@@ -0,0 +1,17 @@
+from django.shortcuts import render
+from django.http import HttpResponse
+from django.template import loader
+
+import vyos
+
+def index(request):
+    interfaces = vyos.get_interfaces()
+    
+    template = loader.get_template('server/index.html')
+    context = {
+        'interfaces': interfaces,
+    }
+    return HttpResponse(template.render(context, request))
+
+
+

+ 1 - 0
vygui/vygui/settings.py

@@ -37,6 +37,7 @@ INSTALLED_APPS = [
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
+    'device.apps.DeviceConfig',
 ]
 
 MIDDLEWARE = [

+ 5 - 1
vygui/vygui/urls.py

@@ -14,8 +14,12 @@ Including another URLconf
     2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
 """
 from django.contrib import admin
-from django.urls import path
+from django.urls import include, path
+
 
 urlpatterns = [
+    path('device/', include('device.urls')),
+    path('config/', include('config.urls')),
+    path('dashboard/', include('dashboard.urls')),
     path('admin/', admin.site.urls),
 ]

+ 114 - 0
vygui/vyos.py

@@ -0,0 +1,114 @@
+import requests
+import json
+import pprint
+import sys
+
+#curl -k -X POST -F data='{"op": "set", "path": ["interfaces", "dummy", "dum1", "address"], "value": "203.0.113.76/32"}' -F key=a6ffb742a8a631a65b07ab2026258629da2632fd https://179.127.12.142:44302/configure
+
+sys.path.append('/var/secrets')
+import local
+
+
+
+
+ 
+SERVER_URL_MANAGE = local.SERVER_URL + 'config-file'
+SERVER_URL_CONFIG = local.SERVER_URL + 'configure'
+SERVER_URL_RETRIE = local.SERVER_URL + 'retrieve'
+
+#data='{"op": "showConfig", "path": ["interfaces", "dummy"]}
+
+
+def getall():
+    #cmd = {"op": "save", "file": "/config/config.boot"}
+    cmd = {"op": "showConfig", "path": ["interfaces", "dummy"]}
+
+    print(json.dumps(cmd))
+    post = {'key': local.SERVER_KEY, '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}
+
+    resp = requests.post(SERVER_URL_RETRIE, verify=False, data=post)
+    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']))
+
+    return resp
+
+
+def get_interfaces():
+    cmd = {"op": "showConfig", "path": ["interfaces"]}
+
+    print(json.dumps(cmd))
+    post = {'key': local.SERVER_KEY, '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}
+
+    resp = requests.post(SERVER_URL_RETRIE, verify=False, data=post)
+    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']
+
+def get_interface(interface_type, interface_name):
+    cmd = {"op": "showConfig", "path": ["interfaces", interface_type, interface_name]}
+
+    print(json.dumps(cmd))
+    post = {'key': local.SERVER_KEY, '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}
+
+    resp = requests.post(SERVER_URL_RETRIE, verify=False, data=post)
+    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']
+