github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/ui/javascripts/app/controllers.js (about)

     1  App.ApplicationController = Ember.ObjectController.extend({
     2    updateCurrentPath: function() {
     3      App.set('currentPath', this.get('currentPath'));
     4    }.observes('currentPath')
     5  });
     6  
     7  App.DcController = Ember.Controller.extend({
     8    needs: ["application"],
     9    // Whether or not the dropdown menu can be seen
    10    isDropdownVisible: false,
    11    isNotificationVisible: true,
    12  
    13    datacenter: Ember.computed.alias('content'),
    14  
    15    // Returns the total number of failing checks.
    16    //
    17    // We treat any non-passing checks as failing
    18    //
    19    totalChecksFailing: function() {
    20      return this.get('nodes').reduce(function(sum, node) {
    21        return sum + node.get('failingChecks');
    22      }, 0);
    23    }.property('nodes'),
    24  
    25    totalChecksPassing: function() {
    26      return this.get('nodes').reduce(function(sum, node) {
    27        return sum + node.get('passingChecks');
    28      }, 0);
    29    }.property('nodes'),
    30  
    31    //
    32    // Returns the human formatted message for the button state
    33    //
    34    checkMessage: function() {
    35      var failingChecks = this.get('totalChecksFailing');
    36      var passingChecks = this.get('totalChecksPassing');
    37  
    38      if (this.get('hasFailingChecks') === true) {
    39        return  failingChecks + ' failing';
    40      } else {
    41        return  passingChecks + ' passing';
    42      }
    43  
    44    }.property('nodes'),
    45  
    46    //
    47    //
    48    //
    49    checkStatus: function() {
    50      if (this.get('hasFailingChecks') === true) {
    51        return "failing";
    52      } else {
    53        return "passing";
    54      }
    55  
    56    }.property('nodes'),
    57  
    58    //
    59    // Boolean if the datacenter has any failing checks.
    60    //
    61    hasFailingChecks: Ember.computed.gt('totalChecksFailing', 0),
    62    
    63    init: function() {
    64      if(App.get('settings.v1-notification-hidden', true)) {
    65        this.set('isNotificationVisible', false);
    66      }
    67    },
    68    actions: {
    69      // Hide and show the dropdown menu
    70      toggle: function(item){
    71        this.toggleProperty('isDropdownVisible');
    72      },
    73      hideNotification: function(e) {
    74        App.set('settings.v1-notification-hidden', true);
    75        this.set('isNotificationVisible', false);
    76      },
    77      // Just hide the dropdown menu
    78      hideDrop: function(item){
    79        this.set('isDropdownVisible', false);
    80      }
    81    }
    82  });
    83  
    84  KvBaseController = Ember.ObjectController.extend({
    85    getParentKeyRoute: function() {
    86      if (this.get('isRoot')) {
    87        return this.get('rootKey');
    88      }
    89      return this.get('parentKey');
    90    },
    91  
    92    transitionToNearestParent: function(parent) {
    93      var controller = this;
    94      var rootKey = controller.get('rootKey');
    95      var dc = controller.get('dc').get('datacenter');
    96      var token = App.get('settings.token');
    97  
    98      Ember.$.ajax({
    99        url: (formatUrl(consulHost + '/v1/kv/' + parent + '?keys', dc, token)),
   100        type: 'GET'
   101      }).then(function(data) {
   102        controller.transitionToRoute('kv.show', parent);
   103      }).fail(function(response) {
   104        if (response.status === 404) {
   105          controller.transitionToRoute('kv.show', rootKey);
   106        }
   107      });
   108  
   109      controller.set('isLoading', false);
   110    }
   111  });
   112  
   113  App.KvShowController = KvBaseController.extend(Ember.Validations.Mixin, {
   114    needs: ["dc"],
   115    dc: Ember.computed.alias("controllers.dc"),
   116    isLoading: false,
   117  
   118    actions: {
   119      // Creates the key from the newKey model
   120      // set on the route.
   121      createKey: function() {
   122        this.set('isLoading', true);
   123  
   124        var controller = this;
   125        var newKey = controller.get('newKey');
   126        var parentKey = controller.get('parentKey');
   127        var grandParentKey = controller.get('grandParentKey');
   128        var dc = controller.get('dc').get('datacenter');
   129        var token = App.get('settings.token');
   130  
   131        // If we don't have a previous model to base
   132        // on our parent, or we're not at the root level,
   133        // add the prefix
   134        if (parentKey !== undefined && parentKey !== "/") {
   135          newKey.set('Key', (parentKey + newKey.get('Key')));
   136        }
   137  
   138        // Put the Key and the Value retrieved from the form
   139        Ember.$.ajax({
   140            url: (formatUrl(consulHost + "/v1/kv/" + newKey.get('Key'), dc, token)),
   141            type: 'PUT',
   142            data: newKey.get('Value')
   143        }).then(function(response) {
   144          // transition to the right place
   145          if (newKey.get('isFolder') === true) {
   146            controller.transitionToRoute('kv.show', newKey.get('Key'));
   147          } else {
   148            controller.transitionToRoute('kv.edit', newKey.get('Key'));
   149          }
   150          controller.set('isLoading', false);
   151        }).fail(function(response) {
   152          // Render the error message on the form if the request failed
   153          controller.set('errorMessage', 'Received error while processing: ' + response.statusText);
   154        });
   155      },
   156  
   157      deleteFolder: function() {
   158  
   159        this.set('isLoading', true);
   160        var controller = this;
   161        var dc = controller.get('dc').get('datacenter');
   162        var grandParent = controller.get('grandParentKey');
   163        var token = App.get('settings.token');
   164  
   165        if (window.confirm("Are you sure you want to delete this folder?")) {
   166          // Delete the folder
   167          Ember.$.ajax({
   168              url: (formatUrl(consulHost + "/v1/kv/" + controller.get('parentKey') + '?recurse', dc, token)),
   169              type: 'DELETE'
   170          }).then(function(response) {
   171            controller.transitionToNearestParent(grandParent);
   172          }).fail(function(response) {
   173            // Render the error message on the form if the request failed
   174            controller.set('errorMessage', 'Received error while processing: ' + response.statusText);
   175          });
   176        }
   177      }
   178    }
   179  });
   180  
   181  App.KvEditController = KvBaseController.extend({
   182    isLoading: false,
   183    needs: ["dc"],
   184    dc: Ember.computed.alias("controllers.dc"),
   185  
   186    actions: {
   187      // Updates the key set as the model on the route.
   188      updateKey: function() {
   189        this.set('isLoading', true);
   190  
   191        var dc = this.get('dc').get('datacenter');
   192        var key = this.get("model");
   193        var controller = this;
   194        var token = App.get('settings.token');
   195  
   196        // Put the key and the decoded (plain text) value
   197        // from the form.
   198        Ember.$.ajax({
   199            url: (formatUrl(consulHost + "/v1/kv/" + key.get('Key'), dc, token)),
   200            type: 'PUT',
   201            data: key.get('valueDecoded')
   202        }).then(function(response) {
   203          // If success, just reset the loading state.
   204          controller.set('isLoading', false);
   205        }).fail(function(response) {
   206          // Render the error message on the form if the request failed
   207          controller.set('errorMessage', 'Received error while processing: ' + response.statusText);
   208        });
   209      },
   210  
   211      cancelEdit: function() {
   212        this.set('isLoading', true);
   213        this.transitionToRoute('kv.show', this.getParentKeyRoute());
   214        this.set('isLoading', false);
   215      },
   216  
   217      deleteKey: function() {
   218        this.set('isLoading', true);
   219  
   220        var controller = this;
   221        var dc = controller.get('dc').get('datacenter');
   222        var key = controller.get("model");
   223        var parent = controller.getParentKeyRoute();
   224        var token = App.get('settings.token');
   225  
   226        // Delete the key
   227        Ember.$.ajax({
   228            url: (formatUrl(consulHost + "/v1/kv/" + key.get('Key'), dc, token)),
   229            type: 'DELETE'
   230        }).then(function(data) {
   231          controller.transitionToNearestParent(parent);
   232        }).fail(function(response) {
   233          // Render the error message on the form if the request failed
   234          controller.set('errorMessage', 'Received error while processing: ' + response.statusText);
   235        });
   236      }
   237    }
   238  
   239  });
   240  
   241  ItemBaseController = Ember.ArrayController.extend({
   242    needs: ["dc", "application"],
   243    queryParams: ["filter", "status", "condensed"],
   244    dc: Ember.computed.alias("controllers.dc"),
   245    condensed: true,
   246    hasExpanded: true,
   247    filterText: "Filter by name",
   248    filter: "", // default
   249    status: "any status", // default
   250    statuses: ["any status", "passing", "failing"],
   251  
   252    isShowingItem: function() {
   253      var currentPath = this.get('controllers.application.currentPath');
   254      return (currentPath === "dc.nodes.show" || currentPath === "dc.services.show");
   255    }.property('controllers.application.currentPath'),
   256  
   257    filteredContent: function() {
   258      var filter = this.get('filter');
   259      var status = this.get('status');
   260  
   261      var items = this.get('items').filter(function(item){
   262        return item.get('filterKey').toLowerCase().match(filter.toLowerCase());
   263      });
   264  
   265      switch (status) {
   266        case "passing":
   267          return items.filterBy('hasFailingChecks', false);
   268        case "failing":
   269          return items.filterBy('hasFailingChecks', true);
   270        default:
   271          return items;
   272      }
   273  
   274    }.property('filter', 'status', 'items.@each'),
   275  
   276    actions: {
   277      toggleCondensed: function() {
   278        this.toggleProperty('condensed');
   279      }
   280    }
   281  });
   282  
   283  App.NodesShowController = Ember.ObjectController.extend({
   284    needs: ["dc", "nodes"],
   285    dc: Ember.computed.alias("controllers.dc"),
   286  
   287    actions: {
   288      invalidateSession: function(sessionId) {
   289        this.set('isLoading', true);
   290        var controller = this;
   291        var node = controller.get('model');
   292        var dc = controller.get('dc').get('datacenter');
   293        var token = App.get('settings.token');
   294  
   295        if (window.confirm("Are you sure you want to invalidate this session?")) {
   296          // Delete the session
   297          Ember.$.ajax({
   298              url: (formatUrl(consulHost + "/v1/session/destroy/" + sessionId, dc, token)),
   299              type: 'PUT'
   300          }).then(function(response) {
   301            return Ember.$.getJSON(formatUrl(consulHost + '/v1/session/node/' + node.Node, dc, token)).then(function(data) {
   302              controller.set('sessions', data);
   303            });
   304          }).fail(function(response) {
   305            // Render the error message on the form if the request failed
   306            controller.set('errorMessage', 'Received error while processing: ' + response.statusText);
   307          });
   308        }
   309      }
   310    }
   311  });
   312  
   313  App.NodesController = ItemBaseController.extend({
   314    items: Ember.computed.alias("nodes"),
   315  });
   316  
   317  App.ServicesController = ItemBaseController.extend({
   318    items: Ember.computed.alias("services"),
   319  });
   320  
   321  App.AclsController = Ember.ArrayController.extend({
   322    needs: ["dc", "application"],
   323    queryParams: ["filter"],
   324    filterText: "Filter by name or ID",
   325    searchBar: true,
   326    newAclButton: true,
   327    types: ["client", "management"],
   328  
   329    dc: Ember.computed.alias("controllers.dc"),
   330    items: Ember.computed.alias("acls"),
   331  
   332    filter: "",
   333  
   334    isShowingItem: function() {
   335      var currentPath = this.get('controllers.application.currentPath');
   336      return (currentPath === "dc.acls.show");
   337    }.property('controllers.application.currentPath'),
   338  
   339    filteredContent: function() {
   340      var filter = this.get('filter');
   341  
   342      var items = this.get('items').filter(function(item, index, enumerable){
   343        // First try to match on the name
   344        var nameMatch = item.get('Name').toLowerCase().match(filter.toLowerCase());
   345        if (nameMatch !== null) {
   346          return nameMatch;
   347        } else {
   348          return item.get('ID').toLowerCase().match(filter.toLowerCase());
   349        }
   350      });
   351  
   352      return items;
   353    }.property('filter', 'items.@each'),
   354  
   355    actions: {
   356      createAcl: function() {
   357        this.set('isLoading', true);
   358  
   359        var controller = this;
   360        var newAcl = controller.get('newAcl');
   361        var dc = controller.get('dc').get('datacenter');
   362        var token = App.get('settings.token');
   363  
   364        // Create the ACL
   365        Ember.$.ajax({
   366            url: formatUrl(consulHost + '/v1/acl/create', dc, token),
   367            type: 'PUT',
   368            data: JSON.stringify(newAcl)
   369        }).then(function(response) {
   370          // transition to the acl
   371          controller.transitionToRoute('acls.show', response.ID);
   372  
   373          // Get the ACL again, including the newly created one
   374          Ember.$.getJSON(formatUrl(consulHost + '/v1/acl/list', dc, token)).then(function(data) {
   375            var objs = [];
   376            data.map(function(obj){
   377              objs.push(App.Acl.create(obj));
   378            });
   379            controller.set('items', objs);
   380          });
   381  
   382          controller.set('isLoading', false);
   383        }).fail(function(response) {
   384          // Render the error message on the form if the request failed
   385          notify('Received error while creating ACL: ' + response.statusText, 8000);
   386          controller.set('isLoading', false);
   387        });
   388      },
   389    }
   390  });
   391  
   392  
   393  App.AclsShowController = Ember.ObjectController.extend({
   394    needs: ["dc", "acls"],
   395    dc: Ember.computed.alias("controllers.dc"),
   396    isLoading: false,
   397    types: ["client", "management"],
   398  
   399    actions: {
   400      set: function() {
   401        this.set('isLoading', true);
   402        var controller = this;
   403        var acl = controller.get('model');
   404        var dc = controller.get('dc').get('datacenter');
   405  
   406        if (window.confirm("Are you sure you want to use this token for your session?")) {
   407          // Set
   408          var token = App.set('settings.token', acl.ID);
   409          controller.transitionToRoute('services');
   410          this.set('isLoading', false);
   411          notify('Now using token: ' + acl.ID, 3000);
   412        }
   413      },
   414  
   415      clone: function() {
   416        this.set('isLoading', true);
   417        var controller = this;
   418        var acl = controller.get('model');
   419        var dc = controller.get('dc').get('datacenter');
   420        var token = App.get('settings.token');
   421  
   422        // Set
   423        controller.transitionToRoute('services');
   424  
   425        Ember.$.ajax({
   426            url: formatUrl(consulHost + '/v1/acl/clone/'+ acl.ID, dc, token),
   427            type: 'PUT'
   428        }).then(function(response) {
   429          controller.transitionToRoute('acls.show', response.ID);
   430          controller.set('isLoading', false);
   431          notify('Successfully cloned token', 4000);
   432        }).fail(function(response) {
   433          // Render the error message on the form if the request failed
   434          controller.set('errorMessage', 'Received error while processing: ' + response.statusText);
   435          controller.set('isLoading', false);
   436        });
   437  
   438      },
   439  
   440      delete: function() {
   441        this.set('isLoading', true);
   442        var controller = this;
   443        var acl = controller.get('model');
   444        var dc = controller.get('dc').get('datacenter');
   445        var token = App.get('settings.token');
   446  
   447        if (window.confirm("Are you sure you want to delete this token?")) {
   448          Ember.$.ajax({
   449              url: formatUrl(consulHost + '/v1/acl/destroy/'+ acl.ID, dc, token),
   450              type: 'PUT'
   451          }).then(function(response) {
   452            Ember.$.getJSON(formatUrl(consulHost + '/v1/acl/list', dc, token)).then(function(data) {
   453              objs = [];
   454              data.map(function(obj){
   455                if (obj.ID === "anonymous") {
   456                  objs.unshift(App.Acl.create(obj));
   457                } else {
   458                  objs.push(App.Acl.create(obj));
   459                }
   460              });
   461              controller.get('controllers.acls').set('acls', objs);
   462            }).then(function() {
   463              controller.transitionToRoute('acls');
   464              controller.set('isLoading', false);
   465              notify('ACL deleted successfully', 3000);
   466            });
   467          }).fail(function(response) {
   468            // Render the error message on the form if the request failed
   469            controller.set('errorMessage', 'Received error while processing: ' + response.statusText);
   470            controller.set('isLoading', false);
   471          });
   472        }
   473      },
   474  
   475      updateAcl: function() {
   476        this.set('isLoading', true);
   477  
   478        var controller = this;
   479        var acl = controller.get('model');
   480        var dc = controller.get('dc').get('datacenter');
   481        var token = App.get('settings.token');
   482  
   483        // Update the ACL
   484        Ember.$.ajax({
   485            url: formatUrl(consulHost + '/v1/acl/update', dc, token),
   486            type: 'PUT',
   487            data: JSON.stringify(acl)
   488        }).then(function(response) {
   489          // transition to the acl
   490          controller.set('isLoading', false);
   491          notify('ACL updated successfully', 3000);
   492        }).fail(function(response) {
   493          // Render the error message on the form if the request failed
   494          notify('Received error while updating ACL: ' + response.statusText, 8000);
   495          controller.set('isLoading', false);
   496        });
   497      }
   498    }
   499  });
   500  
   501  App.SettingsController = Ember.ObjectController.extend({
   502    actions: {
   503      reset: function() {
   504        this.set('isLoading', true);
   505        var controller = this;
   506  
   507        if (window.confirm("Are your sure you want to reset your settings?")) {
   508          localStorage.clear();
   509          controller.set('content', App.Settings.create());
   510          App.set('settings.token', '');
   511          notify('Settings reset', 3000);
   512          this.set('isLoading', false);
   513        }
   514      },
   515  
   516      close: function() {
   517        this.transitionToRoute('index');
   518      }
   519    }
   520  });
   521  
   522  App.ErrorController = Ember.ObjectController.extend({
   523    actions: {
   524      resetToken: function() {
   525        App.set('settings.token', '');
   526        this.transitionToRoute('settings');
   527      },
   528  
   529      backHome: function() {
   530        this.transitionToRoute('index');
   531      }
   532    }
   533  });
   534