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

added support to stored hosts with authentication

parent a05c542e
Loading
Loading
Loading
Loading
+19 −4
Original line number Diff line number Diff line
package controllers

import play.api.Play
import play.api.libs.json.{JsArray, JsString}
import play.api.libs.json.{JsArray, Json}
import play.api.mvc.{Action, Controller}


@@ -10,10 +10,25 @@ class HostsController extends Controller {
  def index = Action {
    request => {
      val hosts = Play.current.configuration.getConfigSeq("hosts") match {
        case Some(a) => a.map { b => b.getString("host").get }
        case None => Seq()
        case Some(configs) =>
          configs.map { config =>
            val username = config.getString("auth.username")
            val password = config.getString("auth.password")
            (username, password) match {
              case (Some(username), Some(password)) =>
                Json.obj(
                  "host" -> config.getString("host").get,
                  "username" -> username,
                  "password" -> password
                )
              case _ =>
                Json.obj("host" -> config.getString("host").get)
            }
      Ok(JsArray(hosts.map(JsString(_))))
          }
        case None =>
          Seq()
      }
      Ok(JsArray(hosts))
    }
  }

+13 −11
Original line number Diff line number Diff line
@@ -5,16 +5,18 @@ import play.api.libs.json._
object Aliases {

  def apply(json: JsValue): JsValue = {
    val indices = json.as[JsObject].keys.filter {
      index => (json \ index \ "aliases").as[JsObject].keys.nonEmpty
    }
    JsArray(indices.flatMap { index => flattenIndexAliases(index, (json \ index \ "aliases").as[JsObject]) }.toSeq)
    // NOTE: ES < 1.5 does not return aliases if no aliases is found
    val indices = json.as[JsObject].value.collect {
      case (index, info) if hasAliases(info) => flattenIndexAliases(index, (info \ "aliases").as[JsObject])
    }.flatten
    JsArray(indices.toSeq)
  }

  def hasAliases(json: JsValue): Boolean =
    (json \ "aliases").asOpt[JsObject].getOrElse(Json.obj()).keys.nonEmpty

  private def flattenIndexAliases(index: String, aliases: JsObject) =
    aliases.keys.map { alias =>
      buildAlias(index, alias, (aliases \ alias).get)
    }
    aliases.keys.map { alias => buildAlias(index, alias, (aliases \ alias).get) }

  private def buildAlias(index: String, alias: String, properties: JsValue): JsValue =
    Json.obj(
+0 −3
Original line number Diff line number Diff line
@@ -14,9 +14,6 @@ application.langs="en"
hosts = [
  {
    host = "http://localhost:9200"
  },
  {
    host = "http://localhost:9201"
  }
]

+3 −3
Original line number Diff line number Diff line
@@ -12,8 +12,8 @@
      <h6>HOSTS</h6>
      <table class="table">
        <tr ng-repeat="host in hosts track by $index">
          <td class="normal-action" ng-click="connect(host)">
            <span>{{host}}</span>
          <td class="normal-action" ng-click="connect(host.host, host.username, host.password)">
            <span>{{host.host}}</span>
          </td>
        </tr>
      </table>
@@ -50,7 +50,7 @@
                <span class="pull-left subtitle" ng-show="connecting">
                    <i class="fa fa-fw fa-circle-o-notch fa-spin"> </i> Connecting
                </span>
            <button type="submit" class="btn btn-success pull-right" ng-click="connect(host, username, password)">Connect</button>
            <button type="submit" class="btn btn-success pull-right" ng-click="connect(host, username, password)" ng-disabled ="!host">Connect</button>
          </div>
        </div>
      </form>