Roberto Berto 5 роки тому
батько
коміт
798c4633ee

+ 19 - 0
vycenter/config/migrations/0005_instancedefault.py

@@ -0,0 +1,19 @@
+# Generated by Django 2.0.3 on 2020-04-27 19:06
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('config', '0004_auto_20200427_1159'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='InstanceDefault',
+            fields=[
+                ('hostname', models.CharField(max_length=120, primary_key=True, serialize=False)),
+            ],
+        ),
+    ]

+ 21 - 0
vycenter/config/migrations/0006_auto_20200427_2202.py

@@ -0,0 +1,21 @@
+# Generated by Django 2.0.3 on 2020-04-27 22:02
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('config', '0005_instancedefault'),
+    ]
+
+    operations = [
+        migrations.DeleteModel(
+            name='InstanceDefault',
+        ),
+        migrations.AddField(
+            model_name='instance',
+            name='main',
+            field=models.BooleanField(default=False),
+        ),
+    ]

+ 2 - 1
vycenter/config/models.py

@@ -7,6 +7,7 @@ class Instance(models.Model):
     port = models.IntegerField()
     key = models.CharField(max_length=100)
     https = models.BooleanField()
-
+    main = models.BooleanField(default=False)
+    
 
 

+ 13 - 2
vycenter/config/templates/config/instances.html

@@ -8,16 +8,27 @@
 {% if instances %}
 
     <table border="1" width="100%">
-    <tr><th>alias</th><th>hostname</th><th>port</th><th>key</th><th>https</th><th>test connection</th></tr>
+    <tr>
+        <th>alias</th>
+        <th>hostname</th>
+        <th>port</th>
+        <th>key</th>
+        <th>https</th>
+        <th>test connection</th>
+        <th>default</th>
+        <th>remove</th>
+    </tr>
 
     {% for instance in instances %}
     <tr>
         <td>{{ instance.alias }}</td>
         <td>{{ instance.hostname }}</td>
         <td>{{ instance.port }}</td>
-        <td>{{ instance.key }}</td>
+        <td>show</td>
         <td>{{ instance.https }}</td>
         <td><a href="{% url 'config:instance-conntry' instance.hostname %}">test</a></td>
+        <td>{% if instance.main == True %}default{% else %}<a href="{% url 'config:instance-default' instance.hostname %}">set default</a>{% endif %}</td>
+        <td>{% if instance.main == True %}-{% else %}<a href="{% url 'config:instance-remove' instance.hostname %}">remove</a>{% endif %}</td>
     </tr>
 
     {% endfor %}

+ 3 - 0
vycenter/config/urls.py

@@ -9,6 +9,9 @@ urlpatterns = [
     path('', views.index, name='index'),
     path('instance-add', views.instance_add, name='instance-add'),
     path('instance-conntry/<str:hostname>', views.instance_conntry, name='instance-conntry'),
+    path('instance-default/<str:hostname>', views.instance_default, name='instance-default'),
+    path('instance-remove/<str:hostname>', views.instance_remove, name='instance-remove'),
+
     path('instances', views.instances, name='instances'),
 
 ]

+ 48 - 1
vycenter/config/views.py

@@ -1,6 +1,8 @@
 from django.shortcuts import render
 from django.http import HttpResponse
 from django.template import loader
+from django.shortcuts import redirect
+
 
 import vyos
 
@@ -10,11 +12,14 @@ from .models import Instance
 def index(request):
     #interfaces = vyos.get_interfaces()
     all_instances = vyos.instance_getall()
+    hostname_default = vyos.get_hostname_prefered(request)
+
 
     template = loader.get_template('config/instance.html')
     context = { 
         #'interfaces': interfaces,
         'instances': all_instances,
+        'hostname_default': hostname_default,
     }   
     return HttpResponse(template.render(context, request))
 
@@ -22,17 +27,21 @@ def index(request):
 
 def instances(request):
     all_instances = vyos.instance_getall()
-    
+    hostname_default = vyos.get_hostname_prefered(request)
+
 
     template = loader.get_template('config/instances.html')
     context = { 
         'instances': all_instances,
+        'hostname_default': hostname_default,
+
     }   
     return HttpResponse(template.render(context, request))
 
 def instance_add(request):
     #interfaces = vyos.get_interfaces()
     all_instances = vyos.instance_getall()
+    hostname_default = vyos.get_hostname_prefered(request)
 
     if len(request.POST) > 0:
         instance = Instance()
@@ -50,6 +59,7 @@ def instance_add(request):
 
     template = loader.get_template('config/instance_add.html')
     context = { 
+        'hostname_default': hostname_default,
         'instance_id': instance_id,
         'instances': all_instances,
     }   
@@ -57,18 +67,55 @@ def instance_add(request):
 
 def instance_conntry(request, hostname):
     all_instances = vyos.instance_getall()
+    hostname_default = vyos.get_hostname_prefered(request)
 
     # permcheck
     instance = Instance.objects.get(hostname=hostname)
     connected = vyos.conntry(hostname)
+    if connected == True:
+        request.session['hostname'] = hostname
+
 
     template = loader.get_template('config/instance_conntry.html')
     context = { 
         'instance': instance,
         "connected": connected,
         'instances': all_instances,
+        'hostname_default': hostname_default,
     }   
     return HttpResponse(template.render(context, request))
 
 
+def instance_default(request, hostname):
+    all_instances = vyos.instance_getall()
+
+    # permcheck
+    instance = Instance.objects.get(hostname=hostname)
+    
+    connected = vyos.conntry(hostname)
+    # show some error when not connected
+    if connected == True:
+        request.session['hostname'] = hostname
+        instance.main = True
+        instance.save()
+
+    return redirect('config:instances')
+
+
+
+def instance_remove(request, hostname):
+    all_instances = vyos.instance_getall()
+
+    # permcheck
+    instance = Instance.objects.get(hostname=hostname)
+    
+    hostname_default = vyos.get_hostname_prefered(request)
+
+    if hostname_default != hostname:
+        instance.delete()
+
+    return redirect('config:instances')
+
+
+
 

+ 18 - 0
vycenter/vycenter/settings.py

@@ -49,8 +49,11 @@ MIDDLEWARE = [
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware',
+    
 ]
 
+SESSION_ENGINE = "django.contrib.sessions.backends.cache"
+
 ROOT_URLCONF = 'vycenter.urls'
 
 TEMPLATES = [
@@ -69,6 +72,21 @@ TEMPLATES = [
     },
 ]
 
+TEMPLATE_CONTEXT_PROCESSORS = (
+#default settings 
+"django.contrib.auth.context_processors.auth",
+"django.core.context_processors.debug",
+"django.core.context_processors.i18n",
+"django.core.context_processors.media",
+"django.core.context_processors.static",
+"django.core.context_processors.tz",
+"django.contrib.messages.context_processors.messages",
+
+#custome settings for loding request in templet
+"django.core.context_processors.request",
+)
+
+
 WSGI_APPLICATION = 'vycenter.wsgi.application'
 
 

+ 62 - 9
vycenter/vycenter/templates/base.html

@@ -27,27 +27,80 @@
         background-color: #ccc;
         padding: 5px;
     }
+    h1 {
+      font-size: 10px;      
+    }
+    #menu-logotop {
+      background-color: #444E5A;
+      padding: 0;
+    }
+    #menu-topline {
+      padding-top: 3px;
+      background-color: #AD343E;
+    }
+    h2 {
+      font-size: 12px;
+    }
+    ol {
+      font-size: 11px;
+    }
+    p.menu-config {
+      padding: 0 5px;
+      margin: 0;
+    }
+    form.instancedefault {
+      display: inline;
+    }
 
-
+    #vyos-id,  #vycenter-config-menu {
+        margin-right: 10px;
+    }
+    
     </style>
   </head>
   <body >
-    <h1 align="center">VyCenter</h1>
 
+    <div class="container" id="menu-logotop" >
+      <div id="menu-topline"></div>
+        
+      <div class="row ">
+        <div class="col-3 ">
+          <h1 align="left"><img src="https://storage.googleapis.com/imgvycenter/logo/logoreduzido20h.png" height="20" alt="vycenter"></h1>
+        </div>
+        <div class="col-9">
+          <form action="/config/instance-default" method="get" id="instancedefault">
+            <p class="text-right menu-config">
+            
+              <select name="vyos-id" id="vyos-id" onchange="this.form.submit();">
+                {% for instance in instances %}
+                <option value="{{instance.hostname}}" {% if hostname_default == instance.hostname %}selected="selected"{% endif %}>{{instance.alias}}</option>
+                {% endfor %}
+          
+                
+              
+              </select>
+            
+
+            
+              <span id="vycenter-config-menu">Config</span>
+              <span id="vycenter-config-user">Usuário</span>
+
+          </p>
+        </form>
+        </div>
+      </div>
+    </div>
 
 <div class="container">
   <div class="row">
     <div class="col-3 menu">
 
     <h2>Manage VyOS</h2>
-    <p><select name="vyos-id">
-      {% for instance in instances %}
-      <option value="{{instance.hostname}}">{{instance.alias}}</option>
-      {% endfor %}
 
-      
-    
-    </select></p>
+
+
+
+
     <ol>
     <li><a href="/instance/">Dashboard</a></li>
     <li>Static Routing</li>

+ 12 - 0
vycenter/vycenter/templates/vycenter/vycenter_login.html

@@ -0,0 +1,12 @@
+{% extends "base.html" %}
+
+{% block header_title %}Login{% endblock %}
+{% block section_title %}Login{% endblock %}
+
+{% block content %}
+
+<p>Auth not yet implemented. Proceed to <a href="{% url 'config:instances' %}">list os instances</a>.</p>
+
+
+
+{% endblock %}

+ 6 - 1
vycenter/vycenter/urls.py

@@ -16,10 +16,15 @@ Including another URLconf
 from django.contrib import admin
 from django.urls import include, path
 
+from . import views
+
+
+app_name = 'vycenter'
+
 
 urlpatterns = [
     path('instance/', include('instance.urls')),
     path('config/', include('config.urls')),
     path('dashboard/', include('dashboard.urls')),
-    path('admin/', admin.site.urls),
+    path('', views.vycenter_login, name='vycenter-login'),
 ]

+ 18 - 0
vycenter/vycenter/views.py

@@ -0,0 +1,18 @@
+from django.shortcuts import render
+from django.http import HttpResponse
+from django.template import loader
+
+import vyos
+
+from config.models import Instance
+
+
+def vycenter_login(request):
+    
+    template = loader.get_template('vycenter/vycenter_login.html')
+    context = {
+    }
+    return HttpResponse(template.render(context, request))
+
+
+

+ 14 - 1
vycenter/vyos.py

@@ -42,6 +42,19 @@ def get_key(hostname):
     instance = Instance.objects.get(hostname=hostname)
     return instance.key
 
+def get_hostname_prefered(request):
+    hostname = None
+
+    if request.session.get('hostname', None) != None:
+        hostname = request.session.get('hostname', None)
+        
+
+    if hostname == None:
+        instance = Instance.objects.get(main=True)
+        hostname = instance.hostname
+
+    return hostname 
+    
 
 #data='{"op": "showConfig", "path": ["interfaces", "dummy"]}
 def instance_getall():
@@ -151,7 +164,7 @@ def get_interfaces(hostname="179.127.12.142"):
 
     return result1['data']
 
-def get_interface(interface_type, interface_name, hostname="179.127.12.142"):
+def get_interface(interface_type, interface_name, hostname):
     cmd = {"op": "showConfig", "path": ["interfaces", interface_type, interface_name]}
 
     print(json.dumps(cmd))