Browse Source

AMBARI-6162. Behavior change: host filtering no longer handles startsWith matches. (Buzhor Denys via onechiporenko)

Oleg Nechiporenko 11 năm trước cách đây
mục cha
commit
9bc9ed055f

+ 15 - 2
ambari-web/app/controllers/main/host.js

@@ -201,6 +201,18 @@ App.MainHostController = Em.ArrayController.extend({
     }
   ],
 
+  /**
+   * Validate and convert input string to valid url parameter.
+   * Detect if user have passed string as regular expression or extend
+   * string to regexp.
+   *
+   * @params {String} value
+   * @return {String}
+   **/
+  getRegExp: function (value) {
+    return validator.isValidMatchesRegexp(value) ? value.replace(/(\.+\*?|(\.\*)+)$/, '') + '.*' : '^$';
+  },
+
   /**
    * get query parameters computed from filter properties, sort properties and custom properties of view
    * @return {Array}
@@ -220,7 +232,6 @@ App.MainHostController = Em.ArrayController.extend({
         type: 'EQUAL'
       })
     }, this);
-
     savedFilterConditions.forEach(function (filter) {
       var property = filterProperties.findProperty('key', colPropAssoc[filter.iColumn]);
       if (property && filter.value.length > 0 && !filter.skipFilter) {
@@ -229,6 +240,9 @@ App.MainHostController = Em.ArrayController.extend({
           value: filter.value,
           type: property.type
         };
+        if (filter.type === 'string') {
+          result.value = this.getRegExp(filter.value);
+        }
         if (filter.type === 'number' || filter.type === 'ambari-bandwidth') {
           result.type = this.getComparisonType(filter.value);
           result.value = this.getProperValue(filter.value);
@@ -241,7 +255,6 @@ App.MainHostController = Em.ArrayController.extend({
         }
       }
     }, this);
-
     savedSortConditions.forEach(function (sort) {
       var property = sortProperties.findProperty('key', sort.name);
 

+ 12 - 1
ambari-web/app/utils/validator.js

@@ -143,5 +143,16 @@ module.exports = {
       default :
         return false;
     }
-}
+  },
+  /**
+   * Validate string that will pass as parameter to .matches() url param.
+   * For example: /api/v1/clusters/c1/hosts?Hosts/host_name.matches(.*localhost.)
+   *
+   * @params {String} value - string to validate
+   * @return {Boolean}
+   * @method isValidMatchesRegexp
+   */
+  isValidMatchesRegexp: function(value) {
+    return /^((\.\*?)?([\w]+)?)+(\.\*?)?$/g.test(value);
+  }
 };

+ 29 - 0
ambari-web/test/controllers/main/host_test.js

@@ -303,4 +303,33 @@ describe('MainHostController', function () {
 
   });
 
+  describe('#getRegExp()', function() {
+    before(function() {
+      hostController = App.MainHostController.create({});
+    });
+
+    var message = '`{0}` should convert to `{1}`',
+        tests = [
+      { value: '.*', expected: '.*' },
+      { value: '.', expected: '.*' },
+      { value: '.*.*', expected: '.*' },
+      { value: '*', expected: '^$' },
+      { value: '........', expected: '.*' },
+      { value: '........*', expected: '.*' },
+      { value: '........?', expected: '^$' },
+      { value: 'a1', expected: 'a1.*' },
+      { value: 'a1.', expected: 'a1.*' },
+      { value: 'a1...', expected: 'a1.*' },
+      { value: 'a1.*', expected: 'a1.*' },
+      { value: 'a1.*.a2.a3', expected: 'a1.*.a2.a3.*' },
+      { value: 'a1.*.a2...a3', expected: 'a1.*.a2...a3.*' }
+    ]
+
+    tests.forEach(function(test){
+      it(message.format(test.value, test.expected), function() {
+        expect(hostController.getRegExp(test.value)).to.be.equal(test.expected);
+      });
+    });
+  });
+
 });

+ 23 - 1
ambari-web/test/utils/validator_test.js

@@ -331,5 +331,27 @@ describe('validator', function () {
         expect(validator.isValidConfigKey(test.i)).to.equal(test.e);
       })
     });
-  })
+  });
+
+  describe('#isValidMatchesRegexp()', function() {
+    var message = '`{0}` should be {1}',
+        tests = [
+          { value: '.*', expected: true },
+          { value: '..', expected: true },
+          { value: '.a1', expected: true },
+          { value: '.*a1', expected: true },
+          { value: '.*a1.*', expected: true },
+          { value: '.*a1.a2', expected: true },
+          { value: '.*a1.*.a2', expected: true },
+          { value: '.*a1.*.a2.*.a3.a4.*.*', expected: true },
+          { value: '*', expected: false },
+          { value: '1>1', expected: false },
+          { value: '.*a1,*', expected: false }
+        ];
+    tests.forEach(function(test) {
+      it(message.format(test.value, (test.expected) ? 'valid' : 'not valid'), function() {
+        expect(validator.isValidMatchesRegexp(test.value)).to.equal(test.expected);
+      })
+    });
+  });
 });