-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
2,843 additions
and
820 deletions.
There are no files selected for viewing
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
450 changes: 450 additions & 0 deletions
450
app/fr/maif/izanami/datastores/ImportExportDatastore.scala
Large diffs are not rendered by default.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package fr.maif.izanami.models | ||
|
||
sealed trait ExportedType { | ||
def order: Int | ||
def table: String | ||
def displayName: String | ||
} | ||
case object ScriptType extends ExportedType { | ||
override def order: Int = 0 | ||
override def table: String = "wasm_script_configurations" | ||
override def displayName: String = "WASM Script" | ||
} | ||
case object ProjectType extends ExportedType { | ||
override def order: Int = 1 | ||
override def table: String = "projects" | ||
override def displayName: String = "Project" | ||
} | ||
case object KeyType extends ExportedType { | ||
override def order: Int = 2 | ||
override def table: String = "apikeys" | ||
override def displayName: String = "Key" | ||
} | ||
case object KeyProjectType extends ExportedType { | ||
override def order: Int = 3 | ||
override def table: String = "apikeys_projects" | ||
override def displayName: String = "Key right on project" | ||
} | ||
case object GlobalContextType extends ExportedType { | ||
override def order: Int = 4 | ||
override def table: String = "global_feature_contexts" | ||
override def displayName: String = "Global context" | ||
} | ||
case object LocalContextType extends ExportedType { | ||
override def order: Int = 5 | ||
override def table: String = "feature_contexts" | ||
override def displayName: String = "Local context" | ||
} | ||
case object WebhookType extends ExportedType { | ||
override def order: Int = 6 | ||
override def table: String = "webhooks" | ||
override def displayName: String = "Webhook" | ||
} | ||
case object TagType extends ExportedType { | ||
override def order: Int = 7 | ||
override def table: String = "tags" | ||
override def displayName: String = "Tag" | ||
} | ||
case object FeatureType extends ExportedType { | ||
override def order: Int = 8 | ||
override def table: String = "features" | ||
override def displayName: String = "Feature" | ||
} | ||
case object FeatureTagType extends ExportedType { | ||
override def order: Int = 9 | ||
override def table: String = "features_tags" | ||
override def displayName: String = "Feature tags link" | ||
} | ||
case object OverloadType extends ExportedType { | ||
override def order: Int = 10 | ||
override def table: String = "feature_contexts_strategies" | ||
override def displayName: String = "Feature overload" | ||
} | ||
case object ProjectRightType extends ExportedType { | ||
override def order: Int = 11 | ||
override def table: String = "users_projects_rights" | ||
override def displayName: String = "User right on project" | ||
} | ||
case object KeyRightType extends ExportedType { | ||
override def order: Int = 12 | ||
|
||
override def table: String = "users_keys_rights" | ||
override def displayName: String = "User right on key" | ||
} | ||
case object WebhookRightType extends ExportedType { | ||
override def order: Int = 13 | ||
|
||
override def table: String = "users_webhooks_rights" | ||
override def displayName: String = "User right on webhook" | ||
} | ||
case object WebhookFeatureType extends ExportedType { | ||
override def order: Int = 14 | ||
|
||
override def table: String = "webhooks_features" | ||
override def displayName: String = "Webhook feature link" | ||
} | ||
case object WebhookProjectType extends ExportedType { | ||
override def order: Int = 15 | ||
override def table: String = "webhooks_projects" | ||
override def displayName: String = "Webhook project link" | ||
} | ||
|
||
object ExportedType { | ||
val exportedTypeToExportedNameAssociation: Seq[(String, ExportedType)] = Seq( | ||
("project", ProjectType), | ||
("feature", FeatureType), | ||
("tag", TagType), | ||
("feature_tag", FeatureTagType), | ||
("overload", OverloadType), | ||
("local_context", LocalContextType), | ||
("global_context", GlobalContextType), | ||
("key", KeyType), | ||
("apikey_project", KeyProjectType), | ||
("webhook", WebhookType), | ||
("webhook_feature", WebhookFeatureType), | ||
("webhook_project", WebhookProjectType), | ||
("user_webhook_right", WebhookRightType), | ||
("project_right", ProjectRightType), | ||
("key_right", KeyRightType), | ||
("script", ScriptType) | ||
) | ||
|
||
def parseExportedType(typestr: String): Option[ExportedType] = { | ||
exportedTypeToExportedNameAssociation.find(t => t._1 == typestr).map(t => t._2) | ||
} | ||
|
||
def exportedTypeToString(exportedType: ExportedType): Option[String] = { | ||
exportedTypeToExportedNameAssociation.find(t => t._2 == exportedType).map(t => t._1) | ||
} | ||
} |
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,38 @@ | ||
package fr.maif.izanami.utils | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
object Helpers { | ||
def sequence[A, B](seq: Seq[Either[A, B]]): Either[A, Seq[B]] = seq.foldRight(Right(Nil): Either[A, List[B]]) { | ||
(e, acc) => for (xs <- acc; x <- e) yield x :: xs | ||
} | ||
|
||
def sequence[A, B, C](seq: Seq[Either[A, B]], init: => C, acc: (C, A) => C): Either[C, Seq[B]] = | ||
seq.foldRight(Right(Nil): Either[C, List[B]]) { | ||
case (Left(err), Left(errAcc)) => Left(acc(errAcc, err)) | ||
case (Left(err), Right(_)) => Left(acc(init, err)) | ||
case (Right(v), Right(vs)) => Right(v :: vs) | ||
case (Right(_), Left(errAcc)) => Left(errAcc) | ||
} | ||
|
||
def chainFutures[E, R]( | ||
futures: Seq[Future[Either[E, R]]] | ||
)(implicit ec: ExecutionContext): Future[Either[Seq[E], Seq[R]]] = { | ||
def act(fs: Seq[Future[Either[E, R]]], res: Either[Seq[E], Seq[R]]): Future[Either[Seq[E], Seq[R]]] = { | ||
fs.headOption | ||
.map(f => { | ||
f.map(e => | ||
(res, e) match { | ||
case (Left(errs), Left(err)) => Left(errs.appended(err)) | ||
case (_, Left(err)) => Left(Seq(err)) | ||
case (Left(errs), _) => Left(errs) | ||
case (Right(rs), Right(r)) => Right(rs.appended(r)) | ||
} | ||
).flatMap(e => act(fs.tail, e)) | ||
}) | ||
.getOrElse(Future.successful(res)) | ||
} | ||
|
||
act(futures, Right(Seq())) | ||
} | ||
} |
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,111 @@ | ||
package fr.maif.izanami.web | ||
|
||
import akka.util.ByteString | ||
import fr.maif.izanami.env.Env | ||
import fr.maif.izanami.models.Feature.lightweightFeatureWrite | ||
import fr.maif.izanami.models.{LightWeightFeature, RightLevels} | ||
import play.api.http.HttpEntity | ||
import play.api.libs.json._ | ||
import play.api.mvc._ | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
class ExportController( | ||
val env: Env, | ||
val controllerComponents: ControllerComponents, | ||
val authAction: TenantAuthActionFactory | ||
) extends BaseController { | ||
implicit val ec: ExecutionContext = env.executionContext | ||
|
||
def exportTenantData(tenant: String): Action[JsValue] = authAction(tenant, RightLevels.Admin).async(parse.json) { | ||
implicit request => | ||
{ | ||
ExportController.tenantExportRequestReads | ||
.reads(request.body) | ||
.asEither | ||
.map(req => { | ||
env.datastores.exportDatastore.exportTenantData(tenant, req) | ||
}) | ||
.fold( | ||
_ => Future.successful(BadRequest(Json.obj("message" -> "Bad body format"))), | ||
futureResult => | ||
futureResult.map(jsons => { | ||
Result( | ||
header = | ||
ResponseHeader(200, Map("Content-Disposition" -> "attachment", "filename" -> "export.ndjson")), | ||
body = HttpEntity.Streamed( | ||
akka.stream.scaladsl.Source.single(ByteString(jsons.mkString("\n"), "UTF-8")), | ||
None, | ||
Some("application/x-ndjson") | ||
) | ||
) | ||
}) | ||
) | ||
} | ||
} | ||
} | ||
|
||
object ExportController { | ||
val exportRequestReads: Reads[ExportRequest] = json => { | ||
(json \ "tenants") | ||
.asOpt[Map[String, TenantExportRequest]](Reads.map(tenantExportRequestReads)) | ||
.map(projects => JsSuccess(ExportRequest(projects))) | ||
.getOrElse(JsError("Bad body format")) | ||
} | ||
|
||
val tenantExportRequestReads: Reads[TenantExportRequest] = json => { | ||
(for ( | ||
projects: ExportList <- (json \ "allProjects") | ||
.asOpt[Boolean] | ||
.flatMap(isAllProjects => | ||
if (isAllProjects) Some(ExportAllItems) | ||
else (json \ "projects").asOpt[Set[String]].map(set => ExportItemList(set)) | ||
); | ||
keys: ExportList <- (json \ "allKeys") | ||
.asOpt[Boolean] | ||
.flatMap(isAllProjects => | ||
if (isAllProjects) Some(ExportAllItems) | ||
else (json \ "keys").asOpt[Set[String]].map(set => ExportItemList(set)) | ||
); | ||
webhooks: ExportList <- (json \ "allWebhooks") | ||
.asOpt[Boolean] | ||
.flatMap(isAllProjects => | ||
if (isAllProjects) Some(ExportAllItems) | ||
else (json \ "webhooks").asOpt[Set[String]].map(set => ExportItemList(set)) | ||
); | ||
userRights <- (json \ "userRights").asOpt[Boolean] | ||
) yield JsSuccess(TenantExportRequest(projects, keys, webhooks, userRights))).getOrElse(JsError("Bad body format")) | ||
} | ||
|
||
val exportResultWrites: Writes[ExportResult] = exportResult => { | ||
Json.obj( | ||
"tenants" -> Json.toJson(exportResult.tenants)(Writes.map(tenantExportResultWrites)) | ||
) | ||
} | ||
|
||
val tenantExportResultWrites: Writes[TenantExportResult] = exportResult => { | ||
Json.obj("projects" -> Json.toJson(exportResult.projects)(Writes.map(projectExportResultWrites))) | ||
} | ||
|
||
val projectExportResultWrites: Writes[ProjectExportResult] = exportResult => { | ||
Json.obj("features" -> Json.toJson(exportResult.features)(Writes.list(lightweightFeatureWrite))) | ||
} | ||
|
||
case class ExportRequest(tenants: Map[String, TenantExportRequest]) {} | ||
case class TenantExportRequest(projects: ExportList, keys: ExportList, webhooks: ExportList, userRights: Boolean) | ||
|
||
sealed trait ExportList | ||
case object ExportAllItems extends ExportList | ||
case class ExportItemList(items: Set[String]) extends ExportList | ||
|
||
case class ExportResult( | ||
tenants: Map[String, TenantExportResult] | ||
) | ||
|
||
case class TenantExportResult( | ||
projects: Map[String, ProjectExportResult] | ||
) | ||
|
||
case class ProjectExportResult(features: List[LightWeightFeature]) | ||
|
||
} |
Oops, something went wrong.