From f685c85fe68cdaa9e9063d349b672445847562f5 Mon Sep 17 00:00:00 2001 From: Leonardo Menezes Date: Sun, 3 Apr 2016 13:21:44 +0200 Subject: [PATCH] use POST for all apis, and include host on body instead of url --- app/controllers/Main.scala | 2 +- .../ClearIndexCacheController.scala | 2 +- .../elasticsearch/CloseIndexController.scala | 2 +- .../elasticsearch/DeleteIndexController.scala | 2 +- .../DisableShardAllocationController.scala | 2 +- .../ElasticsearchController.scala | 10 +-- .../EnableShardAllocationController.scala | 2 +- .../elasticsearch/GetIndexMapping.scala | 2 +- .../elasticsearch/GetIndexSettings.scala | 2 +- .../elasticsearch/NodeStatsController.scala | 2 +- .../elasticsearch/OpenIndexController.scala | 2 +- .../OptimizeIndexController.scala | 2 +- .../elasticsearch/PutClusterSettings.scala | 2 +- .../RefreshIndexController.scala | 2 +- .../MissingRequiredParamException.scala | 3 + .../MissingTargetHostException.scala | 3 + app/models/CerebroRequest.scala | 21 +++++ conf/routes | 24 ++--- public/app.js | 87 +++++++++++++------ src/services/data.js | 87 +++++++++++++------ 20 files changed, 175 insertions(+), 86 deletions(-) create mode 100644 app/exceptions/MissingRequiredParamException.scala create mode 100644 app/exceptions/MissingTargetHostException.scala create mode 100644 app/models/CerebroRequest.scala diff --git a/app/controllers/Main.scala b/app/controllers/Main.scala index 4235a94..f75435d 100644 --- a/app/controllers/Main.scala +++ b/app/controllers/Main.scala @@ -5,6 +5,6 @@ import elastic.ElasticClient object Main extends ElasticsearchController { - def index = processRequest(ElasticClient.main(_)) + def index = processRequest { request => ElasticClient.main(request.host) } } diff --git a/app/controllers/elasticsearch/ClearIndexCacheController.scala b/app/controllers/elasticsearch/ClearIndexCacheController.scala index c4e19e5..0270212 100644 --- a/app/controllers/elasticsearch/ClearIndexCacheController.scala +++ b/app/controllers/elasticsearch/ClearIndexCacheController.scala @@ -4,6 +4,6 @@ import elastic.ElasticClient class ClearIndexCacheController extends ElasticsearchController { - def index(indices: String) = processRequest(ElasticClient.clearIndexCache(indices, _)) + def index = processRequest { request => ElasticClient.clearIndexCache(request.get("indices"), request.host) } } diff --git a/app/controllers/elasticsearch/CloseIndexController.scala b/app/controllers/elasticsearch/CloseIndexController.scala index af03d96..9b96b1f 100644 --- a/app/controllers/elasticsearch/CloseIndexController.scala +++ b/app/controllers/elasticsearch/CloseIndexController.scala @@ -5,6 +5,6 @@ import elastic.ElasticClient class CloseIndexController extends ElasticsearchController { - def index(indices: String) = processRequest(ElasticClient.closeIndex(indices, _)) + def index = processRequest { request => ElasticClient.closeIndex(request.get("indices"), request.host) } } diff --git a/app/controllers/elasticsearch/DeleteIndexController.scala b/app/controllers/elasticsearch/DeleteIndexController.scala index 80eeb21..1debbd6 100644 --- a/app/controllers/elasticsearch/DeleteIndexController.scala +++ b/app/controllers/elasticsearch/DeleteIndexController.scala @@ -5,6 +5,6 @@ import elastic.ElasticClient class DeleteIndexController extends ElasticsearchController { - def index(indices: String) = processRequest(ElasticClient.deleteIndex(indices, _)) + def index = processRequest { request => ElasticClient.deleteIndex(request.get("indices"), request.host) } } diff --git a/app/controllers/elasticsearch/DisableShardAllocationController.scala b/app/controllers/elasticsearch/DisableShardAllocationController.scala index 64ab5c2..d3dbc5c 100644 --- a/app/controllers/elasticsearch/DisableShardAllocationController.scala +++ b/app/controllers/elasticsearch/DisableShardAllocationController.scala @@ -4,6 +4,6 @@ import elastic.ElasticClient class DisableShardAllocationController extends ElasticsearchController { - def index() = processRequest(ElasticClient.disableShardAllocation(_)) + def index = processRequest { request => ElasticClient.disableShardAllocation(request.host) } } diff --git a/app/controllers/elasticsearch/ElasticsearchController.scala b/app/controllers/elasticsearch/ElasticsearchController.scala index 1e2d4e5..7b50dd3 100644 --- a/app/controllers/elasticsearch/ElasticsearchController.scala +++ b/app/controllers/elasticsearch/ElasticsearchController.scala @@ -1,6 +1,7 @@ package controllers.elasticsearch import elastic.ElasticResponse +import models.{CerebroRequest, CerebroRequest$} import play.api.Logger import play.api.mvc.{Action, Controller} @@ -11,16 +12,15 @@ trait ElasticsearchController extends Controller { protected val logger = Logger("elastic") - def processRequest(f: (String => Future[ElasticResponse])) = Action.async { - request => - val host = request.queryString.getOrElse("host", Seq("http://localhost:9200")).head + def processRequest(f: (CerebroRequest => Future[ElasticResponse])) = Action.async(parse.json) { request => + val cRequest = CerebroRequest(request.body) try { - val response = f(host) + val response = f(cRequest) response.map { r => Status(r.status)(r.body) } } catch { - case _ => Future.successful(Status(500)(s"Cannot connect to $host")) + case _ => Future.successful(Status(500)(s"Cannot connect to ${cRequest.host}")) } } diff --git a/app/controllers/elasticsearch/EnableShardAllocationController.scala b/app/controllers/elasticsearch/EnableShardAllocationController.scala index ae1c4e2..fc396cf 100644 --- a/app/controllers/elasticsearch/EnableShardAllocationController.scala +++ b/app/controllers/elasticsearch/EnableShardAllocationController.scala @@ -4,6 +4,6 @@ import elastic.ElasticClient class EnableShardAllocationController extends ElasticsearchController { - def index() = processRequest(ElasticClient.enableShardAllocation(_)) + def index = processRequest { request => ElasticClient.enableShardAllocation(request.host) } } diff --git a/app/controllers/elasticsearch/GetIndexMapping.scala b/app/controllers/elasticsearch/GetIndexMapping.scala index 0ea2451..38a591f 100644 --- a/app/controllers/elasticsearch/GetIndexMapping.scala +++ b/app/controllers/elasticsearch/GetIndexMapping.scala @@ -4,6 +4,6 @@ import elastic.ElasticClient class GetIndexMapping extends ElasticsearchController { - def index(indices: String) = processRequest(ElasticClient.getIndexMapping(indices, _)) + def index = processRequest { request => ElasticClient.getIndexMapping(request.get("index"), request.host) } } diff --git a/app/controllers/elasticsearch/GetIndexSettings.scala b/app/controllers/elasticsearch/GetIndexSettings.scala index 78165ad..35cdaa2 100644 --- a/app/controllers/elasticsearch/GetIndexSettings.scala +++ b/app/controllers/elasticsearch/GetIndexSettings.scala @@ -4,6 +4,6 @@ import elastic.ElasticClient class GetIndexSettings extends ElasticsearchController { - def index(indices: String) = processRequest(ElasticClient.getIndexSettings(indices, _)) + def index = processRequest { request => ElasticClient.getIndexSettings(request.get("index"), request.host) } } diff --git a/app/controllers/elasticsearch/NodeStatsController.scala b/app/controllers/elasticsearch/NodeStatsController.scala index 568b41e..bef79cd 100644 --- a/app/controllers/elasticsearch/NodeStatsController.scala +++ b/app/controllers/elasticsearch/NodeStatsController.scala @@ -4,6 +4,6 @@ import elastic.ElasticClient class NodeStatsController extends ElasticsearchController { - def index(nodes: String) = processRequest(ElasticClient.nodesStats(nodes, _)) + def index = processRequest { request => ElasticClient.nodesStats(request.get("node"), request.host) } } diff --git a/app/controllers/elasticsearch/OpenIndexController.scala b/app/controllers/elasticsearch/OpenIndexController.scala index dc7b894..75bd4ae 100644 --- a/app/controllers/elasticsearch/OpenIndexController.scala +++ b/app/controllers/elasticsearch/OpenIndexController.scala @@ -5,6 +5,6 @@ import elastic.ElasticClient class OpenIndexController extends ElasticsearchController { - def index(indices: String) = processRequest(ElasticClient.openIndex(indices, _)) + def index = processRequest { request => ElasticClient.openIndex(request.get("indices"), request.host) } } diff --git a/app/controllers/elasticsearch/OptimizeIndexController.scala b/app/controllers/elasticsearch/OptimizeIndexController.scala index b4b8ab7..2a64d04 100644 --- a/app/controllers/elasticsearch/OptimizeIndexController.scala +++ b/app/controllers/elasticsearch/OptimizeIndexController.scala @@ -7,6 +7,6 @@ import scala.concurrent.ExecutionContext.Implicits.global class OptimizeIndexController extends ElasticsearchController { - def index(indices: String) = processRequest(ElasticClient.optimizeIndex(indices, _)) + def index = processRequest { request => ElasticClient.optimizeIndex(request.get("indices"), request.host) } } diff --git a/app/controllers/elasticsearch/PutClusterSettings.scala b/app/controllers/elasticsearch/PutClusterSettings.scala index 39321ae..5cf20d3 100644 --- a/app/controllers/elasticsearch/PutClusterSettings.scala +++ b/app/controllers/elasticsearch/PutClusterSettings.scala @@ -4,6 +4,6 @@ import elastic.ElasticClient class PutClusterSettings extends ElasticsearchController { - def index = processRequest(ElasticClient.putClusterSettings("", _)) + def index = processRequest { request => ElasticClient.putClusterSettings(request.get("settings"), request.host) } } diff --git a/app/controllers/elasticsearch/RefreshIndexController.scala b/app/controllers/elasticsearch/RefreshIndexController.scala index 2c7645a..1fe4715 100644 --- a/app/controllers/elasticsearch/RefreshIndexController.scala +++ b/app/controllers/elasticsearch/RefreshIndexController.scala @@ -4,6 +4,6 @@ import elastic.ElasticClient class RefreshIndexController extends ElasticsearchController { - def index(indices: String) = processRequest(ElasticClient.refreshIndex(indices, _)) + def index = processRequest { request => ElasticClient.refreshIndex(request.get("indices"), request.host) } } diff --git a/app/exceptions/MissingRequiredParamException.scala b/app/exceptions/MissingRequiredParamException.scala new file mode 100644 index 0000000..1f8b0ba --- /dev/null +++ b/app/exceptions/MissingRequiredParamException.scala @@ -0,0 +1,3 @@ +package exceptions + +case class MissingRequiredParamException(name: String) extends Exception diff --git a/app/exceptions/MissingTargetHostException.scala b/app/exceptions/MissingTargetHostException.scala new file mode 100644 index 0000000..81c14be --- /dev/null +++ b/app/exceptions/MissingTargetHostException.scala @@ -0,0 +1,3 @@ +package exceptions + +object MissingTargetHostException extends Exception diff --git a/app/models/CerebroRequest.scala b/app/models/CerebroRequest.scala new file mode 100644 index 0000000..27e0a95 --- /dev/null +++ b/app/models/CerebroRequest.scala @@ -0,0 +1,21 @@ +package models + +import exceptions.{MissingRequiredParamException, MissingTargetHostException} +import play.api.libs.json.JsValue + +class CerebroRequest(val host: String, body: JsValue) { + + def get(name: String) = (body \ name).asOpt[String].getOrElse(throw MissingRequiredParamException(name)) + + def getArray(name: String) = (body \ name).asOpt[Array[String]].getOrElse(throw MissingRequiredParamException(name)) + +} + +object CerebroRequest { + + def apply(body: JsValue) = { + val host = (body \ "host").asOpt[String].getOrElse(throw MissingTargetHostException) + new CerebroRequest(host, body) + } + +} diff --git a/conf/routes b/conf/routes index be5643a..ca4c9ec 100644 --- a/conf/routes +++ b/conf/routes @@ -9,18 +9,18 @@ GET / controllers.Application.index() # GET /main controllers.Main.index() GET /apis/overview controllers.ClusterOverviewController.index() -POST /apis/:indices/_close @controllers.elasticsearch.CloseIndexController.index(indices) -POST /apis/:indices/_open @controllers.elasticsearch.OpenIndexController.index(indices) -POST /apis/:indices/_optimize @controllers.elasticsearch.OptimizeIndexController.index(indices) -POST /apis/:indices/_cache/clear @controllers.elasticsearch.ClearIndexCacheController.index(indices) -POST /apis/:indices/_refresh @controllers.elasticsearch.RefreshIndexController.index(indices) -DELETE /apis/:indices/_delete @controllers.elasticsearch.DeleteIndexController.index(indices) -GET /apis/:index/_settings @controllers.elasticsearch.GetIndexSettings.index(index) -GET /apis/:index/_mapping @controllers.elasticsearch.GetIndexMapping.index(index) -PUT /apis/_cluster/settings @controllers.elasticsearch.PutClusterSettings.index -GET /apis/_nodes/:nodes/stats @controllers.elasticsearch.NodeStatsController.index(nodes) -PUT /apis/disable_allocation @controllers.elasticsearch.DisableShardAllocationController.index() -PUT /apis/enable_allocation @controllers.elasticsearch.EnableShardAllocationController.index() +POST /apis/close_indices @controllers.elasticsearch.CloseIndexController.index +POST /apis/open_indices @controllers.elasticsearch.OpenIndexController.index +POST /apis/optimize_indices @controllers.elasticsearch.OptimizeIndexController.index +POST /apis/clear_indices_cache @controllers.elasticsearch.ClearIndexCacheController.index +POST /apis/refresh_indices @controllers.elasticsearch.RefreshIndexController.index +POST /apis/delete_indices @controllers.elasticsearch.DeleteIndexController.index +POST /apis/get_index_settings @controllers.elasticsearch.GetIndexSettings.index +POST /apis/get_index_mapping @controllers.elasticsearch.GetIndexMapping.index +POST /apis/update_cluster_settings @controllers.elasticsearch.PutClusterSettings.index +POST /apis/get_node_stats @controllers.elasticsearch.NodeStatsController.index +POST /apis/disable_shard_allocation @controllers.elasticsearch.DisableShardAllocationController.index +POST /apis/enable_shard_allocation @controllers.elasticsearch.EnableShardAllocationController.index GET /apis/hosts @controllers.HostsController.index diff --git a/public/app.js b/public/app.js index 64882e1..2849b29 100644 --- a/public/app.js +++ b/public/app.js @@ -834,8 +834,11 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.closeIndex = function(index, success, error) { var config = { method: 'POST', - url: baseUrl + '/apis/' + index + '/_close', - params: {host: host} + url: baseUrl + '/apis/close_indices', + data: { + host: host, + indices: index + } }; $http(config). success(success). @@ -845,8 +848,11 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.openIndex = function(index, success, error) { var config = { method: 'POST', - url: baseUrl + '/apis/' + index + '/_open', - params: {host: host} + url: baseUrl + '/apis/open_indices', + data: { + host: host, + indices: index + } }; $http(config). success(success). @@ -856,8 +862,11 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.optimizeIndex = function(index, success, error) { var config = { method: 'POST', - url: baseUrl + '/apis/' + index + '/_optimize', - params: {host: host} + url: baseUrl + '/apis/optimize_indices', + data: { + host: host, + indices: index + } }; $http(config). success(success). @@ -867,8 +876,11 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.refreshIndex = function(index, success, error) { var config = { method: 'POST', - url: baseUrl + '/apis/' + index + '/_refresh', - params: {host: host} + url: baseUrl + '/apis/refresh_indices', + data: { + host: host, + indices: index + } }; $http(config). success(success). @@ -878,8 +890,11 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.clearIndexCache = function(index, success, error) { var config = { method: 'POST', - url: baseUrl + '/apis/' + index + '/_cache/clear', - params: {host: host} + url: baseUrl + '/apis/clear_indices_cache', + data: { + host: host, + indices: index + } }; $http(config). success(success). @@ -888,9 +903,12 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.deleteIndex = function(index, success, error) { var config = { - method: 'DELETE', - url: baseUrl + '/apis/' + index + '/_delete', - params: {host: host} + method: 'POST', + url: baseUrl + '/apis/delete_indices', + data: { + host: host, + indices: index + } }; $http(config). success(success). @@ -899,9 +917,12 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.getIndexSettings = function(index, success, error) { var config = { - method: 'GET', - url: baseUrl + '/apis/' + index + '/_settings', - params: {host: host} + method: 'POST', + url: baseUrl + '/apis/get_index_settings', + data: { + host: host, + index: index + } }; $http(config). success(success). @@ -910,9 +931,12 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.getIndexMapping = function(index, success, error) { var config = { - method: 'GET', - url: baseUrl + '/apis/' + index + '/_mapping', - params: {host: host} + method: 'POST', + url: baseUrl + '/apis/get_index_mapping', + data: { + host: host, + index: index + } }; $http(config). success(success). @@ -921,9 +945,12 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.nodeStats = function(node, success, error) { var config = { - method: 'GET', - url: baseUrl + '/apis/_nodes/' + node + '/stats', - params: {host: host} + method: 'POST', + url: baseUrl + '/apis/get_node_stats', + data: { + host: host, + node: node + } }; $http(config). success(success). @@ -932,9 +959,11 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.enableShardAllocation = function(success, error) { var config = { - method: 'PUT', - url: baseUrl + '/apis/enable_allocation', - params: {host: host} + method: 'POST', + url: baseUrl + '/apis/enable_shard_allocation', + data: { + host: host + } }; $http(config). success(success). @@ -943,9 +972,11 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.disableShardAllocation = function(success, error) { var config = { - method: 'PUT', - url: baseUrl + '/apis/disable_allocation', - params: {host: host} + method: 'POST', + url: baseUrl + '/apis/disable_shard_allocation', + data: { + host: host + } }; $http(config). success(success). diff --git a/src/services/data.js b/src/services/data.js index 0d61677..3d7feb3 100644 --- a/src/services/data.js +++ b/src/services/data.js @@ -59,8 +59,11 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.closeIndex = function(index, success, error) { var config = { method: 'POST', - url: baseUrl + '/apis/' + index + '/_close', - params: {host: host} + url: baseUrl + '/apis/close_indices', + data: { + host: host, + indices: index + } }; $http(config). success(success). @@ -70,8 +73,11 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.openIndex = function(index, success, error) { var config = { method: 'POST', - url: baseUrl + '/apis/' + index + '/_open', - params: {host: host} + url: baseUrl + '/apis/open_indices', + data: { + host: host, + indices: index + } }; $http(config). success(success). @@ -81,8 +87,11 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.optimizeIndex = function(index, success, error) { var config = { method: 'POST', - url: baseUrl + '/apis/' + index + '/_optimize', - params: {host: host} + url: baseUrl + '/apis/optimize_indices', + data: { + host: host, + indices: index + } }; $http(config). success(success). @@ -92,8 +101,11 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.refreshIndex = function(index, success, error) { var config = { method: 'POST', - url: baseUrl + '/apis/' + index + '/_refresh', - params: {host: host} + url: baseUrl + '/apis/refresh_indices', + data: { + host: host, + indices: index + } }; $http(config). success(success). @@ -103,8 +115,11 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.clearIndexCache = function(index, success, error) { var config = { method: 'POST', - url: baseUrl + '/apis/' + index + '/_cache/clear', - params: {host: host} + url: baseUrl + '/apis/clear_indices_cache', + data: { + host: host, + indices: index + } }; $http(config). success(success). @@ -113,9 +128,12 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.deleteIndex = function(index, success, error) { var config = { - method: 'DELETE', - url: baseUrl + '/apis/' + index + '/_delete', - params: {host: host} + method: 'POST', + url: baseUrl + '/apis/delete_indices', + data: { + host: host, + indices: index + } }; $http(config). success(success). @@ -124,9 +142,12 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.getIndexSettings = function(index, success, error) { var config = { - method: 'GET', - url: baseUrl + '/apis/' + index + '/_settings', - params: {host: host} + method: 'POST', + url: baseUrl + '/apis/get_index_settings', + data: { + host: host, + index: index + } }; $http(config). success(success). @@ -135,9 +156,12 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.getIndexMapping = function(index, success, error) { var config = { - method: 'GET', - url: baseUrl + '/apis/' + index + '/_mapping', - params: {host: host} + method: 'POST', + url: baseUrl + '/apis/get_index_mapping', + data: { + host: host, + index: index + } }; $http(config). success(success). @@ -146,9 +170,12 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.nodeStats = function(node, success, error) { var config = { - method: 'GET', - url: baseUrl + '/apis/_nodes/' + node + '/stats', - params: {host: host} + method: 'POST', + url: baseUrl + '/apis/get_node_stats', + data: { + host: host, + node: node + } }; $http(config). success(success). @@ -157,9 +184,11 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.enableShardAllocation = function(success, error) { var config = { - method: 'PUT', - url: baseUrl + '/apis/enable_allocation', - params: {host: host} + method: 'POST', + url: baseUrl + '/apis/enable_shard_allocation', + data: { + host: host + } }; $http(config). success(success). @@ -168,9 +197,11 @@ angular.module('cerebro').factory('DataService', function ($rootScope, $timeout, this.disableShardAllocation = function(success, error) { var config = { - method: 'PUT', - url: baseUrl + '/apis/disable_allocation', - params: {host: host} + method: 'POST', + url: baseUrl + '/apis/disable_shard_allocation', + data: { + host: host + } }; $http(config). success(success). -- GitLab