Skip to content

Commit

Permalink
create cms API in all case of tenant creation
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinovega committed Oct 31, 2024
1 parent 8cf1219 commit 6bec273
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 158 deletions.
51 changes: 13 additions & 38 deletions daikoku/app/controllers/TenantController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -236,55 +236,30 @@ class TenantController(
contact = tenant.contact,
apisCreationPermission = true.some
)
val adminApiPlan = FreeWithoutQuotas(
id = UsagePlanId(IdGenerator.token),
tenant = tenant.id,
billingDuration = BillingDuration(1, BillingTimeUnit.Month),
currency = Currency("EUR"),
customName = Some("admin"),
customDescription = None,
otoroshiTarget = None,
allowMultipleKeys = Some(true),
autoRotation = None,
subscriptionProcess = Seq.empty,
integrationProcess = IntegrationProcess.ApiKey
)
val (adminApi, adminApiPlan) = ApiTemplate.adminApi(adminTeam, tenant)

val adminApi = Api(
id = ApiId(s"admin-api-tenant-${tenant.humanReadableId}"),
tenant = tenant.id,
team = adminTeam.id,
name = s"admin-api-tenant-${tenant.humanReadableId}",
lastUpdate = DateTime.now(),
smallDescription = "admin api",
description = "admin api",
currentVersion = Version("1.0.0"),
state = ApiState.Published,
visibility = ApiVisibility.AdminOnly,
documentation = ApiDocumentation(
id = ApiDocumentationId(IdGenerator.token(32)),
tenant = tenant.id,
pages = Seq.empty[ApiDocumentationDetailPage],
lastModificationAt = DateTime.now()
),
swagger =
Some(SwaggerAccess(url = "/admin-api/swagger.json".some)),
possibleUsagePlans = Seq(adminApiPlan.id),
defaultUsagePlan = UsagePlanId("admin").some,
authorizedTeams = Seq.empty
)
val tenantForCreation = tenant.copy(adminApi = adminApi.id)

val (cmsApi, cmsPlan) = ApiTemplate.cmsApi(adminTeam, tenant)

for {
_ <- env.dataStore.tenantRepo.save(tenantForCreation)
_ <-
env.dataStore.teamRepo
.forTenant(tenantForCreation)
.save(adminTeam)
_ <-
env.dataStore.apiRepo
_ <- env.dataStore.apiRepo
.forTenant(tenantForCreation)
.save(adminApi)
_ <- env.dataStore.apiRepo
.forTenant(tenantForCreation)
.save(cmsApi)
_ <- env.dataStore.usagePlanRepo
.forTenant(tenantForCreation)
.save(cmsPlan)
_ <- env.dataStore.usagePlanRepo
.forTenant(tenantForCreation)
.save(adminApiPlan)
} yield {
Created(tenantForCreation.asJsonWithJwt)
}
Expand Down
88 changes: 87 additions & 1 deletion daikoku/app/domain/apiEntities.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.apache.pekko.http.scaladsl.util.FastFuture
import cats.data.EitherT
import cats.syntax.option._
import controllers.AppError
import fr.maif.otoroshi.daikoku.domain.UsagePlan.FreeWithoutQuotas
import fr.maif.otoroshi.daikoku.domain.json.{
SeqIssueIdFormat,
SeqPostIdFormat,
Expand All @@ -12,7 +13,7 @@ import fr.maif.otoroshi.daikoku.domain.json.{
UsagePlanFormat
}
import fr.maif.otoroshi.daikoku.env.Env
import fr.maif.otoroshi.daikoku.utils.ReplaceAllWith
import fr.maif.otoroshi.daikoku.utils.{IdGenerator, ReplaceAllWith}
import fr.maif.otoroshi.daikoku.utils.StringImplicits.BetterString
import org.joda.time.{DateTime, Days}
import play.api.libs.json._
Expand Down Expand Up @@ -1189,3 +1190,88 @@ object ValidationStep {
override def isAutomatic: Boolean = true
}
}

object ApiTemplate {
def cmsApi(team: Team, tenant: Tenant): (Api, UsagePlan) = {
val plan = FreeWithoutQuotas(
id = UsagePlanId(IdGenerator.token),
tenant = tenant.id,
billingDuration = BillingDuration(1, BillingTimeUnit.Month),
currency = Currency("EUR"),
customName = Some("admin"),
customDescription = Some("Access to cms API"),
otoroshiTarget = None,
allowMultipleKeys = Some(true),
autoRotation = None,
integrationProcess = IntegrationProcess.ApiKey
)
val api = Api(
id = ApiId(IdGenerator.token),
tenant = tenant.id,
team = team.id,
name = s"cms-api-tenant-${tenant.id.value}",
lastUpdate = DateTime.now(),
smallDescription = "cms api",
description =
"@@@ warning \nThis API is dedicated to Daikoku's CMS CLI and is not intended for direct end-user access.\n@@@\n\nTo obtain the API key needed for CLI configuration, please subscribe to this API.\n\nThe complete documentation is available __[here](https://maif.github.io/daikoku/docs/cli)__.",
currentVersion = Version("1.0.0"),
documentation = ApiDocumentation(
id = ApiDocumentationId(IdGenerator.token(32)),
tenant = tenant.id,
pages = Seq.empty[ApiDocumentationDetailPage],
lastModificationAt = DateTime.now()
),
swagger = Some(SwaggerAccess(url = "/cms-api/swagger.json".some)),
possibleUsagePlans = Seq(plan.id),
visibility = ApiVisibility.AdminOnly,
defaultUsagePlan = plan.id.some,
authorizedTeams = Seq.empty,
state = ApiState.Published,
tags = Set("cms"),
categories = Set("administration")
)
(api, plan)
}

def adminApi(team: Team, tenant: Tenant): (Api, UsagePlan) = {
val plan = FreeWithoutQuotas(
id = UsagePlanId(IdGenerator.token),
tenant = tenant.id,
billingDuration = BillingDuration(1, BillingTimeUnit.Month),
currency = Currency("EUR"),
customName = Some("admin"),
customDescription = Some("admin API access"),
otoroshiTarget = None,
allowMultipleKeys = Some(true),
autoRotation = None,
integrationProcess = IntegrationProcess.ApiKey
)

val api = Api(
id = ApiId(s"admin-api-tenant-${tenant.humanReadableId}"),
tenant = tenant.id,
team = team.id,
name = s"admin-api-tenant-${tenant.humanReadableId}",
lastUpdate = DateTime.now(),
smallDescription = "admin api",
description =
"@@@ warning \nThis API is reserved for Daikoku administration and is not intended for direct end-user access.\n@@@\n\nTo obtain the API key needed for admin API, please subscribe to this API.\n\nThe complete openAPI is available __[here](https://maif.github.io/daikoku/openapi)__.",
currentVersion = Version("1.0.0"),
state = ApiState.Published,
visibility = ApiVisibility.AdminOnly,
documentation = ApiDocumentation(
id = ApiDocumentationId(IdGenerator.token(32)),
tenant = tenant.id,
pages = Seq.empty[ApiDocumentationDetailPage],
lastModificationAt = DateTime.now()
),
swagger = Some(SwaggerAccess(url = "/admin-api/swagger.json".some)),
possibleUsagePlans = Seq(plan.id),
defaultUsagePlan = UsagePlanId("admin").some,
authorizedTeams = Seq.empty,
categories = Set("administration")
)

(api, plan)
}
}
84 changes: 6 additions & 78 deletions daikoku/app/env/env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,6 @@ class DaikokuEnv(
"Main dataStore seems to be empty, generating initial data ..."
)
val userId = UserId(IdGenerator.token(32))
val administrationTeamId = TeamId("administration")
val adminApiDefaultTenantId =
ApiId(s"admin-api-tenant-${Tenant.Default.value}")
val cmsApiDefaultTenantId =
Expand All @@ -481,81 +480,6 @@ class DaikokuEnv(
authorizedOtoroshiEntities = None,
contact = "no-replay@daikoku.io"
)
val adminApiDefaultPlan = FreeWithoutQuotas(
id = UsagePlanId(IdGenerator.token),
tenant = Tenant.Default,
billingDuration = BillingDuration(1, BillingTimeUnit.Month),
currency = Currency("EUR"),
customName = Some("admin"),
customDescription = None,
otoroshiTarget = None,
allowMultipleKeys = Some(true),
autoRotation = None,
subscriptionProcess = Seq.empty,
integrationProcess = IntegrationProcess.ApiKey
)

val adminApiDefaultTenant = Api(
id = adminApiDefaultTenantId,
tenant = Tenant.Default,
team = defaultAdminTeam.id,
name = s"admin-api-tenant-${Tenant.Default.value}",
lastUpdate = DateTime.now(),
smallDescription = "admin api",
description = "admin api",
currentVersion = Version("1.0.0"),
documentation = ApiDocumentation(
id = ApiDocumentationId(IdGenerator.token(32)),
tenant = Tenant.Default,
pages = Seq.empty[ApiDocumentationDetailPage],
lastModificationAt = DateTime.now()
),
swagger =
Some(SwaggerAccess(url = "/admin-api/swagger.json".some)),
possibleUsagePlans = Seq(adminApiDefaultPlan.id),
visibility = ApiVisibility.AdminOnly,
defaultUsagePlan = adminApiDefaultPlan.id.some,
authorizedTeams = Seq.empty,
state = ApiState.Published
)

val cmsApiDefaultPlan = FreeWithoutQuotas(
id = UsagePlanId(IdGenerator.token),
tenant = Tenant.Default,
billingDuration = BillingDuration(1, BillingTimeUnit.Month),
currency = Currency("EUR"),
customName = Some("admin"),
customDescription = None,
otoroshiTarget = None,
allowMultipleKeys = Some(true),
autoRotation = None,
subscriptionProcess = Seq.empty,
integrationProcess = IntegrationProcess.ApiKey
)

val cmsApiDefaultTenant = Api(
id = cmsApiDefaultTenantId,
tenant = Tenant.Default,
team = defaultAdminTeam.id,
name = s"cms-api-tenant-${Tenant.Default.value}",
lastUpdate = DateTime.now(),
smallDescription = "cms api",
description = "cms api",
currentVersion = Version("1.0.0"),
documentation = ApiDocumentation(
id = ApiDocumentationId(IdGenerator.token(32)),
tenant = Tenant.Default,
pages = Seq.empty[ApiDocumentationDetailPage],
lastModificationAt = DateTime.now()
),
swagger =
Some(SwaggerAccess(url = "/cms-api/swagger.json".some)),
possibleUsagePlans = Seq(cmsApiDefaultPlan.id),
visibility = ApiVisibility.AdminOnly,
defaultUsagePlan = cmsApiDefaultPlan.id.some,
authorizedTeams = Seq.empty,
state = ApiState.Published
)

val tenant = Tenant(
id = Tenant.Default,
Expand All @@ -577,6 +501,10 @@ class DaikokuEnv(
otoroshiSettings = Set(),
adminApi = adminApiDefaultTenantId
)

val (adminApiDefaultTenant, adminApiDefaultPlan) = ApiTemplate.adminApi(defaultAdminTeam, tenant)
val (cmsApi, cmsPlan) = ApiTemplate.cmsApi(defaultAdminTeam, tenant)

val team = Team(
id = TeamId(IdGenerator.token(32)),
tenant = tenant.id,
Expand Down Expand Up @@ -631,11 +559,11 @@ class DaikokuEnv(
_ <-
dataStore.apiRepo
.forTenant(tenant.id)
.save(cmsApiDefaultTenant)
.save(cmsApi)
_ <-
dataStore.usagePlanRepo
.forTenant(tenant.id)
.save(cmsApiDefaultPlan)
.save(cmsPlan)
_ <- dataStore.userRepo.save(user)
} yield ()

Expand Down
46 changes: 5 additions & 41 deletions daikoku/app/env/evolutions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1108,61 +1108,25 @@ object evolution_1750 extends EvolutionScript {

implicit val executionContext: ExecutionContext = ec

val cmsApiDefaultPlan = FreeWithoutQuotas(
id = UsagePlanId(IdGenerator.token),
tenant = Tenant.Default,
billingDuration = BillingDuration(1, BillingTimeUnit.Month),
currency = Currency("EUR"),
customName = Some("admin"),
customDescription = None,
otoroshiTarget = None,
allowMultipleKeys = Some(true),
autoRotation = None,
subscriptionProcess = Seq.empty,
integrationProcess = IntegrationProcess.ApiKey
)

val cmsApiDefaultTenantId =
ApiId(s"cms-api-tenant-${Tenant.Default.value}")

for {
tenants <-dataStore.tenantRepo.findAll()
tenants <- dataStore.tenantRepo.findAll()
_ <- Future.sequence(tenants.map(tenant => dataStore.teamRepo
.forTenant(tenant)
.findOne(Json.obj("type" -> TeamType.Admin.name))
.flatMap(team => {
if(team.isDefined) {
val cmsApiDefaultTenant = Api(
id = cmsApiDefaultTenantId,
tenant = Tenant.Default,
team = team.get.id,
name = s"cms-api-tenant-${Tenant.Default.value}",
lastUpdate = DateTime.now(),
smallDescription = "cms api",
description = "cms api",
currentVersion = Version("1.0.0"),
documentation = ApiDocumentation(
id = ApiDocumentationId(IdGenerator.token(32)),
tenant = Tenant.Default,
pages = Seq.empty[ApiDocumentationDetailPage],
lastModificationAt = DateTime.now()
),
swagger =
Some(SwaggerAccess(url = "/cms-api/swagger.json".some)),
possibleUsagePlans = Seq(cmsApiDefaultPlan.id),
visibility = ApiVisibility.AdminOnly,
defaultUsagePlan = cmsApiDefaultPlan.id.some,
authorizedTeams = Seq.empty,
state = ApiState.Published
)
if (team.isDefined) {
val (cmsApi, cmsPlan) = ApiTemplate.cmsApi(team.get, tenant)

Future.sequence(Seq(
dataStore.apiRepo
.forTenant(tenant.id)
.save(cmsApiDefaultTenant),
.save(cmsApi),
dataStore.usagePlanRepo
.forTenant(tenant.id)
.save(cmsApiDefaultPlan)
.save(cmsPlan)
))
} else {
Future.successful(())
Expand Down

0 comments on commit 6bec273

Please sign in to comment.