Skip to content

Commit

Permalink
Fix error when creating a view with invalid mappings or settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Dumas committed Oct 12, 2023
1 parent bd60ed2 commit ba9cc0f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ plugins.elasticsearch {
# username= "elastic_user"
# password= "password"
# }
# configuration of the Elasticsearch client
client = ${app.defaults.http-client-compression}
# the elasticsearch event log configuration
event-log = ${app.defaults.event-log}
# the elasticsearch pagination config
Expand All @@ -20,8 +18,6 @@ plugins.elasticsearch {
prefix = ${app.defaults.indexing.prefix}
# configuration of the maximum number of view references allowed on an aggregated view
max-view-refs = 20
# the maximum idle duration in between events on the indexing stream after which the stream will be stopped (min. 10 minutes).
idle-timeout = 30 minutes
# In order to disable this feature, set an infinite time ('Inf')
# idle-timeout = Inf
#the maximum duration allowed so that synchronous indexing can complete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import ch.epfl.bluebrain.nexus.delta.sdk.acls.AclCheck
import ch.epfl.bluebrain.nexus.delta.sdk.deletion.ProjectDeletionTask
import ch.epfl.bluebrain.nexus.delta.sdk.directives.DeltaSchemeDirectives
import ch.epfl.bluebrain.nexus.delta.sdk.fusion.FusionConfig
import ch.epfl.bluebrain.nexus.delta.sdk.http.HttpClient
import ch.epfl.bluebrain.nexus.delta.sdk.http.{HttpClient, HttpClientConfig}
import ch.epfl.bluebrain.nexus.delta.sdk.identities.Identities
import ch.epfl.bluebrain.nexus.delta.sdk.identities.model.ServiceAccount
import ch.epfl.bluebrain.nexus.delta.sdk.model._
Expand Down Expand Up @@ -53,8 +53,8 @@ class ElasticSearchPluginModule(priority: Int) extends ModuleDef {
make[ElasticSearchViewsConfig].from { ElasticSearchViewsConfig.load(_) }

make[HttpClient].named("elasticsearch-client").from {
(cfg: ElasticSearchViewsConfig, as: ActorSystem[Nothing], sc: Scheduler) =>
HttpClient()(cfg.client, as.classicSystem, sc)
val httpConfig = HttpClientConfig.noRetry(true)
(as: ActorSystem[Nothing], sc: Scheduler) => HttpClient()(httpConfig, as.classicSystem, sc)
}

make[ElasticSearchClient].from {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class ElasticSearchClient(client: HttpClient, endpoint: Uri, maxIndexPathLength:
case false =>
client(Put(endpoint / index.value, payload).withHttpCredentials) {
case resp if resp.status.isSuccess() => discardEntity(resp) >> IO.pure(true)
}
}.tapError { e => UIO.pure(println(e)) }
case true =>
IO.pure(false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import akka.http.scaladsl.model.headers.BasicHttpCredentials
import ch.epfl.bluebrain.nexus.delta.sdk.instances._
import ch.epfl.bluebrain.nexus.delta.plugins.elasticsearch.client.ElasticSearchClient.Refresh
import ch.epfl.bluebrain.nexus.delta.sdk.Defaults
import ch.epfl.bluebrain.nexus.delta.sdk.http.HttpClientConfig
import ch.epfl.bluebrain.nexus.delta.sdk.model.search.PaginationConfig
import ch.epfl.bluebrain.nexus.delta.sourcing.config.{BatchConfig, EventLogConfig, QueryConfig}
import com.typesafe.config.Config
import pureconfig.error.{CannotConvert, FailureReason}
import pureconfig.error.CannotConvert
import pureconfig.generic.semiauto.deriveReader
import pureconfig.{ConfigReader, ConfigSource}

Expand All @@ -23,8 +22,6 @@ import scala.concurrent.duration._
* the base uri to the Elasticsearch HTTP endpoint
* @param credentials
* the credentials to authenticate to the Elasticsearch endpoint
* @param client
* configuration of the Elasticsearch client
* @param eventLog
* configuration of the event log
* @param pagination
Expand All @@ -35,8 +32,6 @@ import scala.concurrent.duration._
* prefix for indices
* @param maxViewRefs
* configuration of the maximum number of view references allowed on an aggregated view
* @param idleTimeout
* the maximum idle duration in between events on the indexing stream after which the stream will be stopped
* @param syncIndexingTimeout
* the maximum duration for synchronous indexing to complete
* @param syncIndexingRefresh
Expand All @@ -55,13 +50,11 @@ import scala.concurrent.duration._
final case class ElasticSearchViewsConfig(
base: Uri,
credentials: Option[BasicHttpCredentials],
client: HttpClientConfig,
eventLog: EventLogConfig,
pagination: PaginationConfig,
batch: BatchConfig,
prefix: String,
maxViewRefs: Int,
idleTimeout: Duration,
syncIndexingTimeout: FiniteDuration,
syncIndexingRefresh: Refresh,
maxIndexPathLength: Int,
Expand Down Expand Up @@ -98,11 +91,5 @@ object ElasticSearchViewsConfig {
}

implicit final val elasticSearchViewsConfigReader: ConfigReader[ElasticSearchViewsConfig] =
deriveReader[ElasticSearchViewsConfig].emap { c =>
Either.cond(
c.idleTimeout.gteq(10.minutes),
c,
new FailureReason { override def description: String = "'idle-timeout' must be greater than 10 minutes" }
)
}
deriveReader[ElasticSearchViewsConfig]
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ class ElasticSearchViewsSpec extends BaseSpec with EitherValuable with CirceEq {
}
}

"fail to create a view with an invalid mapping" in {
val invalidMapping =
json"""{"mapping": "fail"}"""
val payload = json"""{ "@type": "ElasticSearchView", "mapping": $invalidMapping }"""
deltaClient.put[Json](s"/views/$fullId/invalid", payload, ScoobyDoo) { expectBadRequest }
}

"fail to create a view with invalid settings" in {
val invalidSettings =
json"""{"analysis": "fail"}"""
val payload = json"""{ "@type": "ElasticSearchView", "mapping": { }, "settings": $invalidSettings }"""
deltaClient.put[Json](s"/views/$fullId/invalid", payload, ScoobyDoo) { expectBadRequest }
}

"create people view in project 2" in {
deltaClient.put[Json](
s"/views/$fullId2/test-resource:people",
Expand Down

0 comments on commit ba9cc0f

Please sign in to comment.