The entitlement facet will invoke entitle_status to check the entitlement status and show the appropriate buttons. If it's unregistered it will show Register and Import button. If it's registered it will show the Consume button only. If it's imported it will show the Import button only. The Import button will open a dialog box for importing entitlement certificate.

--
Endi S. Dewata
From 85b436d33abafcd925db13860beaa5b2b86ac240 Mon Sep 17 00:00:00 2001
From: Endi S. Dewata <edew...@redhat.com>
Date: Mon, 11 Apr 2011 20:22:38 -0500
Subject: [PATCH] Entitlement import.

The entitlement facet will invoke entitle_status to check the entitlement
status and show the appropriate buttons. If it's unregistered it will show
Register and Import button. If it's registered it will show the Consume
button only. If it's imported it will show the Import button only. The
Import button will open a dialog box for importing entitlement certificate.
---
 install/ui/entitle.js                              |  174 ++++++++++++++++++--
 install/ui/ipa.css                                 |    4 +
 install/ui/test/data/entitle_import.json           |   12 ++
 ...e_get_unregistered.json => entitle_status.json} |    2 +-
 install/ui/test/data/entitle_status_imported.json  |   12 ++
 .../ui/test/data/entitle_status_registered.json    |   12 ++
 6 files changed, 204 insertions(+), 12 deletions(-)
 create mode 100644 install/ui/test/data/entitle_import.json
 rename install/ui/test/data/{entitle_get_unregistered.json => entitle_status.json} (99%)
 create mode 100644 install/ui/test/data/entitle_status_imported.json
 create mode 100644 install/ui/test/data/entitle_status_registered.json

diff --git a/install/ui/entitle.js b/install/ui/entitle.js
index 1853f38d4d84d583ba286cb9500e1529816fc2d4..ee556d18d05457b26c6f0fbee28a65fa0bce1936 100644
--- a/install/ui/entitle.js
+++ b/install/ui/entitle.js
@@ -27,6 +27,7 @@ IPA.entitle = {};
 
 IPA.entitle.unregistered = 0;
 IPA.entitle.registered = 1;
+IPA.entitle.imported = 2;
 
 IPA.entity_factories.entitle = function() {
 
@@ -87,6 +88,18 @@ IPA.entity_factories.entitle = function() {
             ]
         }).
         dialog({
+            factory: IPA.entitle.import_dialog,
+            name: 'import',
+            title: 'Import Certificate',
+            fields: [
+                {
+                    name: 'certificate',
+                    label: 'Certificate',
+                    undo: false
+                }
+            ]
+        }).
+        dialog({
             factory: IPA.entitle.consume_dialog,
             name: 'consume',
             title: 'Consume Entitlements',
@@ -108,18 +121,49 @@ IPA.entitle.entity = function(spec) {
 
     var that = IPA.entity(spec);
 
+    that.get_status = function(on_success, on_error) {
+
+        var name = 'entitle_status';
+
+        if (that.status == IPA.entitle.registered) {
+            name += '_registered';
+        } else if (that.status == IPA.entitle.imported) {
+            name += '_imported';
+        }
+
+        var command = IPA.command({
+            name: name,
+            method: 'entitle_status',
+            on_success: function(data, text_status, xhr) {
+                if (data.result.result.uuid == 'IMPORTED') {
+                    that.status = IPA.entitle.imported;
+                } else {
+                    that.status = IPA.entitle.registered;
+                }
+
+                if (on_success) {
+                    on_success.call(this, data, text_status, xhr);
+                }
+            },
+            on_error: function(xhr, text_status, error_thrown) {
+                that.status = IPA.entitle.unregistered;
+
+                if (on_error) {
+                    on_error.call(this, xhr, text_status, error_thrown);
+                }
+            },
+            retry: false
+        });
+
+        command.execute();
+    };
+
     that.get_certificates = function(on_success, on_error) {
 
         var command = IPA.command({
-            name: 'entitle_get' + (that.status == IPA.entitle.registered ? '' : '_unregistered'),
             entity: 'entitle',
             method: 'get',
-            on_success: function(data, text_status, xhr) {
-                that.status = IPA.entitle.registered;
-                if (on_success) {
-                    on_success.call(this, data, text_status, xhr);
-                }
-            },
+            on_success: on_success,
             on_error: on_error,
             retry: false
         });
@@ -146,6 +190,23 @@ IPA.entitle.entity = function(spec) {
         command.execute();
     };
 
+    that.import_certificate = function(certificate, on_success, on_error) {
+
+        var command = IPA.command({
+            method: 'entitle_import',
+            args: [ certificate ],
+            on_success: function(data, text_status, xhr) {
+                that.status = IPA.entitle.imported;
+                if (on_success) {
+                    on_success.call(this, data, text_status, xhr);
+                }
+            },
+            on_error: on_error
+        });
+
+        command.execute();
+    };
+
     that.consume = function(quantity, on_success, on_error) {
 
         var command = IPA.command({
@@ -186,6 +247,12 @@ IPA.entitle.search_facet = function(spec) {
 
         $('<input/>', {
             type: 'button',
+            name: 'import',
+            value: 'Import'
+        }).appendTo(buttons);
+
+        $('<input/>', {
+            type: 'button',
             name: 'consume',
             value: 'Consume'
         }).appendTo(buttons);
@@ -209,6 +276,19 @@ IPA.entitle.search_facet = function(spec) {
         that.register_button.css('display', 'none');
         button.replaceWith(that.register_button);
 
+        button = $('input[name=import]', action_panel);
+        that.import_button = IPA.action_button({
+            label: 'Import',
+            icon: 'ui-icon-plus',
+            style: 'display: none;',
+            click: function() {
+                var dialog = that.entity.get_dialog('import');
+                dialog.open(that.container);
+            }
+        });
+        that.import_button.css('display', 'none');
+        button.replaceWith(that.import_button);
+
         button = $('input[name=consume]', action_panel);
         that.consume_button = IPA.action_button({
             label: 'Consume',
@@ -228,7 +308,15 @@ IPA.entitle.search_facet = function(spec) {
         function on_success(data, text_status, xhr) {
 
             that.register_button.css('display', 'none');
-            that.consume_button.css('display', 'inline');
+            that.import_button.css('display', 'none');
+            that.consume_button.css('display', 'none');
+
+            if (that.entity.status == IPA.entitle.registered) {
+                that.consume_button.css('display', 'inline');
+
+            } else if (that.entity.status == IPA.entitle.imported) {
+                that.import_button.css('display', 'inline');
+            }
 
             that.table.empty();
 
@@ -251,15 +339,27 @@ IPA.entitle.search_facet = function(spec) {
         function on_error(xhr, text_status, error_thrown) {
 
             that.register_button.css('display', 'inline');
+            that.import_button.css('display', 'inline');
             that.consume_button.css('display', 'none');
 
             var summary = $('span[name=summary]', that.table.tfoot).empty();
             summary.append(error_thrown.message);
         }
 
-        that.entity.get_certificates(
-            on_success,
-            on_error);
+        if (typeof that.entity.status == 'undefined') {
+            that.entity.get_status(
+                function(data, text_status, xhr) {
+                    that.entity.get_certificates(
+                        on_success,
+                        on_error);
+                },
+                on_error);
+
+        } else {
+            that.entity.get_certificates(
+                on_success,
+                on_error);
+        }
     };
 
     return that;
@@ -293,6 +393,58 @@ IPA.entitle.register_dialog = function(spec) {
     return that;
 };
 
+IPA.entitle.import_dialog = function(spec) {
+
+    spec = spec || {};
+
+    var that = IPA.dialog(spec);
+
+    that.width = spec.width || 500;
+    that.height = spec.height || 400;
+
+    that.request = spec.request;
+
+    that.add_button('Import', function() {
+
+        var certificate = that.textarea.val();
+        certificate =
+            IPA.cert.BEGIN_CERTIFICATE+'\n'+
+            $.trim(certificate)+'\n'+
+            IPA.cert.END_CERTIFICATE+'\n';
+
+        that.entity.import_certificate(
+            certificate,
+            function() {
+                var facet = that.entity.get_facet('search');
+                facet.refresh();
+                that.close();
+            }
+        );
+    });
+
+    that.add_button(IPA.messages.buttons.cancel, function() {
+        that.close();
+    });
+
+    that.create = function() {
+        that.container.append('Enter the Base64-encoded certificate below:');
+        that.container.append('<br/>');
+        that.container.append('<br/>');
+
+        that.container.append(IPA.cert.BEGIN_CERTIFICATE);
+        that.container.append('<br/>');
+
+        that.textarea = $('<textarea/>', {
+            style: 'width: 100%; height: 225px;'
+        }).appendTo(that.container);
+
+        that.container.append('<br/>');
+        that.container.append(IPA.cert.END_CERTIFICATE);
+    };
+
+    return that;
+};
+
 IPA.entitle.consume_dialog = function(spec) {
 
     spec = spec || {};
diff --git a/install/ui/ipa.css b/install/ui/ipa.css
index 4fe040135a34b05c3eb4d5d98ba06a01a5253e35..b345967c902f90299f087e512d904a1c4dbc8ec5 100644
--- a/install/ui/ipa.css
+++ b/install/ui/ipa.css
@@ -475,6 +475,10 @@ span.ui-icon-search {
         margin-left: 1em !important;
 }
 
+[title="Import"] {
+        margin-left: 1em !important;
+}
+
 [title="Consume"] {
         margin-left: 1em !important;
 }
diff --git a/install/ui/test/data/entitle_import.json b/install/ui/test/data/entitle_import.json
new file mode 100644
index 0000000000000000000000000000000000000000..f6dc905b37bb6c9905c623234e5bb5d1ccd120c0
--- /dev/null
+++ b/install/ui/test/data/entitle_import.json
@@ -0,0 +1,12 @@
+{
+    "error": null,
+    "id": null,
+    "result": {
+        "result": {
+            "consumed": 1,
+            "product": "Multiplier Product Client Pack (50)",
+            "quantity": 1,
+            "uuid": "IMPORTED"
+        }
+    }
+}
diff --git a/install/ui/test/data/entitle_get_unregistered.json b/install/ui/test/data/entitle_status.json
similarity index 99%
rename from install/ui/test/data/entitle_get_unregistered.json
rename to install/ui/test/data/entitle_status.json
index 47dce1f18a08ee04b430cce43c7413024be44081..8a691ada2fc76cee77c69db21b7611afc8042407 100644
--- a/install/ui/test/data/entitle_get_unregistered.json
+++ b/install/ui/test/data/entitle_status.json
@@ -8,4 +8,4 @@
     },
     "id": 0,
     "result": null
-}
\ No newline at end of file
+}
diff --git a/install/ui/test/data/entitle_status_imported.json b/install/ui/test/data/entitle_status_imported.json
new file mode 100644
index 0000000000000000000000000000000000000000..cc2693a0e95a2ccf60d72d4d4ce6ccefc2da5713
--- /dev/null
+++ b/install/ui/test/data/entitle_status_imported.json
@@ -0,0 +1,12 @@
+{
+    "error": null,
+    "id": 0,
+    "result": {
+        "result": {
+            "consumed": 1,
+            "product": "Multiplier Product Client Pack (50)",
+            "quantity": 1,
+            "uuid": "IMPORTED"
+        }
+    }
+}
diff --git a/install/ui/test/data/entitle_status_registered.json b/install/ui/test/data/entitle_status_registered.json
new file mode 100644
index 0000000000000000000000000000000000000000..166ec14fc0433cfb5d0d8b75d784624e131e6d12
--- /dev/null
+++ b/install/ui/test/data/entitle_status_registered.json
@@ -0,0 +1,12 @@
+{
+    "error": null,
+    "id": 0,
+    "result": {
+        "result": {
+            "consumed": 30,
+            "product": "MKT-multiplier-client-50",
+            "quantity": 250,
+            "uuid": "a3865364-33cc-4ce3-b000-0f08498cc085"
+        }
+    }
+}
-- 
1.7.4

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to