Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Jan 15, 2025
1 parent 436b989 commit 4a58f94
Show file tree
Hide file tree
Showing 19 changed files with 783 additions and 130 deletions.
24 changes: 23 additions & 1 deletion otoroshi/app/api/api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package otoroshi.api
import akka.http.scaladsl.util.FastFuture
import akka.stream.scaladsl.{Framing, Source}
import akka.util.ByteString
import next.models.Api
import org.apache.commons.lang3.math.NumberUtils
import org.joda.time.DateTime
import otoroshi.actions.{ApiAction, ApiActionContext}
Expand Down Expand Up @@ -858,6 +859,26 @@ class OtoroshiResources(env: Env) {
stateOne = id => env.proxyState.draft(id),
stateUpdate = seq => env.proxyState.updateDrafts(seq)
)
),
//////
Resource(
"Api",
"apis",
"api",
"apis.otoroshi.io",
ResourceVersion("v1", true, false, true),
GenericResourceAccessApiWithState[Api](
Api.format,
classOf[Api],
env.datastores.apiDataStore.key,
env.datastores.apiDataStore.extractId,
json => json.select("id").asString,
() => "id",
(_v, _p) => env.datastores.apiDataStore.template(env).json,
stateAll = () => env.proxyState.allApis(),
stateOne = id => env.proxyState.api(id),
stateUpdate = seq => env.proxyState.updateApis(seq)
)
)
) ++ env.adminExtensions.resources()
}
Expand Down Expand Up @@ -1415,6 +1436,7 @@ class GenericApiController(ApiAction: ApiAction, cc: ControllerComponents)(impli
) match {
case None => result(Results.NotFound, notFoundBody, request, None)
case Some(resource) if !resource.access.canBulk && bulk =>
println("HERE")
result(
Results.Unauthorized,
Json.obj("error" -> "unauthorized", "error_description" -> "you cannot do that"),
Expand Down Expand Up @@ -1999,7 +2021,7 @@ class GenericApiController(ApiAction: ApiAction, cc: ControllerComponents)(impli
}
}

// GET /apis/:group/:version/:entity/:id/_template
// GET /apis/:group/:version/:entity/_template
def template(group: String, version: String, entity: String) = ApiAction.async { ctx =>
withResource(group, version, entity, ctx.request) { resource =>
val templ = resource.access.template(version, ctx.request.queryString.mapValues(_.last))
Expand Down
4 changes: 4 additions & 0 deletions otoroshi/app/cluster/cluster.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import akka.util.ByteString
import com.github.blemale.scaffeine.Scaffeine
import com.google.common.io.Files
import com.typesafe.config.ConfigFactory
import next.models.{ApiDataStore, KvApiDataStore}
import org.apache.commons.codec.binary.Hex
import org.joda.time.DateTime
import otoroshi.api.OtoroshiEnvHolder
Expand Down Expand Up @@ -2945,6 +2946,9 @@ class SwappableInMemoryDataStores(
private lazy val _draftDataStore = new KvDraftDataStore(redis, env)
override def draftsDataStore: DraftDataStore = _draftDataStore

private lazy val _apiDataStore = new KvApiDataStore(redis, env)
override def apiDataStore: ApiDataStore = _apiDataStore

private lazy val _adminPreferencesDatastore = new AdminPreferencesDatastore(env)
def adminPreferencesDatastore: AdminPreferencesDatastore = _adminPreferencesDatastore

Expand Down
3 changes: 3 additions & 0 deletions otoroshi/app/models/config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ case class DefaultTemplates(
certificate: Option[JsObject] = Json.obj().some, // Option[Cert],
script: Option[JsObject] = Json.obj().some, // Option[Script],
draft: Option[JsObject] = Json.obj().some, // Option[Draft],
api: Option[JsObject] = Json.obj().some, // Option[Api],
tcpService: Option[JsObject] = Json.obj().some, // Option[TcpService],
dataExporter: Option[JsObject] = Json.obj().some, // Option[DataExporterConfig],
tenant: Option[JsObject] = Json.obj().some, // Option[Tenant],
Expand All @@ -550,6 +551,7 @@ object DefaultTemplates {
certificate = json.select("certificate").asOpt[JsObject],
script = json.select("script").asOpt[JsObject],
draft = json.select("draft").asOpt[JsObject],
api = json.select("api").asOpt[JsObject],
tcpService = json.select("tcpService").asOpt[JsObject],
dataExporter = json.select("dataExporter").asOpt[JsObject],
tenant = json.select("tenant").asOpt[JsObject],
Expand All @@ -574,6 +576,7 @@ object DefaultTemplates {
"certificate" -> o.certificate.getOrElse(JsNull).asValue,
"script" -> o.script.getOrElse(JsNull).asValue,
"draft" -> o.draft.getOrElse(JsNull).asValue,
"api" -> o.api.getOrElse(JsNull).asValue,
"tcpService" -> o.tcpService.getOrElse(JsNull).asValue,
"dataExporter" -> o.dataExporter.getOrElse(JsNull).asValue,
"tenant" -> o.tenant.getOrElse(JsNull).asValue,
Expand Down
Loading

0 comments on commit 4a58f94

Please sign in to comment.