From f0e5ad4231ff62d24bf5ec802d4da98034daa0b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 5 Jun 2015 13:53:44 +0200 Subject: [PATCH] Feature #3748: Quotas dialog for users and groups --- src/sunstone/public/app/tabs/groups-tab.js | 1 + .../public/app/tabs/groups-tab/actions.js | 45 +++++-- .../app/tabs/groups-tab/dialogs/quotas.js | 78 ++++++++++++ .../groups-tab/dialogs/quotas/dialogId.js | 3 + .../tabs/groups-tab/dialogs/quotas/html.hbs | 18 +++ src/sunstone/public/app/tabs/users-tab.js | 3 +- .../public/app/tabs/users-tab/actions.js | 36 ++++-- .../app/tabs/users-tab/dialogs/quotas.js | 78 ++++++++++++ .../tabs/users-tab/dialogs/quotas/dialogId.js | 3 + .../tabs/users-tab/dialogs/quotas/html.hbs | 18 +++ .../public/app/utils/quotas/quota-widgets.js | 120 +++++++++++++----- 11 files changed, 344 insertions(+), 59 deletions(-) create mode 100644 src/sunstone/public/app/tabs/groups-tab/dialogs/quotas.js create mode 100644 src/sunstone/public/app/tabs/groups-tab/dialogs/quotas/dialogId.js create mode 100644 src/sunstone/public/app/tabs/groups-tab/dialogs/quotas/html.hbs create mode 100644 src/sunstone/public/app/tabs/users-tab/dialogs/quotas.js create mode 100644 src/sunstone/public/app/tabs/users-tab/dialogs/quotas/dialogId.js create mode 100644 src/sunstone/public/app/tabs/users-tab/dialogs/quotas/html.hbs diff --git a/src/sunstone/public/app/tabs/groups-tab.js b/src/sunstone/public/app/tabs/groups-tab.js index 0483df367a..f63ed02996 100644 --- a/src/sunstone/public/app/tabs/groups-tab.js +++ b/src/sunstone/public/app/tabs/groups-tab.js @@ -8,6 +8,7 @@ define(function(require) { var DATATABLE_ID = "dataTableGroups"; var _dialogs = [ + require('./groups-tab/dialogs/quotas') ]; var _panels = [ diff --git a/src/sunstone/public/app/tabs/groups-tab/actions.js b/src/sunstone/public/app/tabs/groups-tab/actions.js index 7419da2388..6c17b4eee8 100644 --- a/src/sunstone/public/app/tabs/groups-tab/actions.js +++ b/src/sunstone/public/app/tabs/groups-tab/actions.js @@ -6,8 +6,10 @@ define(function(require) { var OpenNebulaResource = require('opennebula/group'); var RESOURCE = "Group"; + var XML_ROOT = "GROUP"; var TAB_ID = require('./tabId'); var CREATE_DIALOG_ID = require('./form-panels/create/formPanelId'); + var QUOTAS_DIALOG_ID = require('./dialogs/quotas/dialogId'); var _actions = { "Group.create" : { @@ -106,25 +108,38 @@ define(function(require) { error: Notifier.onError, }, - /* TODO "Group.fetch_quotas" : { - type: "single", - call: OpenNebula.Group.show, - callback: function (request,response) { - populateQuotasDialog( - response.GROUP, - default_group_quotas, - "#group_quotas_dialog", - $group_quotas_dialog); - }, - error: onError + type: "single", + call: OpenNebulaResource.show, + callback: function (request,response) { + Sunstone.getDialog(QUOTAS_DIALOG_ID).setParams({element: response[XML_ROOT]}); + Sunstone.getDialog(QUOTAS_DIALOG_ID).reset(); + Sunstone.getDialog(QUOTAS_DIALOG_ID).show(); + }, + error: Notifier.onError }, "Group.quotas_dialog" : { - type: "custom", - call: popUpGroupQuotasDialog + type: "custom", + call: function() { + var tab = $('#' + TAB_ID); + if (Sunstone.rightInfoVisible(tab)) { + $('a[href="#group_quotas_tab"]', tab).click(); + $('#edit_quotas_button', tab).click(); + } else { + var sel_elems = Sunstone.getDataTable(TAB_ID).elements(); + //If only one group is selected we fecth the group's quotas + if (sel_elems.length == 1){ + Sunstone.runAction(RESOURCE+'.fetch_quotas',sel_elems[0]); + } else { + // More than one, shows '0' usage + Sunstone.getDialog(QUOTAS_DIALOG_ID).setParams({element: {}}); + Sunstone.getDialog(QUOTAS_DIALOG_ID).reset(); + Sunstone.getDialog(QUOTAS_DIALOG_ID).show(); + } + } + } }, - */ "Group.set_quota" : { type: "multiple", @@ -133,6 +148,8 @@ define(function(require) { return Sunstone.getDataTable(TAB_ID).elements(); }, callback: function(request) { + Sunstone.getDialog(QUOTAS_DIALOG_ID).hide(); + Sunstone.runAction(RESOURCE+'.show',request.request.data[0]); }, error: Notifier.onError diff --git a/src/sunstone/public/app/tabs/groups-tab/dialogs/quotas.js b/src/sunstone/public/app/tabs/groups-tab/dialogs/quotas.js new file mode 100644 index 0000000000..1d20983b4b --- /dev/null +++ b/src/sunstone/public/app/tabs/groups-tab/dialogs/quotas.js @@ -0,0 +1,78 @@ +define(function(require) { + /* + DEPENDENCIES + */ + + var BaseDialog = require('utils/dialogs/dialog'); + var TemplateHTML = require('hbs!./quotas/html'); + var Locale = require('utils/locale'); + var QuotaDefaults = require('utils/quotas/quota-defaults'); + var QuotaWidgets = require('utils/quotas/quota-widgets'); + + /* + CONSTANTS + */ + + var DIALOG_ID = require('./quotas/dialogId'); + var TAB_ID = require('../tabId'); + var RESOURCE = "Group"; + var XML_ROOT = "GROUP"; + + /* + CONSTRUCTOR + */ + + function Dialog() { + this.dialogId = DIALOG_ID; + + this.element = undefined; + + BaseDialog.call(this); + } + + Dialog.DIALOG_ID = DIALOG_ID; + Dialog.prototype = Object.create(BaseDialog.prototype); + Dialog.prototype.constructor = Dialog; + Dialog.prototype.html = _html; + Dialog.prototype.onShow = _onShow; + Dialog.prototype.setup = _setup; + Dialog.prototype.setParams = _setParams; + + return Dialog; + + /* + FUNCTION DEFINITIONS + */ + + function _html() { + return TemplateHTML({ + 'dialogId': this.dialogId, + 'quotasHTML': QuotaWidgets.dialogHTML() + }); + } + + function _setup(context) { + var that = this; + + QuotaWidgets.setupQuotasDialog(context); + + return false; + } + + /** + * @param {object} params + * - params.element : group object, or empty object {} + */ + function _setParams(params) { + this.element = params.element; + } + + function _onShow(context) { + QuotaWidgets.populateQuotasDialog( + this.element, + QuotaDefaults.getDefaultQuotas(RESOURCE), + context); + + return false; + } +}); diff --git a/src/sunstone/public/app/tabs/groups-tab/dialogs/quotas/dialogId.js b/src/sunstone/public/app/tabs/groups-tab/dialogs/quotas/dialogId.js new file mode 100644 index 0000000000..5aef053778 --- /dev/null +++ b/src/sunstone/public/app/tabs/groups-tab/dialogs/quotas/dialogId.js @@ -0,0 +1,3 @@ +define(function(require){ + return 'groupQuotasDialog'; +}); \ No newline at end of file diff --git a/src/sunstone/public/app/tabs/groups-tab/dialogs/quotas/html.hbs b/src/sunstone/public/app/tabs/groups-tab/dialogs/quotas/html.hbs new file mode 100644 index 0000000000..26843ba4c9 --- /dev/null +++ b/src/sunstone/public/app/tabs/groups-tab/dialogs/quotas/html.hbs @@ -0,0 +1,18 @@ + \ No newline at end of file diff --git a/src/sunstone/public/app/tabs/users-tab.js b/src/sunstone/public/app/tabs/users-tab.js index bb27393049..f5fab9cae6 100644 --- a/src/sunstone/public/app/tabs/users-tab.js +++ b/src/sunstone/public/app/tabs/users-tab.js @@ -9,7 +9,8 @@ define(function(require) { var _dialogs = [ require('./users-tab/dialogs/password'), - require('./users-tab/dialogs/auth-driver') + require('./users-tab/dialogs/auth-driver'), + require('./users-tab/dialogs/quotas') ]; var _panels = [ diff --git a/src/sunstone/public/app/tabs/users-tab/actions.js b/src/sunstone/public/app/tabs/users-tab/actions.js index 8667a28072..d8fe5e9686 100644 --- a/src/sunstone/public/app/tabs/users-tab/actions.js +++ b/src/sunstone/public/app/tabs/users-tab/actions.js @@ -6,10 +6,12 @@ define(function(require) { var OpenNebulaResource = require('opennebula/user'); var RESOURCE = "User"; + var XML_ROOT = "USER"; var TAB_ID = require('./tabId'); var CREATE_DIALOG_ID = require('./form-panels/create/formPanelId'); var PASSWORD_DIALOG_ID = require('./dialogs/password/dialogId'); var AUTH_DRIVER_DIALOG_ID = require('./dialogs/auth-driver/dialogId'); + var QUOTAS_DIALOG_ID = require('./dialogs/quotas/dialogId'); var _actions = { "User.create" : { @@ -152,26 +154,38 @@ define(function(require) { error: Notifier.onError }, - /* TODO - "User.fetch_quotas" : { type: "single", call: OpenNebulaResource.show, callback: function (request,response) { - populateQuotasDialog( - response.USER, - default_user_quotas, - "#user_quotas_dialog", - $user_quotas_dialog); + Sunstone.getDialog(QUOTAS_DIALOG_ID).setParams({element: response[XML_ROOT]}); + Sunstone.getDialog(QUOTAS_DIALOG_ID).reset(); + Sunstone.getDialog(QUOTAS_DIALOG_ID).show(); }, - error: onError + error: Notifier.onError }, "User.quotas_dialog" : { type: "custom", - call: popUpUserQuotasDialog + call: function() { + var tab = $('#' + TAB_ID); + if (Sunstone.rightInfoVisible(tab)) { + $('a[href="#user_quotas_tab"]', tab).click(); + $('#edit_quotas_button', tab).click(); + } else { + var sel_elems = Sunstone.getDataTable(TAB_ID).elements(); + //If only one user is selected we fecth the user's quotas + if (sel_elems.length == 1){ + Sunstone.runAction(RESOURCE+'.fetch_quotas',sel_elems[0]); + } else { + // More than one, shows '0' usage + Sunstone.getDialog(QUOTAS_DIALOG_ID).setParams({element: {}}); + Sunstone.getDialog(QUOTAS_DIALOG_ID).reset(); + Sunstone.getDialog(QUOTAS_DIALOG_ID).show(); + } + } + } }, - */ "User.set_quota" : { type: "multiple", @@ -180,6 +194,8 @@ define(function(require) { return Sunstone.getDataTable(TAB_ID).elements(); }, callback: function(request) { + Sunstone.getDialog(QUOTAS_DIALOG_ID).hide(); + Sunstone.runAction(RESOURCE+'.show',request.request.data[0]); }, error: Notifier.onError diff --git a/src/sunstone/public/app/tabs/users-tab/dialogs/quotas.js b/src/sunstone/public/app/tabs/users-tab/dialogs/quotas.js new file mode 100644 index 0000000000..0a944f1fc7 --- /dev/null +++ b/src/sunstone/public/app/tabs/users-tab/dialogs/quotas.js @@ -0,0 +1,78 @@ +define(function(require) { + /* + DEPENDENCIES + */ + + var BaseDialog = require('utils/dialogs/dialog'); + var TemplateHTML = require('hbs!./quotas/html'); + var Locale = require('utils/locale'); + var QuotaDefaults = require('utils/quotas/quota-defaults'); + var QuotaWidgets = require('utils/quotas/quota-widgets'); + + /* + CONSTANTS + */ + + var DIALOG_ID = require('./quotas/dialogId'); + var TAB_ID = require('../tabId'); + var RESOURCE = "User"; + var XML_ROOT = "USER"; + + /* + CONSTRUCTOR + */ + + function Dialog() { + this.dialogId = DIALOG_ID; + + this.element = undefined; + + BaseDialog.call(this); + } + + Dialog.DIALOG_ID = DIALOG_ID; + Dialog.prototype = Object.create(BaseDialog.prototype); + Dialog.prototype.constructor = Dialog; + Dialog.prototype.html = _html; + Dialog.prototype.onShow = _onShow; + Dialog.prototype.setup = _setup; + Dialog.prototype.setParams = _setParams; + + return Dialog; + + /* + FUNCTION DEFINITIONS + */ + + function _html() { + return TemplateHTML({ + 'dialogId': this.dialogId, + 'quotasHTML': QuotaWidgets.dialogHTML() + }); + } + + function _setup(context) { + var that = this; + + QuotaWidgets.setupQuotasDialog(context); + + return false; + } + + /** + * @param {object} params + * - params.element : user object, or empty object {} + */ + function _setParams(params) { + this.element = params.element; + } + + function _onShow(context) { + QuotaWidgets.populateQuotasDialog( + this.element, + QuotaDefaults.getDefaultQuotas(RESOURCE), + context); + + return false; + } +}); diff --git a/src/sunstone/public/app/tabs/users-tab/dialogs/quotas/dialogId.js b/src/sunstone/public/app/tabs/users-tab/dialogs/quotas/dialogId.js new file mode 100644 index 0000000000..61ae4a872c --- /dev/null +++ b/src/sunstone/public/app/tabs/users-tab/dialogs/quotas/dialogId.js @@ -0,0 +1,3 @@ +define(function(require){ + return 'userQuotasDialog'; +}); \ No newline at end of file diff --git a/src/sunstone/public/app/tabs/users-tab/dialogs/quotas/html.hbs b/src/sunstone/public/app/tabs/users-tab/dialogs/quotas/html.hbs new file mode 100644 index 0000000000..04bda836dc --- /dev/null +++ b/src/sunstone/public/app/tabs/users-tab/dialogs/quotas/html.hbs @@ -0,0 +1,18 @@ + \ No newline at end of file diff --git a/src/sunstone/public/app/utils/quotas/quota-widgets.js b/src/sunstone/public/app/utils/quotas/quota-widgets.js index c697d40e7f..9d54c9ae29 100644 --- a/src/sunstone/public/app/utils/quotas/quota-widgets.js +++ b/src/sunstone/public/app/utils/quotas/quota-widgets.js @@ -1035,13 +1035,13 @@ define(function(require) { function _quotaBar(usage, limit, default_limit){ var int_usage = parseInt(usage, 10); - var int_limit = __quotaIntLimit(limit, default_limit); + var int_limit = _quotaIntLimit(limit, default_limit); return ProgressBar.html(int_usage, int_limit, null); } function _quotaBarMB(usage, limit, default_limit){ var int_usage = parseInt(usage, 10); - var int_limit = __quotaIntLimit(limit, default_limit); + var int_limit = _quotaIntLimit(limit, default_limit); info_str = Humanize.size(int_usage * 1024)+' / ' + ((int_limit >= 0) ? Humanize.size(int_limit * 1024) : '-'); @@ -1051,40 +1051,10 @@ define(function(require) { function _quotaBarFloat(usage, limit, default_limit){ var float_usage = parseFloat(usage, 10); - var float_limit = __quotaFloatLimit(limit, default_limit); + var float_limit = _quotaFloatLimit(limit, default_limit); return ProgressBar.html(float_usage, float_limit, null); } - function __quotaIntLimit(limit, default_limit){ - i_limit = parseInt(limit, 10); - i_default_limit = parseInt(default_limit, 10); - - if (limit == QUOTA_LIMIT_DEFAULT){ - i_limit = i_default_limit; - } - - if (isNaN(i_limit)){ - i_limit = 0; - } - - return i_limit; - } - - function __quotaFloatLimit(limit, default_limit){ - f_limit = parseFloat(limit, 10); - f_default_limit = parseFloat(default_limit, 10); - - if (f_limit == parseFloat(QUOTA_LIMIT_DEFAULT, 10)){ - f_limit = f_default_limit; - } - - if (isNaN(f_limit)){ - f_limit = 0; - } - - return f_limit; - } - function _quotaIntLimit(limit, default_limit){ i_limit = parseInt(limit, 10); i_default_limit = parseInt(default_limit, 10); @@ -1117,12 +1087,94 @@ define(function(require) { return f_limit } + //============================================================================ + // Quotas dialog + //============================================================================ + + function _quotas_tmpl(){ + return '
\ + \ +
\ +
\ +
\ +
\ +
\ +
\ +
\ +
\ +
\ +
\ +
\ +
\ +
'; + } + + /** + * Sets up a dialog to edit and update user and group quotas + */ + function _setupQuotasDialog(context){ + $('form', context).submit(function(){ + var obj = retrieveQuotasValues(context); + var action = $('div.form_buttons button',this).val(); + var sel_elems = Sunstone.getAction(action).elements(); + Sunstone.runAction(action,sel_elems,obj); + + return false; + }); + } + + function _populateQuotasDialog(resource_info, default_quotas, context){ + var vms_quota = _vmsWidget(resource_info, default_quotas); + var cpu_quota = _cpuWidget(resource_info, default_quotas); + var memory_quota = _memoryWidget(resource_info, default_quotas); + var volatile_size_quota = _volatileWidget(resource_info, default_quotas); + + var image_quota = _imageWidget(resource_info, default_quotas); + var network_quota = _networkWidget(resource_info, default_quotas); + var datastore_quota = _datastoreWidget(resource_info, default_quotas); + + $("#vm_quota", context).html( + '
' + vms_quota + '
\ +
' + cpu_quota + '
\ +
' + memory_quota + '
\ +
' + volatile_size_quota+ '
'); + + $("#datastore_quota", context).html( + '
' + datastore_quota + '
'); + + $("#image_quota", context).html( + '
' + image_quota + '
'); + + $("#network_quota", context).html( + '
' + network_quota + '
'); + + setupQuotasBarButtons(resource_info, context); + + _setupImageWidget(context, default_quotas); + _setupNetworkWidget(context, default_quotas); + _setupDatastoreWidget(context, default_quotas); + + quotasPanelEditAction(context); + + context.foundation(); + } + return { 'initEmptyQuotas': _initEmptyQuotas, 'quotaBar': _quotaBar, 'quotaBarMB': _quotaBarMB, 'quotaBarFloat': _quotaBarFloat, 'initQuotasPanel': _initQuotasPanel, - 'setupQuotasPanel': _setupQuotasPanel + 'setupQuotasPanel': _setupQuotasPanel, + 'dialogHTML': _quotas_tmpl, + 'setupQuotasDialog': _setupQuotasDialog, + 'populateQuotasDialog': _populateQuotasDialog, }; });