Commit c0017f46 authored by Leonardo Menezes's avatar Leonardo Menezes
Browse files

store requests to db

parent c6dce6df
Loading
Loading
Loading
Loading
+18 −10
Original line number Diff line number Diff line
package controllers

import java.sql.Date
import javax.inject.Inject

import controllers.auth.AuthenticationModule
import dao.{RestHistoryDAO, RestRequest}
import elastic.{ElasticClient, Error, Success}
import models.{CerebroResponse, ClusterMapping, Hosts}
import play.api.libs.json.{JsArray, Json}

import scala.concurrent.ExecutionContext.Implicits.global

class RestController @Inject()(val authentication: AuthenticationModule,
                               val hosts: Hosts,
                               client: ElasticClient) extends BaseController {
                               client: ElasticClient,
                               restHistoryDAO: RestHistoryDAO) extends BaseController {

  def request = process { request =>
    client.executeRequest(
      request.get("method"),
      request.get("path"),
      request.getObjOpt("data"),
      request.target
    ).map { response =>
      CerebroResponse(response.status, response.body)
    val method = request.get("method")
    val path = request.get("path")
    val body = request.getObjOpt("data")
    client.executeRequest(method, path, body, request.target).map {
      case s: Success =>
        val bodyAsString = body.map(_.toString).getOrElse("{}")
        val username = request.user.map(_.name).getOrElse("")
        restHistoryDAO.save(RestRequest(path, method, bodyAsString, username, new Date(System.currentTimeMillis)))
        CerebroResponse(s.status, s.body)

      case e: Error => CerebroResponse(e.status, e.body)
    }
  }

  def getClusterMapping = process { request =>
  def index = process { request =>
    client.getClusterMapping(request.target).map {
      case Success(status, mapping) => CerebroResponse(status, ClusterMapping(mapping))
      case Success(status, body) => CerebroResponse(status, ClusterMapping(body))
      case Error(status, error) => CerebroResponse(status, error)
    }
  }
+1 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ POST /aliases/update_aliases controllers.AliasesControl

# Rest module
POST        /rest/request                             controllers.RestController.request
POST        /rest/get_cluster_mapping                 controllers.RestController.getClusterMapping
POST        /rest                                     controllers.RestController.index

# Overview module
POST        /overview                                 controllers.ClusterOverviewController.index
+27 −16
Original line number Diff line number Diff line
@@ -1108,9 +1108,9 @@ angular.module('cerebro').factory('RepositoriesDataService', ['DataService',
]);

angular.module('cerebro').controller('RestController', ['$scope', '$http',
  '$sce', 'DataService', 'AlertService', 'ModalService', 'AceEditorService',
  '$sce', 'RestDataService', 'AlertService', 'ModalService', 'AceEditorService',
  'ClipboardService',
  function($scope, $http, $sce, DataService, AlertService, ModalService,
  function($scope, $http, $sce, RestDataService, AlertService, ModalService,
           AceEditorService, ClipboardService) {

    $scope.editor = undefined;
@@ -1132,14 +1132,15 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http',

    $scope.execute = function() {
      var data = $scope.editor.getValue();
      var method = $scope.method;
      $scope.response = undefined;
      DataService.execute($scope.method, $scope.path, data, success, failure);
      RestDataService.execute(method, $scope.path, data, success, failure);
    };

    $scope.setup = function() {
      $scope.editor = AceEditorService.init('rest-client-editor');
      $scope.editor.setValue('{}');
      DataService.getClusterMapping(
      RestDataService.load(
        function(response) {
          $scope.mappings = response;
          $scope.updateOptions($scope.path);
@@ -1159,7 +1160,7 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http',

    $scope.copyAsCURLCommand = function() {
      var method = $scope.method;
      var host = DataService.getHost();
      var host = RestDataService.getHost();
      var path = encodeURI($scope.path);
      if (path.substring(0, 1) !== '/') {
        path = '/' + path;
@@ -1183,6 +1184,27 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http',
  }]
);

angular.module('cerebro').factory('RestDataService', ['DataService',
  function(DataService) {

    this.load = function(success, error) {
      DataService.send('/rest', {}, success, error);
    };

    this.execute = function(method, path, data, success, error) {
      var requestData = {method: method, data: data, path: path};
      DataService.send('/rest/request', requestData, success, error);
    };

    this.getHost = function() {
      return DataService.getHost();
    };

    return this;

  }
]);

angular.module('cerebro').controller('SnapshotController', ['$scope',
'SnapshotsDataService', 'AlertService', 'ModalService',
  function($scope, SnapshotsDataService, AlertService, ModalService) {
@@ -2494,17 +2516,6 @@ angular.module('cerebro').factory('DataService', ['$rootScope', '$timeout',
      request(config, success, error);
    };

    // ---------- Rest ----------

    this.getClusterMapping = function(success, error) {
      clusterRequest('/rest/get_cluster_mapping', {}, success, error);
    };

    this.execute = function(method, path, data, success, error) {
      var requestData = {method: method, data: data, path: path};
      clusterRequest('/rest/request', requestData, success, error);
    };

    // ---------- External API ----------

    this.send = function(path, data, success, error) {
+6 −5
Original line number Diff line number Diff line
angular.module('cerebro').controller('RestController', ['$scope', '$http',
  '$sce', 'DataService', 'AlertService', 'ModalService', 'AceEditorService',
  '$sce', 'RestDataService', 'AlertService', 'ModalService', 'AceEditorService',
  'ClipboardService',
  function($scope, $http, $sce, DataService, AlertService, ModalService,
  function($scope, $http, $sce, RestDataService, AlertService, ModalService,
           AceEditorService, ClipboardService) {

    $scope.editor = undefined;
@@ -23,14 +23,15 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http',

    $scope.execute = function() {
      var data = $scope.editor.getValue();
      var method = $scope.method;
      $scope.response = undefined;
      DataService.execute($scope.method, $scope.path, data, success, failure);
      RestDataService.execute(method, $scope.path, data, success, failure);
    };

    $scope.setup = function() {
      $scope.editor = AceEditorService.init('rest-client-editor');
      $scope.editor.setValue('{}');
      DataService.getClusterMapping(
      RestDataService.load(
        function(response) {
          $scope.mappings = response;
          $scope.updateOptions($scope.path);
@@ -50,7 +51,7 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http',

    $scope.copyAsCURLCommand = function() {
      var method = $scope.method;
      var host = DataService.getHost();
      var host = RestDataService.getHost();
      var path = encodeURI($scope.path);
      if (path.substring(0, 1) !== '/') {
        path = '/' + path;
+20 −0
Original line number Diff line number Diff line
angular.module('cerebro').factory('RestDataService', ['DataService',
  function(DataService) {

    this.load = function(success, error) {
      DataService.send('/rest', {}, success, error);
    };

    this.execute = function(method, path, data, success, error) {
      var requestData = {method: method, data: data, path: path};
      DataService.send('/rest/request', requestData, success, error);
    };

    this.getHost = function() {
      return DataService.getHost();
    };

    return this;

  }
]);
Loading