Commit 100d4fa9 authored by Leonardo Menezes's avatar Leonardo Menezes
Browse files

first version of repository / snapshot modules

parent df5bbad5
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ class CreateIndexController extends BaseController {

  def execute = process { (request, client) =>
    client.createIndex(
      request.get("index"), request.getOpt("metadata").getOrElse(Json.obj()),
      request.get("index"), request.getObjOpt("metadata").getOrElse(Json.obj()),
      ElasticServer(request.host, request.authentication)
    ).map { response =>
      Status(response.status)(response.body)
+31 −0
Original line number Diff line number Diff line
package controllers

import models.ElasticServer
import models.repository.Repositories
import scala.concurrent.ExecutionContext.Implicits.global

class RepositoriesController extends BaseController {

  def get = process { (request, client) =>
    client.getRepositories(ElasticServer(request.host, request.authentication)).map { response =>
      Status(response.status)(Repositories(response.body))
    }
  }

  def save = process { (request, client) =>
    val name = request.get("name")
    val repoType = request.get("type")
    val settings = request.getObj("settings")
    client.createRepository(name, repoType, settings, ElasticServer(request.host, request.authentication)).map {
      response => Status(response.status)(response.body)
    }
  }

  def delete = process { (request, client) =>
    val name = request.get("name")
    client.deleteRepository(name, ElasticServer(request.host, request.authentication)).map { response =>
      Status(response.status)(response.body)
    }
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ class RestController extends BaseController {
    client.executeRequest(
      request.get("method"),
      request.get("path"),
      request.getOpt("data"),
      request.getObjOpt("data"),
      ElasticServer(request.host, request.authentication)
    ).map { response =>
      Status(response.status)(response.body)
+67 −0
Original line number Diff line number Diff line
package controllers

import models.ElasticServer
import models.commons.Indices
import models.snapshot.{Repositories, Snapshots}
import play.api.libs.json.Json

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class SnapshotsController extends BaseController {

  def get = process { (request, client) =>
    Future.sequence(Seq(
      client.getIndices(ElasticServer(request.host, request.authentication)),
      client.getRepositories(ElasticServer(request.host, request.authentication))
    )).map { responses =>
      Json.obj(
        "indices" -> Indices(responses(0).body),
        "repositories" -> Repositories(responses(1).body)
      )
    }.map(Ok(_))
  }

  def getSnapshots = process { (request, client) =>
    val repository = request.get("repository")
    client.getSnapshots(repository, ElasticServer(request.host, request.authentication)).map { response =>
      Status(response.status)(Snapshots(response.body))
    }
  }

  def delete = process { (request, client) =>
    val repository = request.get("repository")
    val snapshot = request.get("snapshot")
    client.deleteSnapshot(repository, snapshot, ElasticServer(request.host, request.authentication)).map { response =>
      Status(response.status)(response.body)
    }
  }

  def create = process { (request, client) =>
    val repository = request.get("repository")
    val snapshot = request.get("snapshot")
    val indices = request.getAsStringArray("indices").map(_.mkString)
    val ignoreUnavailable = request.getBoolean("ignoreUnavailable")
    val includeGlobalState = request.getBoolean("includeGlobalState")
    client.createSnapshot(repository, snapshot, ignoreUnavailable,
      includeGlobalState, indices, ElasticServer(request.host, request.authentication)).map {
      response => Status(response.status)(response.body)
    }
  }

  def restore = process { (request, client) =>
    val repository = request.get("repository")
    val snapshot = request.get("snapshot")
    val renamePattern = request.getOpt("renamePattern")
    val renameReplacement = request.getOpt("renameReplacement")
    val ignoreUnavailable = request.getBoolean("ignoreUnavailable")
    val includeAliases = request.getBoolean("includeAliases")
    val includeGlobalState = request.getBoolean("includeGlobalState")
    val indices = request.getAsStringArray("indices").map(_.mkString)
    client.restoreSnapshot(repository, snapshot, renamePattern,
      renameReplacement, ignoreUnavailable, includeAliases, includeGlobalState,
      indices, ElasticServer(request.host, request.authentication)).map {
      response => Status(response.status)(response.body)
    }
  }
}
+62 −1
Original line number Diff line number Diff line
@@ -2,7 +2,8 @@ package elastic

import models.{ESAuth, ElasticServer}
import play.api.Play.current
import play.api.libs.json.{JsArray, JsObject, JsValue, Json}
import play.api.http
import play.api.libs.json._
import play.api.libs.ws.{WS, WSAuthScheme}

import scala.concurrent.ExecutionContext.Implicits.global
@@ -190,6 +191,66 @@ trait ElasticClient {
    execute(s"${target.host}$path", "GET", None, target.authentication)
  }


  // Repositories
  def getRepositories(target: ElasticServer) = {
    val path = s"/_snapshot"
    execute(s"${target.host}$path", "GET", None, target.authentication)
  }

  def createRepository(name: String, repoType: String, settings: JsValue, target: ElasticServer) = {
    val path = s"/_snapshot/$name"
    val data = Json.obj("type" -> JsString(repoType), "settings" -> settings).toString
    execute(s"${target.host}$path", "PUT", Some(data), target.authentication)
  }

  def deleteRepository(name: String, target: ElasticServer) = {
    val path = s"/_snapshot/$name"
    execute(s"${target.host}$path", "DELETE", None, target.authentication)
  }

  // Snapshots
  def getSnapshots(repository: String, target: ElasticServer) = {
    val path = s"/_snapshot/$repository/_all"
    execute(s"${target.host}$path", "GET", None, target.authentication)
  }

  def deleteSnapshot(repository: String, snapshot: String, target: ElasticServer) = {
    val path = s"/_snapshot/$repository/$snapshot"
    execute(s"${target.host}$path", "DELETE", None, target.authentication)
  }

  def createSnapshot(repository: String, snapshot: String, ignoreUnavailable: Boolean,
                     includeGlobalState: Boolean, indices: Option[String], target: ElasticServer) = {
    val path = s"/_snapshot/$repository/$snapshot"
    val data = JsObject(
      Seq(
        ("repository", JsString(repository)),
        ("snapshot", JsString(snapshot)),
        ("ignoreUnavailable", JsBoolean(ignoreUnavailable)),
        ("includeGlobalState", JsBoolean(includeGlobalState))
      ) ++ indices.map { i => Seq(("indices", JsString(i))) }.getOrElse(Nil)
    ).toString
    execute(s"${target.host}$path", "PUT", Some(data), target.authentication)
  }

  def restoreSnapshot(repository: String, snapshot: String, renamePattern: Option[String],
                      renameReplacement: Option[String], ignoreUnavailable: Boolean, includeAliases: Boolean,
                      includeGlobalState: Boolean, indices: Option[String], target: ElasticServer) = {
    val path = s"/_snapshot/$repository/$snapshot/_restore"
    val data = JsObject(
      Seq(
        ("ignore_unavailable", JsBoolean(ignoreUnavailable)),
        ("include_global_state", JsBoolean(includeGlobalState)),
        ("include_aliases", JsBoolean(includeAliases))
      ) ++
      indices.map { i => Seq(("indices", JsString(i))) }.getOrElse(Nil) ++
      renamePattern.map { r => Seq(("rename_pattern", JsString(r))) }.getOrElse(Nil) ++
        renameReplacement.map { r => Seq(("rename_replacement", JsString(r))) }.getOrElse(Nil)
    ).toString
    execute(s"${target.host}$path", "POST", Some(data), target.authentication)
  }

  def saveClusterSettings(settings: JsValue, target: ElasticServer) = {
    val path = s"/_cluster/settings"
    execute(s"${target.host}$path", "PUT", Some(settings.toString), target.authentication)
Loading