ui: utils: add function for parsing maintenance mode

...since the same code is used is more than one place

Signed-off-by: Hannes Laimer <h.laimer@proxmox.com>
This commit is contained in:
Hannes Laimer 2022-04-26 06:23:34 +00:00 committed by Thomas Lamprecht
parent fbd6f54f39
commit 1b7479c968
3 changed files with 17 additions and 15 deletions

View File

@ -640,13 +640,22 @@ Ext.define('PBS.Utils', {
return `${icon} ${value}`; return `${icon} ${value}`;
}, },
// FIXME: this "parser" is brittle and relies on the order the arguments will appear in
parseMaintenanceMode: function(mode) {
let [type, message] = mode.split(/,(.+)/);
type = type.split("=").pop();
message = message ? message.split("=")[1]
.replace(/^"(.*)"$/, '$1')
.replaceAll('\\"', '"') : null;
return [type, message];
},
renderMaintenance: function(mode, activeTasks) { renderMaintenance: function(mode, activeTasks) {
if (!mode) { if (!mode) {
return gettext('None'); return gettext('None');
} }
// FIXME: this "parser" is brittle and relies on the order the arguments will appear in
let [type, message] = mode.split(","); let [type, message] = PBS.Utils.parseMaintenanceMode(mode);
type = type.split("=").pop();
const conflictingTasks = activeTasks.write + (type === 'offline' ? activeTasks.read : 0); const conflictingTasks = activeTasks.write + (type === 'offline' ? activeTasks.read : 0);
@ -659,7 +668,7 @@ Ext.define('PBS.Utils', {
} }
if (message) { if (message) {
extra += ` (${message.split("=").pop()})`; extra += ` ("${message}")`;
} }
let modeText = Proxmox.Utils.unknownText; let modeText = Proxmox.Utils.unknownText;

View File

@ -49,12 +49,9 @@ Ext.define('PBS.DataStoreInfo', {
success: function(response) { success: function(response) {
const config = response.result.data; const config = response.result.data;
if (config['maintenance-mode']) { if (config['maintenance-mode']) {
const [_type, msg] = config['maintenance-mode'].split(/,(.+)/); const [_type, msg] = PBS.Utils.parseMaintenanceMode(config['maintenance-mode']);
const message = msg ? ': ' + msg.split("=")[1]
.replace(/^"(.*)"$/, '$1')
.replaceAll('\\"', '"') : '';
me.view.el.mask( me.view.el.mask(
`${gettext('Datastore is in maintenance mode')}${message}`, `${gettext('Datastore is in maintenance mode')}${msg ? ': ' + msg : ''}`,
'fa pbs-maintenance-mask', 'fa pbs-maintenance-mask',
); );
} else { } else {

View File

@ -62,14 +62,10 @@ Ext.define('PBS.window.MaintenanceOptions', {
'maintenance-msg': '', 'maintenance-msg': '',
}; };
if (values['maintenance-mode']) { if (values['maintenance-mode']) {
let [type, message] = values['maintenance-mode'].split(/,(.+)/); const [type, message] = PBS.Utils.parseMaintenanceMode(values['maintenance-mode']);
type = type.split("=").pop();
message = message ? message.split("=")[1]
.replace(/^"(.*)"$/, '$1')
.replaceAll('\\"', '"') : '';
options = { options = {
'maintenance-type': type, 'maintenance-type': type,
'maintenance-msg': message, 'maintenance-msg': message ?? '',
}; };
} }