From 862e5f8d8f787e055a4671a69c2eda56241317f5 Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Thu, 28 Feb 2019 09:16:45 +0100
Subject: [PATCH] Add view network page

JIRA INFRA-860 #action In Progress
---
 app/network/views.py                    |  7 ++++
 app/static/js/hosts.js                  | 12 +++++++
 app/templates/_helpers.html             | 10 ++++++
 app/templates/network/networks.html     |  6 ++--
 app/templates/network/view_host.html    |  4 +--
 app/templates/network/view_network.html | 45 +++++++++++++++++++++++++
 6 files changed, 79 insertions(+), 5 deletions(-)
 create mode 100644 app/templates/network/view_network.html

diff --git a/app/network/views.py b/app/network/views.py
index 2636b81..11f0f2a 100644
--- a/app/network/views.py
+++ b/app/network/views.py
@@ -585,6 +585,13 @@ def list_networks():
     return render_template("network/networks.html", networks=networks)
 
 
+@bp.route("/networks/view/<vlan_name>")
+@login_required
+def view_network(vlan_name):
+    network = models.Network.query.filter_by(vlan_name=vlan_name).first_or_404()
+    return render_template("network/view_network.html", network=network)
+
+
 @bp.route("/networks/create", methods=("GET", "POST"))
 @login_groups_accepted("admin")
 def create_network():
diff --git a/app/static/js/hosts.js b/app/static/js/hosts.js
index 8336fe3..b6c590b 100644
--- a/app/static/js/hosts.js
+++ b/app/static/js/hosts.js
@@ -9,6 +9,15 @@ $(document).ready(function() {
     return '<a href="'+ url + '">' + data + '</a>';
   }
 
+  function render_network_link(data) {
+    // render funtion to create link to Network view page
+    if ( data === null ) {
+      return data;
+    }
+    var url = $SCRIPT_ROOT + "/network/networks/view/" + data;
+    return '<a href="'+ url + '">' + data + '</a>';
+  }
+
   if( $("#hostForm").length || $("#editHostForm").length ) {
     var hostVarsEditor = CodeMirror.fromTextArea(ansible_vars, {
         lineNumbers: true,
@@ -158,6 +167,9 @@ $(document).ready(function() {
         defaultContent: "",
         orderable: false },
       { data: 'interfaces.0.network',
+        render: function(data, type, row) {
+          return render_network_link(data);
+        },
         defaultContent: "",
         orderable: false }
     ]
diff --git a/app/templates/_helpers.html b/app/templates/_helpers.html
index 29c2084..353685d 100644
--- a/app/templates/_helpers.html
+++ b/app/templates/_helpers.html
@@ -39,6 +39,10 @@
 {% if active %}active{% endif %}
 {%- endmacro %}
 
+{% macro link_to_network(network) -%}
+  <a href="{{ url_for('network.view_network', vlan_name=network.vlan_name) }}">{{ network.vlan_name }}</a>
+{%- endmacro %}
+
 {% macro link_to_host(host) -%}
   <a href="{{ url_for('network.view_host', name=host.name) }}">{{ host.fqdn|default(host.name) }}</a>
 {%- endmacro %}
@@ -49,6 +53,12 @@
   {% endfor %}
 {%- endmacro %}
 
+{% macro link_to_interfaces_host(interfaces) -%}
+  {% for interface in interfaces %}
+    {{ link_to_host(interface.host) }}
+  {% endfor %}
+{%- endmacro %}
+
 {% macro link_to_item(ics_id) -%}
   {% if ics_id %}
     <a href="{{ url_for('inventory.view_item', ics_id=ics_id) }}">{{ ics_id }}</a>
diff --git a/app/templates/network/networks.html b/app/templates/network/networks.html
index f28dd3a..2b97346 100644
--- a/app/templates/network/networks.html
+++ b/app/templates/network/networks.html
@@ -1,5 +1,5 @@
 {% extends "base-fluid.html" %}
-{% from "_helpers.html" import is_active %}
+{% from "_helpers.html" import is_active, link_to_network %}
 
 {% block title %}Networks - CSEntry{% endblock %}
 
@@ -36,9 +36,9 @@
     <tbody>
       {% for network in networks %}
         <tr>
-          <td>{{ network.vlan_name }}</td>
+          <td>{{ link_to_network(network) }}</td>
           <td>{{ network.vlan_id }}</td>
-          <td>{{ network.description }}</td>
+          <td>{{ network.description | safe }}</td>
           <td>{{ network.address }}</td>
           <td>{{ network.first_ip }}</td>
           <td>{{ network.last_ip }}</td>
diff --git a/app/templates/network/view_host.html b/app/templates/network/view_host.html
index 38c9b45..a1dda62 100644
--- a/app/templates/network/view_host.html
+++ b/app/templates/network/view_host.html
@@ -1,6 +1,6 @@
 {% extends "network/hosts.html" %}
 {% from "_helpers.html" import link_to_items, link_to_stack_members,
-   link_to_ansible_groups,
+   link_to_ansible_groups, link_to_network,
    delete_button_with_confirmation, render_field, submit_button_with_confirmation %}
 
 {% block title %}View Host - CSEntry{% endblock %}
@@ -121,7 +121,7 @@
         <td>{{ interface.cnames | join(' ') }}</td>
         <td>{{ interface.ip }}</td>
         <td>{{ interface.mac }}</td>
-        <td>{{ interface.network }}</td>
+        <td>{{ link_to_network(interface.network) }}</td>
         <td>{{ interface.tags | join(' ') }}</td>
       </tr>
       {% endfor %}
diff --git a/app/templates/network/view_network.html b/app/templates/network/view_network.html
new file mode 100644
index 0000000..8527eb9
--- /dev/null
+++ b/app/templates/network/view_network.html
@@ -0,0 +1,45 @@
+{% extends "network/networks.html" %}
+{% from "_helpers.html" import link_to_interfaces_host %}
+
+{% block title %}View Ansible network - CSEntry{% endblock %}
+
+{% block networks_nav %}
+    <li class="nav-item">
+      <a class="nav-link active" href="{{ url_for('network.view_network', vlan_name=network.vlan_name) }}">View network</a>
+    </li>
+{% endblock %}
+
+{% block networks_main %}
+  <div class="row">
+    <div class="col-sm-9">
+      <dl class="row">
+        <dt class="col-sm-3">Vlan name</dt>
+        <dd class="col-sm-9">{{ network.vlan_name }}</dd>
+        <dt class="col-sm-3">Vlan id</dt>
+        <dd class="col-sm-9">{{ network.vlan_id }}</dd>
+        <dt class="col-sm-3">Description</dt>
+        <dd class="col-sm-9">{{ network.description | safe }}</dd>
+        <dt class="col-sm-3">Address</dt>
+        <dd class="col-sm-9">{{ network.address }}</dd>
+        <dt class="col-sm-3">First IP</dt>
+        <dd class="col-sm-9">{{ network.first_ip }}</dd>
+        <dt class="col-sm-3">Last IP</dt>
+        <dd class="col-sm-9">{{ network.last_ip }}</dd>
+        <dt class="col-sm-3">Gateway</dt>
+        <dd class="col-sm-9">{{ network.gateway }}</dd>
+        <dt class="col-sm-3">Network Scope</dt>
+        <dd class="col-sm-9">{{ network.scope }}</dd>
+        <dt class="col-sm-3">Domain</dt>
+        <dd class="col-sm-9">{{ network.domain }}</dd>
+        <dt class="col-sm-3">Admin only</dt>
+        <dd class="col-sm-9">{{ network.admin_only }}</dd>
+        <dt class="col-sm-3">Hosts</dt>
+        <dd class="col-sm-9">{{ link_to_interfaces_host(network.interfaces) }}</dd>
+        <dt class="col-sm-3">Created by</dt>
+        <dd class="col-sm-9">{{ network.user }}</dd>
+        <dt class="col-sm-3">Created at</dt>
+        <dd class="col-sm-9">{{ network.created_at }}</dd>
+      </dl>
+    </div>
+  </div>
+{%- endblock %}
-- 
GitLab