Skip to content

Commit

Permalink
Migrate all ScopeInitializations
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergrabinski committed Oct 3, 2023
1 parent 651a4db commit 90631e1
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package ch.epfl.bluebrain.nexus.delta.plugins.blazegraph

import cats.effect.IO
import cats.implicits._
import ch.epfl.bluebrain.nexus.delta.kernel.Logger
import ch.epfl.bluebrain.nexus.delta.kernel.effect.migration._
import ch.epfl.bluebrain.nexus.delta.kernel.kamon.KamonMetricComponent
import ch.epfl.bluebrain.nexus.delta.kernel.syntax._
import ch.epfl.bluebrain.nexus.delta.kernel.syntax.kamonSyntax
import ch.epfl.bluebrain.nexus.delta.plugins.blazegraph.BlazegraphViews.entityType
import ch.epfl.bluebrain.nexus.delta.plugins.blazegraph.model.BlazegraphViewRejection.{ProjectContextRejection, ResourceAlreadyExists}
import ch.epfl.bluebrain.nexus.delta.plugins.blazegraph.model.BlazegraphViewValue.IndexingBlazegraphViewValue
import ch.epfl.bluebrain.nexus.delta.plugins.blazegraph.model._
import ch.epfl.bluebrain.nexus.delta.sdk.{Defaults, ScopeInitialization}
import ch.epfl.bluebrain.nexus.delta.sdk.error.ServiceError.ScopeInitializationFailed
import ch.epfl.bluebrain.nexus.delta.sdk.identities.model.ServiceAccount
import ch.epfl.bluebrain.nexus.delta.sdk.organizations.model.Organization
import ch.epfl.bluebrain.nexus.delta.sdk.projects.model.Project
import ch.epfl.bluebrain.nexus.delta.sdk.{Defaults, ScopeInitialization}
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity.Subject
import com.typesafe.scalalogging.Logger
import monix.bio.{IO, UIO}

/**
* The default creation of the default SparqlView as part of the project initialization.
Expand All @@ -30,7 +32,7 @@ class BlazegraphScopeInitialization(
defaults: Defaults
) extends ScopeInitialization {

private val logger: Logger = Logger[BlazegraphScopeInitialization]
private val logger = Logger.cats[BlazegraphScopeInitialization]
implicit private val serviceAccountSubject: Subject = serviceAccount.subject
implicit private val kamonComponent: KamonMetricComponent = KamonMetricComponent(entityType.value)

Expand All @@ -45,23 +47,23 @@ class BlazegraphScopeInitialization(
permission = permissions.query
)

override def onProjectCreation(project: Project, subject: Identity.Subject): IO[ScopeInitializationFailed, Unit] =
override def onProjectCreation(project: Project, subject: Identity.Subject): IO[Unit] =
views
.create(defaultViewId, project.ref, defaultValue)
.void
.onErrorHandleWith {
case _: ResourceAlreadyExists => UIO.unit // nothing to do, view already exits
case _: ProjectContextRejection => UIO.unit // project or org are likely deprecated
.handleErrorWith {
case _: ResourceAlreadyExists => IO.unit // nothing to do, view already exits
case _: ProjectContextRejection => IO.unit // project or org are likely deprecated
case rej =>
val str =
s"Failed to create the default SparqlView for project '${project.ref}' due to '${rej.reason}'."
UIO.delay(logger.error(str)) >> IO.raiseError(ScopeInitializationFailed(str))
s"Failed to create the default SparqlView for project '${project.ref}' due to '${rej.getMessage}'."
logger.error(str) >> IO.raiseError(ScopeInitializationFailed(str))
}
.span("createDefaultSparqlView")

override def onOrganizationCreation(
organization: Organization,
subject: Identity.Subject
): IO[ScopeInitializationFailed, Unit] = IO.unit
): IO[Unit] = IO.unit

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import ch.epfl.bluebrain.nexus.delta.sdk.projects.FetchContext.ContextRejection
import ch.epfl.bluebrain.nexus.delta.sdk.views.ViewRef
import ch.epfl.bluebrain.nexus.delta.sourcing.model.ProjectRef
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Tag.UserTag
import ch.epfl.bluebrain.nexus.delta.sourcing.rejection.Rejection
import io.circe.syntax.EncoderOps
import io.circe.{Encoder, JsonObject}

sealed abstract class BlazegraphViewRejection(val reason: String) extends Product with Serializable
sealed abstract class BlazegraphViewRejection(val reason: String) extends Rejection

object BlazegraphViewRejection {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ch.epfl.bluebrain.nexus.delta.plugins.elasticsearch

import cats.effect.IO
import cats.implicits._
import ch.epfl.bluebrain.nexus.delta.kernel.kamon.KamonMetricComponent
import ch.epfl.bluebrain.nexus.delta.kernel.syntax._
import ch.epfl.bluebrain.nexus.delta.plugins.elasticsearch.ElasticSearchViews.entityType
Expand All @@ -15,8 +17,8 @@ import ch.epfl.bluebrain.nexus.delta.sdk.views.PipeStep
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity.Subject
import ch.epfl.bluebrain.nexus.delta.sourcing.stream.pipes.{DefaultLabelPredicates, SourceAsText}
import com.typesafe.scalalogging.Logger
import monix.bio.{IO, UIO}
import ch.epfl.bluebrain.nexus.delta.kernel.Logger
import ch.epfl.bluebrain.nexus.delta.kernel.effect.migration._

/**
* The default creation of the default ElasticSearchView as part of the project initialization.
Expand All @@ -32,7 +34,7 @@ class ElasticSearchScopeInitialization(
defaults: Defaults
) extends ScopeInitialization {

private val logger: Logger = Logger[ElasticSearchScopeInitialization]
private val logger = Logger.cats[ElasticSearchScopeInitialization]
implicit private val serviceAccountSubject: Subject = serviceAccount.subject
implicit private val kamonComponent: KamonMetricComponent = KamonMetricComponent(entityType.value)

Expand All @@ -48,23 +50,23 @@ class ElasticSearchScopeInitialization(
permission = permissions.query
)

override def onProjectCreation(project: Project, subject: Identity.Subject): IO[ScopeInitializationFailed, Unit] =
override def onProjectCreation(project: Project, subject: Identity.Subject): IO[Unit] =
views
.create(defaultViewId, project.ref, defaultValue)
.void
.onErrorHandleWith {
case _: ResourceAlreadyExists => UIO.unit // nothing to do, view already exits
case _: ProjectContextRejection => UIO.unit // project or org are likely deprecated
.handleErrorWith {
case _: ResourceAlreadyExists => IO.unit // nothing to do, view already exits
case _: ProjectContextRejection => IO.unit // project or org are likely deprecated
case rej =>
val str =
s"Failed to create the default ElasticSearchView for project '${project.ref}' due to '${rej.reason}'."
UIO.delay(logger.error(str)) >> IO.raiseError(ScopeInitializationFailed(str))
s"Failed to create the default ElasticSearchView for project '${project.ref}' due to '${rej.getMessage}'."
logger.error(str) >> IO.raiseError(ScopeInitializationFailed(str))
}
.span("createDefaultElasticSearchView")

override def onOrganizationCreation(
organization: Organization,
subject: Identity.Subject
): IO[ScopeInitializationFailed, Unit] = IO.unit
): IO[Unit] = IO.unit

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import ch.epfl.bluebrain.nexus.delta.sdk.syntax._
import ch.epfl.bluebrain.nexus.delta.sdk.views.ViewRef
import ch.epfl.bluebrain.nexus.delta.sourcing.model.ProjectRef
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Tag.UserTag
import ch.epfl.bluebrain.nexus.delta.sourcing.rejection.Rejection
import ch.epfl.bluebrain.nexus.delta.sourcing.stream.ProjectionErr
import io.circe.syntax._
import io.circe.{Encoder, Json, JsonObject}
Expand All @@ -30,7 +31,7 @@ import io.circe.{Encoder, Json, JsonObject}
* @param reason
* a descriptive message as to why the rejection occurred
*/
sealed abstract class ElasticSearchViewRejection(val reason: String) extends Product with Serializable
sealed abstract class ElasticSearchViewRejection(val reason: String) extends Rejection

object ElasticSearchViewRejection {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class SearchPluginModule(priority: Int) extends ModuleDef {
(views: CompositeViews, config: SearchConfig, serviceAccount: ServiceAccount, baseUri: BaseUri) =>
new SearchScopeInitialization(views, config.indexing, serviceAccount, config.defaults)(baseUri)
}
many[ScopeInitialization].ref[SearchScopeInitialization]

make[SearchRoutes].from {
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import ch.epfl.bluebrain.nexus.delta.sdk.identities.model.ServiceAccount
import ch.epfl.bluebrain.nexus.delta.sdk.model.BaseUri
import ch.epfl.bluebrain.nexus.delta.sdk.organizations.model.Organization
import ch.epfl.bluebrain.nexus.delta.sdk.projects.model.Project
import ch.epfl.bluebrain.nexus.delta.sdk.{CatsScopeInitialization, Defaults}
import ch.epfl.bluebrain.nexus.delta.sdk.{Defaults, ScopeInitialization}
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity.Subject

Expand All @@ -24,7 +24,7 @@ final class SearchScopeInitialization(
serviceAccount: ServiceAccount,
defaults: Defaults
)(implicit baseUri: BaseUri)
extends CatsScopeInitialization {
extends ScopeInitialization {

private val logger = Logger.cats[SearchScopeInitialization]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.epfl.bluebrain.nexus.delta.plugins.storage

import cats.effect.IO
import ch.epfl.bluebrain.nexus.delta.kernel.kamon.KamonMetricComponent
import ch.epfl.bluebrain.nexus.delta.plugins.storage.storages.Storages.entityType
import ch.epfl.bluebrain.nexus.delta.plugins.storage.storages.model.StorageFields.DiskStorageFields
Expand All @@ -12,8 +13,9 @@ import ch.epfl.bluebrain.nexus.delta.sdk.organizations.model.Organization
import ch.epfl.bluebrain.nexus.delta.sdk.projects.model.Project
import ch.epfl.bluebrain.nexus.delta.sdk.{Defaults, ScopeInitialization}
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity
import com.typesafe.scalalogging.Logger
import monix.bio.{IO, UIO}
import cats.implicits._
import ch.epfl.bluebrain.nexus.delta.kernel.Logger
import ch.epfl.bluebrain.nexus.delta.kernel.effect.migration._

/**
* The default creation of the default disk storage as part of the project initialization.
Expand All @@ -29,7 +31,7 @@ class StorageScopeInitialization(
defaults: Defaults
) extends ScopeInitialization {

private val logger: Logger = Logger[StorageScopeInitialization]
private val logger = Logger.cats[StorageScopeInitialization]
implicit private val kamonComponent: KamonMetricComponent = KamonMetricComponent(entityType.value)

implicit private val caller: Caller = serviceAccount.caller
Expand All @@ -45,23 +47,23 @@ class StorageScopeInitialization(
maxFileSize = None
)

override def onProjectCreation(project: Project, subject: Identity.Subject): IO[ScopeInitializationFailed, Unit] =
override def onProjectCreation(project: Project, subject: Identity.Subject): IO[Unit] =
storages
.create(defaultStorageId, project.ref, defaultValue)
.void
.onErrorHandleWith {
case _: ResourceAlreadyExists => UIO.unit // nothing to do, storage already exits
case _: ProjectContextRejection => UIO.unit // project or org are likely deprecated
.handleErrorWith {
case _: ResourceAlreadyExists => IO.unit // nothing to do, storage already exits
case _: ProjectContextRejection => IO.unit // project or org are likely deprecated
case rej =>
val str =
s"Failed to create the default DiskStorage for project '${project.ref}' due to '${rej.reason}'."
UIO.delay(logger.error(str)) >> IO.raiseError(ScopeInitializationFailed(str))
s"Failed to create the default DiskStorage for project '${project.ref}' due to '${rej.getMessage}'."
logger.error(str) >> IO.raiseError(ScopeInitializationFailed(str))
}
.span("createDefaultStorage")

override def onOrganizationCreation(
organization: Organization,
subject: Identity.Subject
): IO[ScopeInitializationFailed, Unit] = IO.unit
): IO[Unit] = IO.unit

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import ch.epfl.bluebrain.nexus.delta.sdk.permissions.model.Permission
import ch.epfl.bluebrain.nexus.delta.sdk.projects.FetchContext.ContextRejection
import ch.epfl.bluebrain.nexus.delta.sourcing.model.ProjectRef
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Tag.UserTag
import ch.epfl.bluebrain.nexus.delta.sourcing.rejection.Rejection
import com.typesafe.scalalogging.Logger
import io.circe.syntax._
import io.circe.{Encoder, JsonObject}
Expand All @@ -26,9 +27,7 @@ import io.circe.{Encoder, JsonObject}
* @param reason
* a descriptive message as to why the rejection occurred
*/
sealed abstract class StorageRejection(val reason: String, val loggedDetails: Option[String] = None)
extends Product
with Serializable
sealed abstract class StorageRejection(val reason: String, val loggedDetails: Option[String] = None) extends Rejection

object StorageRejection {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package ch.epfl.bluebrain.nexus.delta.sdk

import cats.effect.IO
import ch.epfl.bluebrain.nexus.delta.sdk.SIO.SIO
import ch.epfl.bluebrain.nexus.delta.sdk.error.ServiceError.ScopeInitializationFailed
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity.Subject
import ch.epfl.bluebrain.nexus.delta.sdk.organizations.model.Organization
import ch.epfl.bluebrain.nexus.delta.sdk.projects.model.Project
import monix.bio.{IO => BIO}
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity.Subject

/**
* Lifecycle hook for organization and project initialization. It's meant to be used for plugins to preconfigure an
* organization or project, like for example the creation of a default view or setting the appropriate permissions.
* Implementations should use a `many[ScopeInitialization]` binding such that all implementation are collected during
* the service bootstrapping.
*/
trait ScopeInitializationF[F[_]] {
trait ScopeInitialization {

/**
* The method is invoked synchronously during the organization creation for its immediate configuration.
Expand All @@ -27,7 +24,7 @@ trait ScopeInitializationF[F[_]] {
* @param subject
* the identity that was recorded for the creation of the organization
*/
def onOrganizationCreation(organization: Organization, subject: Subject): F[Unit]
def onOrganizationCreation(organization: Organization, subject: Subject): IO[Unit]

/**
* The method is invoked synchronously during the project creation for immediate configuration of the project.
Expand All @@ -40,13 +37,6 @@ trait ScopeInitializationF[F[_]] {
* @param subject
* the identity that was recorded for the creation of the project
*/
def onProjectCreation(project: Project, subject: Subject): F[Unit]
def onProjectCreation(project: Project, subject: Subject): IO[Unit]

}

object SIO {
type SIO[A] = BIO[ScopeInitializationFailed, A]
}

trait ScopeInitialization extends ScopeInitializationF[SIO]
trait CatsScopeInitialization extends ScopeInitializationF[IO]
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.encoder.JsonLdEncoder
import ch.epfl.bluebrain.nexus.delta.sdk.marshalling.HttpResponseFields
import ch.epfl.bluebrain.nexus.delta.sdk.permissions.model.Permission
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Label
import ch.epfl.bluebrain.nexus.delta.sourcing.rejection.Rejection
import io.circe.syntax._
import io.circe.{Encoder, JsonObject}

Expand All @@ -19,7 +20,7 @@ import io.circe.{Encoder, JsonObject}
* @param reason
* a descriptive message as to why the rejection occurred
*/
sealed abstract class AclRejection(val reason: String) extends Product with Serializable
sealed abstract class AclRejection(val reason: String) extends Rejection

object AclRejection {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.epfl.bluebrain.nexus.delta.sdk.organizations

import cats.effect.Clock
import ch.epfl.bluebrain.nexus.delta.kernel.effect.migration._
import ch.epfl.bluebrain.nexus.delta.kernel.kamon.KamonMetricComponent
import ch.epfl.bluebrain.nexus.delta.kernel.search.Pagination
import ch.epfl.bluebrain.nexus.delta.kernel.utils.UUIDF
Expand Down Expand Up @@ -30,7 +31,7 @@ final class OrganizationsImpl private (
)(implicit caller: Subject): IO[OrganizationRejection, OrganizationResource] =
for {
resource <- eval(CreateOrganization(label, description, caller)).span("createOrganization")
_ <- IO.parTraverseUnordered(scopeInitializations)(_.onOrganizationCreation(resource.value, caller))
_ <- IO.parTraverseUnordered(scopeInitializations)(_.onOrganizationCreation(resource.value, caller).toUIO)
.void
.mapError(OrganizationInitializationFailed)
.span("initializeOrganization")
Expand Down
Loading

0 comments on commit 90631e1

Please sign in to comment.