浏览代码

added module routes static #27

Roberto Berto 5 年之前
父节点
当前提交
06e902f379
共有 3 个文件被更改,包括 63 次插入9 次删除
  1. 29 1
      vycenter/static/templates/static/list.html
  2. 13 5
      vycenter/static/views.py
  3. 21 3
      vycenter/vyos.py

+ 29 - 1
vycenter/static/templates/static/list.html

@@ -11,10 +11,38 @@
 
 
 
-<p>
+<p class="margin-topbottom">
     <a href="{% url 'static:static' %}">Create new static route</a>
 </p>
 
+
+{% if static_list %}
+
+    <table border="1" width="100%">
+    <tr>
+        <th>route</th>
+        <th>next-hop (router to use)</th>
+    </tr>
+
+    {% for x in static_list %}
+    <tr>
+        <td>{{ x.route }}</td>
+        <td>
+            {% for y in x.nexthop %}
+                {{ y }}
+            {% endfor %}
+        </td>
+
+    </tr>
+
+    {% endfor %}
+
+    </table>   
+{% else %}
+    <p>No static routes.</p>
+{% endif %}
+
+
     
 
 

+ 13 - 5
vycenter/static/views.py

@@ -15,9 +15,14 @@ def index(request):
         
     all_instances = vyos.instance_getall()
     hostname_default = vyos.get_hostname_prefered(request)
-    #static_list = vyos.get_route_static(hostname_default)
-    static_list = None
-    
+    static_dict = vyos.get_route_static(hostname_default)
+    static_list = []
+    for s in static_dict['route']:
+        static_list.append({
+            'route': s,
+            'nexthop': static_dict['route'][s]['next-hop'],
+        })
+
     template = loader.get_template('static/list.html')
     context = { 
         'instances': all_instances,
@@ -36,16 +41,19 @@ def static(request):
     hostname_default = vyos.get_hostname_prefered(request)
     static_list = vyos.get_route_static(hostname_default)
 
+
+
     error_message = None
     if 'subnet' in request.POST and 'nexthop' in request.POST:
-        #return1 = vyos.set_route_static(hostname_default, request.POST['subnet'], request.POST['nexthop'])
-        return1 = False
+        return1 = vyos.set_route_static(hostname_default, request.POST['subnet'], request.POST['nexthop'])
         if return1 == False: 
             error_message = 'Cannot add static route.'
         else:
            return redirect('static:static-list')
 
 
+    ippath = vyos.ip_route(hostname_default)
+
     template = loader.get_template('static/static.html')
     context = { 
         'instances': all_instances,

+ 21 - 3
vycenter/vyos.py

@@ -28,6 +28,11 @@ def get_url_configure(hostname):
     url = get_url(hostname) + '/configure'
     return url
 
+def get_url_show(hostname):
+    url = get_url(hostname) + '/show'
+    return url
+
+
 def get_url_retrieve(hostname):
     url = get_url(hostname) + '/retrieve'        
     return url
@@ -44,6 +49,8 @@ def api(type, hostname, cmd):
         url = get_url_manage(hostname)
     elif type == "configure":
         url = get_url_configure(hostname)
+    elif type == "show":
+        url = get_url_show(hostname)        
     else:
         return False
 
@@ -79,6 +86,10 @@ def api(type, hostname, cmd):
 def api_get(hostname, cmd):
     return api('retrieve', hostname, cmd)
 
+
+def api_show(hostname, cmd):
+    return api('show', hostname, cmd)    
+
 def api_set(hostname, cmd):
     return api('configure', hostname, cmd)    
 
@@ -182,13 +193,20 @@ def insert_firewall_rules(hostname, cmd):
     return result1
 
 def get_route_static(hostname):
-    cmd = {"op": "showConfig", "path": ["show","protocols","route","static"]}
+    cmd = {"op": "showConfig", "path": ["protocols","static","route"]}
 
     result1 = api_get(hostname, cmd)
     return result1
 
 def set_route_static(hostname, subnet, nexthop):
-    cmd = {"op": "set", "path": ["set","protocols","static","route", subnet, "next-hop", nexthop]}
+    cmd = {"op": "set", "path": ["protocols","static","route", subnet, "next-hop", nexthop]}
 
     result1 = api_set(hostname, cmd)
-    return result1    
+    return result1    
+
+
+def ip_route(hostname):
+    cmd = {"op": "show", "path": ["ip","route"]}
+
+    result1 = api_show(hostname, cmd)
+    return result1