Skip to content

Commit

Permalink
wip - test admin API
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinovega committed Dec 20, 2023
1 parent 977ada1 commit f69e81e
Show file tree
Hide file tree
Showing 5 changed files with 2,720 additions and 70 deletions.
55 changes: 29 additions & 26 deletions daikoku/app/controllers/AdminApiController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -327,23 +327,25 @@ class ApiAdminApiController(daa: DaikokuApiAction,
override def validate(entity: Api): EitherT[Future, AppError, Api] = {
import cats.implicits._
for {
_ <- EitherT.fromOptionF[Future, AppError, Tenant](env.dataStore.tenantRepo.findById(entity.tenant), AppError.TenantNotFound)
_ <- entity.possibleUsagePlans.map(planId => EitherT.fromOptionF[Future, AppError, UsagePlan](env.dataStore.usagePlanRepo.forTenant(entity.tenant).findById(planId), AppError.EntityNotFound(s"Usage Plan (${planId.value})")))
_ <- EitherT.fromOptionF[Future, AppError, Tenant](env.dataStore.tenantRepo.findById(entity.tenant), AppError.ParsingPayloadError("Tenant not found"))
_ <- entity.possibleUsagePlans.map(planId => EitherT.fromOptionF[Future, AppError, UsagePlan](env.dataStore.usagePlanRepo.forTenant(entity.tenant).findById(planId), AppError.ParsingPayloadError(s"Usage Plan (${planId.value}) not found")))
.toList
.sequence
_ <- EitherT.cond[Future][AppError, Unit](entity.possibleUsagePlans.contains(entity.defaultUsagePlan), (), AppError.EntityNotFound(s"Default Usage Plan (${entity.defaultUsagePlan.value})"))
_ <- EitherT.fromOptionF[Future, AppError, Team](env.dataStore.teamRepo.forTenant(entity.tenant).findById(entity.team), AppError.TeamNotFound)
_ <- EitherT.fromOptionF[Future, AppError, Team](env.dataStore.teamRepo.forTenant(entity.tenant).findById(entity.team), AppError.TeamNotFound)
_ <- EitherT.fromOptionF[Future, AppError, Team](env.dataStore.teamRepo.forTenant(entity.tenant).findOne(Json.obj("_id" -> Json.obj("$ne" -> entity.id.asJson), "name" -> entity.name)), AppError.EntityConflict("Team name already exists"))
_ <- entity.documentation.pages.map(_.id).map(pageId => EitherT.fromOptionF[Future, AppError, ApiDocumentationPage](env.dataStore.apiDocumentationPageRepo.forTenant(entity.tenant).findById(pageId), AppError.EntityNotFound(s"Documentation page (${pageId.value})")))
_ <- EitherT.cond[Future][AppError, Unit](entity.possibleUsagePlans.contains(entity.defaultUsagePlan), (), AppError.ParsingPayloadError(s"Default Usage Plan (${entity.defaultUsagePlan.value}) not found"))
_ <- EitherT.fromOptionF[Future, AppError, Team](env.dataStore.teamRepo.forTenant(entity.tenant).findById(entity.team), AppError.ParsingPayloadError("Team not found"))
_ <- EitherT(env.dataStore.teamRepo.forTenant(entity.tenant).findOne(Json.obj("_id" -> Json.obj("$ne" -> entity.id.asJson), "name" -> entity.name)).map {
case Some(_) => Left(AppError.ParsingPayloadError("Api name already exists"))
case None => Right(())
})
_ <- entity.documentation.pages.map(_.id).map(pageId => EitherT.fromOptionF[Future, AppError, ApiDocumentationPage](env.dataStore.apiDocumentationPageRepo.forTenant(entity.tenant).findById(pageId), AppError.ParsingPayloadError(s"Documentation page (${pageId.value}) not found")))
.toList
.sequence
_ <- entity.parent match {
case Some(api) => EitherT.fromOptionF[Future, AppError, Api](env.dataStore.apiRepo.forTenant(entity.tenant).findById(api), AppError.EntityNotFound("parent API"))
case Some(api) => EitherT.fromOptionF[Future, AppError, Api](env.dataStore.apiRepo.forTenant(entity.tenant).findById(api), AppError.ParsingPayloadError("Parent API not found"))
case None => EitherT.pure[Future, AppError](())
}
_ <- entity.apis match {
case Some(apis) => apis.map(api => EitherT.fromOptionF[Future, AppError, Api](env.dataStore.apiRepo.forTenant(entity.tenant).findById(api), AppError.EntityNotFound(s"api as children (${api.value})")))
case Some(apis) => apis.map(api => EitherT.fromOptionF[Future, AppError, Api](env.dataStore.apiRepo.forTenant(entity.tenant).findById(api), AppError.ParsingPayloadError(s"Children API (${api.value}) not found")))
.toList
.sequence
case None => EitherT.pure[Future, AppError](Seq.empty[Api])
Expand Down Expand Up @@ -545,7 +547,7 @@ class MessagesAdminApiController(daa: DaikokuApiAction,
extends AdminApiController[Message, DatastoreId](daa, env, cc) {
override def entityClass = classOf[Message]
override def entityName: String = "message"
override def pathRoot: String = s"/admin-api/${entityName}s"
override def pathRoot: String = s"/admin-api/messages"
override def entityStore(tenant: Tenant,
ds: DataStore): Repo[Message, DatastoreId] =
ds.messageRepo.forTenant(tenant)
Expand All @@ -558,11 +560,12 @@ class MessagesAdminApiController(daa: DaikokuApiAction,

override def validate(entity: Message): EitherT[Future, AppError, Message] =
for {
_ <- EitherT.fromOptionF[Future, AppError, Tenant](env.dataStore.tenantRepo.findById(entity.tenant), AppError.TenantNotFound)
_ <- EitherT.fromOptionF[Future, AppError, User](env.dataStore.userRepo.findById(entity.sender), AppError.EntityNotFound(s"sender (${entity.sender.value}"))
_ <- entity.participants.map(u => EitherT.fromOptionF[Future, AppError, User](env.dataStore.userRepo.findById(u), AppError.EntityNotFound(s"participant (${u.value})")))
_ <- EitherT.fromOptionF[Future, AppError, Tenant](env.dataStore.tenantRepo.findById(entity.tenant), AppError.ParsingPayloadError("Tenant not found"))
_ <- EitherT.fromOptionF[Future, AppError, User](env.dataStore.userRepo.findById(entity.sender), AppError.ParsingPayloadError(s"Sender (${entity.sender.value}) not found"))
_ <- entity.participants.map(u => EitherT.fromOptionF[Future, AppError, User](env.dataStore.userRepo.findById(u), AppError.ParsingPayloadError(s"Participant (${u.value}) not found")))
.toList
.sequence
_ <- EitherT.cond[Future][AppError, Unit](entity.participants.contains(entity.sender), (), AppError.ParsingPayloadError("Sender must included in participants"))
} yield entity

override def getId(entity: Message): DatastoreId = entity.id
Expand All @@ -587,8 +590,8 @@ class IssuesAdminApiController(daa: DaikokuApiAction,

override def validate(entity: ApiIssue): EitherT[Future, AppError, ApiIssue] =
for {
_ <- EitherT.fromOptionF[Future, AppError, Tenant](env.dataStore.tenantRepo.findById(entity.tenant), AppError.TenantNotFound)
_ <- EitherT.fromOptionF[Future, AppError, User](env.dataStore.userRepo.findById(entity.by), AppError.UserNotFound)
_ <- EitherT.fromOptionF[Future, AppError, Tenant](env.dataStore.tenantRepo.findById(entity.tenant), AppError.ParsingPayloadError("Tenant not found"))
_ <- EitherT.fromOptionF[Future, AppError, User](env.dataStore.userRepo.findById(entity.by), AppError.ParsingPayloadError("By not found"))
} yield entity

override def getId(entity: ApiIssue): ApiIssueId = entity.id
Expand All @@ -613,7 +616,7 @@ class PostsAdminApiController(daa: DaikokuApiAction,

override def validate(entity: ApiPost): EitherT[Future, AppError, ApiPost] =
for {
_ <- EitherT.fromOptionF[Future, AppError, Tenant](env.dataStore.tenantRepo.findById(entity.tenant), AppError.TenantNotFound)
_ <- EitherT.fromOptionF[Future, AppError, Tenant](env.dataStore.tenantRepo.findById(entity.tenant), AppError.ParsingPayloadError("Tenant not found"))
} yield entity

override def getId(entity: ApiPost): ApiPostId = entity.id
Expand All @@ -638,7 +641,7 @@ class CmsPagesAdminApiController(daa: DaikokuApiAction,

override def validate(entity: CmsPage): EitherT[Future, AppError, CmsPage] =
for {
_ <- EitherT.fromOptionF[Future, AppError, Tenant](env.dataStore.tenantRepo.findById(entity.tenant), AppError.TenantNotFound)
_ <- EitherT.fromOptionF[Future, AppError, Tenant](env.dataStore.tenantRepo.findById(entity.tenant), AppError.ParsingPayloadError("Tenant not found"))
} yield entity

override def getId(entity: CmsPage): CmsPageId = entity.id
Expand All @@ -663,7 +666,7 @@ class TranslationsAdminApiController(daa: DaikokuApiAction,

override def validate(entity: Translation): EitherT[Future, AppError, Translation] =
for {
_ <- EitherT.fromOptionF[Future, AppError, Tenant](env.dataStore.tenantRepo.findById(entity.tenant), AppError.TenantNotFound)
_ <- EitherT.fromOptionF[Future, AppError, Tenant](env.dataStore.tenantRepo.findById(entity.tenant), AppError.ParsingPayloadError("Tenant not found"))
} yield entity

override def getId(entity: Translation): DatastoreId = entity.id
Expand All @@ -688,13 +691,13 @@ class UsagePlansAdminApiController(daa: DaikokuApiAction,

override def validate(entity: UsagePlan): EitherT[Future, AppError, UsagePlan] =
for {
tenant <- EitherT.fromOptionF[Future, AppError, Tenant](env.dataStore.tenantRepo.findById(entity.tenant), AppError.TenantNotFound)
tenant <- EitherT.fromOptionF[Future, AppError, Tenant](env.dataStore.tenantRepo.findById(entity.tenant), AppError.ParsingPayloadError("Tenant not found"))
_ <- entity.otoroshiTarget match {
case Some(target) => EitherT.cond[Future][AppError, Unit](tenant.otoroshiSettings.map(_.id).contains(target.otoroshiSettings), (), AppError.EntityNotFound(s"Otoroshi setting (${target.otoroshiSettings.value})"))
case Some(target) => EitherT.cond[Future][AppError, Unit](tenant.otoroshiSettings.map(_.id).contains(target.otoroshiSettings), (), AppError.ParsingPayloadError(s"Otoroshi setting not found"))
case None => EitherT.pure[Future, AppError](())
}
_ <- entity.paymentSettings match {
case Some(target) => EitherT.cond[Future][AppError, Unit](tenant.thirdPartyPaymentSettings.map(_.id).contains(target.thirdPartyPaymentSettingsId), (), AppError.EntityNotFound(s"Otororoshi setting (${target.thirdPartyPaymentSettingsId.value})"))
case Some(target) => EitherT.cond[Future][AppError, Unit](tenant.thirdPartyPaymentSettings.map(_.id).contains(target.thirdPartyPaymentSettingsId), (), AppError.ParsingPayloadError(s"Payment setting not found"))
case None => EitherT.pure[Future, AppError](())
}
} yield entity
Expand Down Expand Up @@ -724,11 +727,11 @@ class SubscriptionDemandsAdminApiController(daa: DaikokuApiAction,

override def validate(entity: SubscriptionDemand): EitherT[Future, AppError, SubscriptionDemand] =
for {
_ <- EitherT.fromOptionF[Future, AppError, Tenant](env.dataStore.tenantRepo.findById(entity.tenant), AppError.TenantNotFound)
_ <- EitherT.fromOptionF[Future, AppError, Api](env.dataStore.apiRepo.forTenant(entity.tenant).findById(entity.api), AppError.ApiNotFound)
_ <- EitherT.fromOptionF[Future, AppError, UsagePlan](env.dataStore.usagePlanRepo.forTenant(entity.tenant).findById(entity.plan), AppError.PlanNotFound)
_ <- EitherT.fromOptionF[Future, AppError, Team](env.dataStore.teamRepo.forTenant(entity.tenant).findById(entity.team), AppError.TeamNotFound)
_ <- EitherT.fromOptionF[Future, AppError, User](env.dataStore.userRepo.findById(entity.from), AppError.UserNotFound)
_ <- EitherT.fromOptionF[Future, AppError, Tenant](env.dataStore.tenantRepo.findById(entity.tenant), AppError.ParsingPayloadError("Tenant not found"))
_ <- EitherT.fromOptionF[Future, AppError, Api](env.dataStore.apiRepo.forTenant(entity.tenant).findById(entity.api), AppError.ParsingPayloadError("Api not found"))
_ <- EitherT.fromOptionF[Future, AppError, UsagePlan](env.dataStore.usagePlanRepo.forTenant(entity.tenant).findById(entity.plan), AppError.ParsingPayloadError("Plan not found"))
_ <- EitherT.fromOptionF[Future, AppError, Team](env.dataStore.teamRepo.forTenant(entity.tenant).findById(entity.team), AppError.ParsingPayloadError("Team not found"))
_ <- EitherT.fromOptionF[Future, AppError, User](env.dataStore.userRepo.findById(entity.from), AppError.ParsingPayloadError("From not found"))
} yield entity

override def getId(entity: SubscriptionDemand): SubscriptionDemandId = entity.id
Expand Down
12 changes: 6 additions & 6 deletions daikoku/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,12 @@ GET /admin-api/audit-events/:id fr.maif.otoroshi.daikoku
POST /admin-api/audit-events fr.maif.otoroshi.daikoku.ctrls.AuditEventAdminApiController.createEntity()
GET /admin-api/audit-events fr.maif.otoroshi.daikoku.ctrls.AuditEventAdminApiController.findAll()

DELETE /admin-api/message/:id fr.maif.otoroshi.daikoku.ctrls.MessagesAdminApiController.deleteEntity(id)
PATCH /admin-api/message/:id fr.maif.otoroshi.daikoku.ctrls.MessagesAdminApiController.patchEntity(id)
PUT /admin-api/message/:id fr.maif.otoroshi.daikoku.ctrls.MessagesAdminApiController.updateEntity(id)
GET /admin-api/message/:id fr.maif.otoroshi.daikoku.ctrls.MessagesAdminApiController.findById(id)
POST /admin-api/message fr.maif.otoroshi.daikoku.ctrls.MessagesAdminApiController.createEntity()
GET /admin-api/message fr.maif.otoroshi.daikoku.ctrls.MessagesAdminApiController.findAll()
DELETE /admin-api/messages/:id fr.maif.otoroshi.daikoku.ctrls.MessagesAdminApiController.deleteEntity(id)
PATCH /admin-api/messages/:id fr.maif.otoroshi.daikoku.ctrls.MessagesAdminApiController.patchEntity(id)
PUT /admin-api/messages/:id fr.maif.otoroshi.daikoku.ctrls.MessagesAdminApiController.updateEntity(id)
GET /admin-api/messages/:id fr.maif.otoroshi.daikoku.ctrls.MessagesAdminApiController.findById(id)
POST /admin-api/messages fr.maif.otoroshi.daikoku.ctrls.MessagesAdminApiController.createEntity()
GET /admin-api/messages fr.maif.otoroshi.daikoku.ctrls.MessagesAdminApiController.findAll()

DELETE /admin-api/posts/:id fr.maif.otoroshi.daikoku.ctrls.PostsAdminApiController.deleteEntity(id)
PATCH /admin-api/posts/:id fr.maif.otoroshi.daikoku.ctrls.PostsAdminApiController.patchEntity(id)
Expand Down
20 changes: 4 additions & 16 deletions daikoku/public/swaggers/admin-api-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2531,14 +2531,8 @@ paths:
- $ref: '#/components/schemas/patch'
- $ref: '#/components/schemas/Tenant'
responses:
'200':
description: updated entity
content:
application/json:
schema:
type: object
items:
$ref: '#/components/schemas/Tenant'
'204':
description: No Content
'401':
description: unauthorized
content:
Expand Down Expand Up @@ -2571,14 +2565,8 @@ paths:
schema:
$ref: '#/components/schemas/Tenant'
responses:
'200':
description: updated entity
content:
application/json:
schema:
type: object
items:
$ref: '#/components/schemas/Tenant'
'204':
description: No Content
'401':
description: unauthorized
content:
Expand Down
Loading

0 comments on commit f69e81e

Please sign in to comment.