From 1787840687f55dcdc3ed2764e34e56e80147d3e0 Mon Sep 17 00:00:00 2001
From: Pavel Vomacka <pvomacka@redhat.com>
Date: Fri, 21 Jul 2017 12:45:47 +0200
Subject: [PATCH] WebUI: Cockpit integration

Link to the cockpit is placed to each host details page in case
the Cockpit is installed on the server.

Showing or hiding cockpit link is possible because of Cockpit's API
which provides public URL for check whether Cockpit is the software
which listen on given port.

https://pagure.io/freeipa/issue/4891
---
 install/ui/src/freeipa/host.js | 74 ++++++++++++++++++++++++++++++++++++++++++
 ipaserver/plugins/internal.py  |  4 +++
 2 files changed, 78 insertions(+)

diff --git a/install/ui/src/freeipa/host.js b/install/ui/src/freeipa/host.js
index ac434d8455..8f2cd6fb34 100644
--- a/install/ui/src/freeipa/host.js
+++ b/install/ui/src/freeipa/host.js
@@ -184,6 +184,16 @@ return {
                     ]
                 },
                 {
+                    name: 'cockpit_integration',
+                    label: '@i18n:objects.host.cockpit_title',
+                    $factory: IPA.section,
+                    fields: [
+                        {
+                            $type: 'cockpit_link'
+                        }
+                    ]
+                },
+                {
                     $factory: IPA.section,
                     name: 'divider',
                     layout_css_class: 'col-sm-12',
@@ -653,6 +663,69 @@ IPA.host_fqdn_field = function(spec) {
     return that;
 };
 
+IPA.cockpit_link_widget = function(spec) {
+    spec = spec || {};
+
+    var that = IPA.link_widget(spec);
+
+    /**
+     * Port on which Cockpit is listening.
+     *
+     * In the future might be configurable.
+     * @property {number}
+     */
+    that.port = spec.port || 9090;
+
+    /**
+     * Cockpit component link.
+     *
+     * It is possible to set Cockpit component which will be shown by default
+     * after clicking on the link.
+     * @property {string}
+     */
+    that.component = spec.component || '';
+
+    that.check_cockpit_availability = function(link) {
+        var xhr = $.ajax({
+            method: 'GET',
+            url: link + '/ping',
+            success: function() {
+                that.link.css('display', '');
+            },
+            error: function() {
+                that.nonlink.css('display', '');
+            }
+        });
+    };
+
+    that.update = function() {
+
+        that.update_link();
+
+        var pkey = this.facet.get_pkey();
+        var link = "https://" + pkey + ":" + that.port;
+        var msg = text.get('@i18n:objects.host.cockpit_not_detected');
+        msg = msg.replace(/\$\{host\}/g, pkey);
+
+        that.link.attr("href", link + that.component);
+        that.link.html(link);
+        that.nonlink.html(msg);
+
+        that.check_cockpit_availability(link);
+    };
+
+    that.update_link = function() {
+
+        var link = false;
+        var nonlink = false;
+
+        that.link.css('display', link ? '' : 'none');
+        that.nonlink.css('display', nonlink ? '' : 'none');
+    };
+
+    return that;
+};
+
 IPA.host_adder_dialog = function(spec) {
 
     spec = spec || {};
@@ -1124,6 +1197,7 @@ exp.register = function() {
     w.register('force_host_add_checkbox', IPA.force_host_add_checkbox_widget);
     f.register('host_password', IPA.field);
     w.register('host_password', IPA.host_password_widget);
+    w.register('cockpit_link', IPA.cockpit_link_widget);
 
     a.register('host_unprovision', exp.unprovision_action);
 };
diff --git a/ipaserver/plugins/internal.py b/ipaserver/plugins/internal.py
index 56fbcbc0ea..bac24af119 100644
--- a/ipaserver/plugins/internal.py
+++ b/ipaserver/plugins/internal.py
@@ -593,6 +593,10 @@ class i18n_messages(Command):
             "host": {
                 "certificate": _("Host Certificate"),
                 "cn": _("Host Name"),
+                "cockpit_title": _("Cockpit"),
+                "cockpit_not_detected": _(
+                    "Cockpit has not been detected on ${host} host."
+                ),
                 "delete_key_unprovision": _("Delete Key, Unprovision"),
                 "details": _("Host Settings"),
                 "enrolled": _("Enrolled"),
