Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
wip: ror stating algorithm
- Loading branch information
Showing
31 changed files
with
251 additions
and
75 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 25 additions & 2 deletions
27
core/src/main/scala/tech/beshu/ror/configuration/ConfigLoader.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,31 @@ | ||
package tech.beshu.ror.configuration | ||
|
||
import io.circe.Json | ||
import monix.eval.Task | ||
import tech.beshu.ror.configuration.ConfigLoader.{ConfigLoaderError, RawRorConfig} | ||
import tech.beshu.ror.configuration.ConfigLoader.ConfigLoaderError.{MoreThanOneRorSection, NoRorSection} | ||
|
||
trait ConfigLoader { | ||
trait ConfigLoader[SPECIALIZED_ERROR] { | ||
|
||
def load(): Task[RawRorConfig] | ||
def load(): Task[Either[ConfigLoaderError[SPECIALIZED_ERROR], RawRorConfig]] | ||
|
||
protected def validateRorJson(json: Json): Either[ConfigLoaderError[SPECIALIZED_ERROR], Json] = { | ||
json \\ "readonlyrest" match { | ||
case Nil => Left(NoRorSection) | ||
case one :: Nil => Right(one) | ||
case _ => Left(MoreThanOneRorSection) | ||
} | ||
} | ||
} | ||
|
||
object ConfigLoader { | ||
|
||
final case class RawRorConfig(rawConfig: Json) extends AnyVal | ||
|
||
sealed trait ConfigLoaderError[+SPECIALIZED_ERROR] | ||
object ConfigLoaderError { | ||
case object NoRorSection extends ConfigLoaderError[Nothing] | ||
case object MoreThanOneRorSection extends ConfigLoaderError[Nothing] | ||
final case class SpecializedError[ERROR](error: ERROR) extends ConfigLoaderError[ERROR] | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
core/src/main/scala/tech/beshu/ror/configuration/EsConfig.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package tech.beshu.ror.configuration | ||
|
||
import java.nio.file.Path | ||
|
||
import better.files.File | ||
import monix.eval.Task | ||
import org.yaml.snakeyaml.Yaml | ||
import tech.beshu.ror.configuration.EsConfig.LoadEsConfigError.{FileNotFound, MalformedContent} | ||
|
||
import scala.collection.JavaConverters._ | ||
import scala.reflect.ClassTag | ||
import scala.util.Try | ||
|
||
final case class EsConfig(forceLoadRorFromFile: Boolean) | ||
|
||
object EsConfig { | ||
|
||
def from(path: Path): Task[Either[LoadEsConfigError, EsConfig]] = Task { | ||
val config = File(s"${path.toAbsolutePath}/elasticsearch.yml") | ||
for { | ||
_ <- Either.cond(config.exists, (), FileNotFound(config)) | ||
content <- parseFileContent(config) | ||
} yield EsConfig( | ||
get(content, "readonlyrest.force_load_from_file", false) | ||
) | ||
} | ||
|
||
private def parseFileContent(file: File) = { | ||
val yaml = new Yaml() | ||
Try(yaml.load[java.util.Map[String, Object]](file.inputStream.get())) | ||
.toEither | ||
.left.map(MalformedContent(file, _)) | ||
.map(_.asScala.toMap[String, Any]) | ||
} | ||
|
||
private def get[T : ClassTag](config: Map[String, Any], key: String, default: T) = { | ||
config | ||
.get(key) | ||
.collect { case value: T => value } | ||
.getOrElse(default) | ||
} | ||
|
||
sealed trait LoadEsConfigError | ||
object LoadEsConfigError { | ||
final case class FileNotFound(file: File) extends LoadEsConfigError | ||
final case class MalformedContent(file: File, throwable: Throwable) extends LoadEsConfigError | ||
} | ||
} |
46 changes: 44 additions & 2 deletions
46
core/src/main/scala/tech/beshu/ror/configuration/FileConfigLoader.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,50 @@ | ||
package tech.beshu.ror.configuration | ||
|
||
import java.io.InputStreamReader | ||
import java.nio.file.Path | ||
|
||
import better.files.File | ||
import io.circe.Json | ||
import io.circe.yaml.parser | ||
import monix.eval.Task | ||
import tech.beshu.ror.Constants | ||
import tech.beshu.ror.configuration.ConfigLoader.{ConfigLoaderError, RawRorConfig} | ||
import tech.beshu.ror.configuration.ConfigLoader.ConfigLoaderError.SpecializedError | ||
import tech.beshu.ror.configuration.FileConfigLoader.FileConfigError | ||
import tech.beshu.ror.configuration.FileConfigLoader.FileConfigError.{FileNotExist, InvalidFileContent} | ||
import tech.beshu.ror.utils.EnvVarsProvider | ||
|
||
class FileConfigLoader(envVarsProvider: EnvVarsProvider) extends ConfigLoader { | ||
override def load(): Task[RawRorConfig] = ??? | ||
class FileConfigLoader(esConfigFolderPath: Path, | ||
envVarsProvider: EnvVarsProvider) extends ConfigLoader[FileConfigError] { | ||
|
||
override def load(): Task[Either[ConfigLoaderError[FileConfigError], RawRorConfig]] = Task { | ||
val configFile = envVarsProvider.getEnv(Constants.SETTINGS_YAML_FILE_PATH_PROPERTY) match { | ||
case Some(customRorFilePath) => File(customRorFilePath) | ||
case None => File(s"${esConfigFolderPath.toAbsolutePath}/readonlyrest.yml") | ||
} | ||
loadRorConfigFromFile(configFile) | ||
} | ||
|
||
private def loadRorConfigFromFile(file: File) = { | ||
for { | ||
_ <- Either.cond(file.exists, (), SpecializedError(FileNotExist(file))) | ||
content <- parseFileContent(file) | ||
} yield RawRorConfig(content) | ||
} | ||
|
||
private def parseFileContent(file: File): Either[ConfigLoaderError[FileConfigError], Json] = { | ||
parser | ||
.parse(new InputStreamReader(file.inputStream.get())) | ||
.left.map(ex => SpecializedError(InvalidFileContent(file, ex))) | ||
.flatMap { json => validateRorJson(json) } | ||
} | ||
} | ||
|
||
object FileConfigLoader { | ||
|
||
sealed trait FileConfigError | ||
object FileConfigError { | ||
final case class FileNotExist(file: File) extends FileConfigError | ||
final case class InvalidFileContent(file: File, throwable: Throwable) extends FileConfigError | ||
} | ||
} |
39 changes: 37 additions & 2 deletions
39
core/src/main/scala/tech/beshu/ror/configuration/IndexConfigLoader.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,42 @@ | ||
package tech.beshu.ror.configuration | ||
|
||
import io.circe.yaml.parser | ||
import monix.eval.Task | ||
import tech.beshu.ror.configuration.ConfigLoader.{ConfigLoaderError, RawRorConfig} | ||
import tech.beshu.ror.configuration.ConfigLoader.ConfigLoaderError.SpecializedError | ||
import tech.beshu.ror.configuration.IndexConfigLoader.IndexConfigError | ||
import tech.beshu.ror.configuration.IndexConfigLoader.IndexConfigError.{IndexConfigNotExist, InvalidIndexContent} | ||
import tech.beshu.ror.es.IndexContentProvider | ||
|
||
class IndexConfigLoader(indexContentProvider: IndexContentProvider) | ||
extends ConfigLoader[IndexConfigError] { | ||
|
||
override def load(): Task[Either[ConfigLoaderError[IndexConfigError], RawRorConfig]] = | ||
indexContentProvider | ||
.contentOf(".readonlyrest", "settings", "1") | ||
.map { | ||
case Right(content) => | ||
parseIndexContent(content).map(RawRorConfig.apply) | ||
case Left(IndexContentProvider.Error.CannotReachContentSource) => | ||
Left(SpecializedError[IndexConfigError](IndexConfigNotExist)) | ||
case Left(IndexContentProvider.Error.ContentNotFound) => | ||
Left(SpecializedError[IndexConfigError](IndexConfigNotExist)) | ||
} | ||
|
||
private def parseIndexContent(content: String) = { | ||
parser | ||
.parse(content) | ||
.left.map(ex => SpecializedError(InvalidIndexContent(ex))) | ||
.flatMap { json => validateRorJson(json) } | ||
} | ||
} | ||
|
||
object IndexConfigLoader { | ||
|
||
sealed trait IndexConfigError | ||
object IndexConfigError { | ||
case object IndexConfigNotExist extends IndexConfigError | ||
final case class InvalidIndexContent(throwable: Throwable) extends IndexConfigError | ||
} | ||
|
||
class IndexConfigLoader extends ConfigLoader { | ||
override def load(): Task[RawRorConfig] = ??? | ||
} |
24 changes: 0 additions & 24 deletions
24
core/src/main/scala/tech/beshu/ror/configuration/RawRorConfig.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package tech.beshu.ror.es | ||
|
||
trait AuditSink { | ||
def submit(indexName: String, documentId: String, jsonRecord: String): Unit | ||
} |
17 changes: 17 additions & 0 deletions
17
core/src/main/scala/tech/beshu/ror/es/IndexContentProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package tech.beshu.ror.es | ||
|
||
import monix.eval.Task | ||
|
||
trait IndexContentProvider { | ||
|
||
def contentOf(index: String, `type`: String, id: String): Task[Either[IndexContentProvider.Error, String]] | ||
} | ||
|
||
object IndexContentProvider { | ||
|
||
sealed trait Error | ||
object Error { | ||
case object ContentNotFound extends Error | ||
case object CannotReachContentSource extends Error | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.