Bladeren bron

AMBARI-15737: Host not expanding more than one field in host-name pattern (akovalenko)

Aleksandr Kovalenko 9 jaren geleden
bovenliggende
commit
fb8f34855f

+ 50 - 21
ambari-web/app/controllers/wizard/step2_controller.js

@@ -318,36 +318,65 @@ App.WizardStep2Controller = Em.Controller.extend({
    */
   parseHostNamesAsPatternExpression: function () {
     this.set('isPattern', false);
-    var self = this;
     var hostNames = [];
-    $.each(this.get('hostNameArr'), function (e, a) {
-      var start, end, extra = {0: ""};
-      if (/\[\d*\-\d*\]/.test(a)) {
-        start = a.match(/\[\d*/);
-        end = a.match(/\-\d*]/);
 
-        start = start[0].substr(1);
-        end = end[0].substr(1);
+    this.get('hostNameArr').forEach(function (a) {
+      var hn,
+          allPatterns = a.match(/\[\d*\-\d*\]/g),
+          patternsNumber = allPatterns ? allPatterns.length : 0;
+
+      if (patternsNumber) {
+        hn = [a];
+        for (var i = 0; i < patternsNumber; i++) {
+          hn = this._replacePatternInHosts(hn);
+        }
+        hostNames = hostNames.concat(hn);
+      } else {
+        hostNames.push(a);
+      }
+    }, this);
+
+    this.set('hostNameArr', hostNames.uniq());
+  },
+
+  /**
+   * return an array of results with pattern replacement for each host
+   * replace only first pattern in each host
+   * designed to be called recursively in <code>parseHostNamesAsPatternExpression</code>
+   *
+   * @param {Array} rawHostNames
+   * @private
+   * @return {Array}
+   */
+  _replacePatternInHosts: function (rawHostNames) {
+    var start, end, extra, allHostNames = [];
+    rawHostNames.forEach(function (rawHostName) {
+      var hostNames = [];
+      start = rawHostName.match(/\[\d*/);
+      end = rawHostName.match(/\-\d*]/);
+      extra = {0: ""};
 
-        if (parseInt(start) <= parseInt(end, 10) && parseInt(start, 10) >= 0) {
-          self.set('isPattern', true);
+      start = start[0].substr(1);
+      end = end[0].substr(1);
 
-          if (start[0] == "0" && start.length > 1) {
-            extra = start.match(/0*/);
-          }
+      if (parseInt(start) <= parseInt(end, 10) && parseInt(start, 10) >= 0) {
+        this.set('isPattern', true);
 
-          for (var i = parseInt(start, 10); i < parseInt(end, 10) + 1; i++) {
-            hostNames.push(a.replace(/\[\d*\-\d*\]/, extra[0].substring(0, start.length - i.toString().length) + i))
-          }
+        if (start[0] == "0" && start.length > 1) {
+          extra = start.match(/0*/);
+        }
 
-        } else {
-          hostNames.push(a);
+        for (var i = parseInt(start, 10); i < parseInt(end, 10) + 1; i++) {
+          hostNames.push(rawHostName.replace(/\[\d*\-\d*\]/, extra[0].substring(0, start.length - i.toString().length) + i))
         }
+
       } else {
-        hostNames.push(a);
+        hostNames.push(rawHostName);
       }
-    });
-    this.set('hostNameArr', hostNames.uniq());
+      allHostNames = allHostNames.concat(hostNames);
+    }, this);
+
+    return allHostNames;
   },
 
   /**

+ 20 - 0
ambari-web/test/controllers/wizard/step2_test.js

@@ -420,6 +420,26 @@ describe('App.WizardStep2Controller', function () {
       expect(result).to.equal(true);
     });
 
+    it('should parse hosts from multiple pattern expression to hostNameArr', function () {
+      var controller = App.WizardStep2Controller.create({
+        hostNameArr: ['test[1-2]host[01-05]']
+      });
+      controller.parseHostNamesAsPatternExpression();
+      var hosts = controller.get('hostNameArr');
+      expect(hosts).eql([
+          'test1host01',
+          'test1host02',
+          'test1host03',
+          'test1host04',
+          'test1host05',
+          'test2host01',
+          'test2host02',
+          'test2host03',
+          'test2host04',
+          'test2host05'
+      ]);
+    });
+
     it('should skip duplicates', function () {
       var controller = App.WizardStep2Controller.create({
         hostNameArr: ['host[1-3]', 'host2']