[pve-devel] [PATCH manager v3 2/2] add HostList validator and check monhosts with it

Dominik Csapak d.csapak at proxmox.com
Tue Jul 12 14:41:28 CEST 2016


this adds a vtype which splits the given string into a list by
; or , or space
and checks if it is a valid (hostname|ip) port notation

also make the rbd monhost input field use it

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
changes since v2:
* make full tests for the host/ip port notations
  modeled after the sub 'parse_host_and_port' from pve-common
 www/manager6/Toolkit.js         | 24 ++++++++++++++++++++++--
 www/manager6/Utils.js           |  4 ++++
 www/manager6/storage/RBDEdit.js |  1 +
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/www/manager6/Toolkit.js b/www/manager6/Toolkit.js
index decaab6..7bef6f2 100644
--- a/www/manager6/Toolkit.js
+++ b/www/manager6/Toolkit.js
@@ -1,4 +1,5 @@
-/*global IP4_match, IP4_cidr_match, IP6_match, IP6_cidr_match, IP64_match, DnsName_match*/
+/*global IP4_match, IP4_cidr_match, IP6_match, IP6_cidr_match, IP64_match, DnsName_match, DnsName_REGEXP, IPV4_REGEXP, IPV6_REGEXP*/
+/*global HostPort_match, HostPortBrackets_match, IP6_dotnotation_match*/
 // ExtJS related things
 
 PVE.Utils.toolkit = 'extjs';
@@ -101,7 +102,26 @@ Ext.apply(Ext.form.field.VTypes, {
     pveMail: function(v) {
         return (/^(\w+)([\-+.][\w]+)*@(\w[\-\w]*\.){1,5}([A-Za-z]){2,63}$/).test(v);
     },
-    pveMailText: gettext('Example') + ": user at example.com"
+    pveMailText: gettext('Example') + ": user at example.com",
+
+    HostList: function(v) {
+	var list = v.split(/[\ \,\;]+/);
+	var i;
+	for (i = 0; i < list.length; i++) {
+	    if (list[i] == "") {
+		continue;
+	    }
+
+	    if (!HostPort_match.test(list[i]) &&
+		!HostPortBrackets_match.test(list[i]) &&
+		!IP6_dotnotation_match.test(list[i])) {
+		return false;
+	    }
+	}
+
+	return true;
+    },
+    HostListText: gettext('Not a valid list of hosts')
 });
 
 // ExtJs 5-6 has an issue with caching
diff --git a/www/manager6/Utils.js b/www/manager6/Utils.js
index 7ed2560..0182413 100644
--- a/www/manager6/Utils.js
+++ b/www/manager6/Utils.js
@@ -59,6 +59,10 @@ var IP64_match = new RegExp("^(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + ")$");
 var DnsName_REGEXP = "(?:(([a-zA-Z0-9]([a-zA-Z0-9\\-]*[a-zA-Z0-9])?)\\.)*([A-Za-z0-9]([A-Za-z0-9\\-]*[A-Za-z0-9])?))";
 var DnsName_match = new RegExp("^" + DnsName_REGEXP + "$");
 
+var HostPort_match = new RegExp("^(" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")(:\\d+)?$");
+var HostPortBrackets_match = new RegExp("^\\[(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")\\](:\\d+)?$");
+var IP6_dotnotation_match = new RegExp("^" + IPV6_REGEXP + "(\\.\\d+)?$");
+
 Ext.define('PVE.Utils', { statics: {
 
     // this class only contains static functions
diff --git a/www/manager6/storage/RBDEdit.js b/www/manager6/storage/RBDEdit.js
index a38ea52..a046ec3 100644
--- a/www/manager6/storage/RBDEdit.js
+++ b/www/manager6/storage/RBDEdit.js
@@ -39,6 +39,7 @@ Ext.define('PVE.storage.RBDInputPanel', {
 	    {
 		xtype: me.create ? 'textfield' : 'displayfield',
 		name: 'monhost',
+		vtype: 'HostList',
 		value: '',
 		fieldLabel: gettext('Monitor Host'),
 		allowBlank: false
-- 
2.1.4





More information about the pve-devel mailing list