Skip to content

Commit

Permalink
implement device listing endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Simão Mata <[email protected]>
  • Loading branch information
simao committed Jan 28, 2020
1 parent c4529f8 commit f552eef
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,15 @@ protected class EcuRepository()(implicit val db: Database, val ec: ExecutionCont
.filter(_.namespace === ns)
.map(_.hardwareId)
.distinct
.paginateAndSortResult(identity, offset = offset, limit = limit)
.paginateResult(offset = offset, limit = limit)
}

def findAllDeviceIds(ns: Namespace, offset: Long, limit: Long): Future[PaginationResult[DeviceId]] = db.run {
Schema.devices
.filter(_.namespace === ns)
.map(d => (d.id, d.createdAt))
.paginateAndSortResult(_._2, offset = offset, limit = limit)
.map(_.map(_._1))
}

def findFor(deviceId: DeviceId): Future[Map[EcuIdentifier, Ecu]] = db.run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ object Schema {
def namespace = column[Namespace]("namespace")
def id = column[DeviceId]("id")
def primaryEcu = column[EcuIdentifier]("primary_ecu_id")
def createdAt = column[Instant]("created_at")

def pk = primaryKey("devices_pk", id)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.advancedtelematic.libtuf.data.TufDataType.TargetName
import com.advancedtelematic.libtuf_server.keyserver.KeyserverClient
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
import slick.jdbc.MySQLProfile.api._
import PaginationParametersDirectives._

import scala.concurrent.ExecutionContext

Expand All @@ -37,13 +38,6 @@ class AdminResource(extractNamespace: Directive1[Namespace], val keyserverClient
val deviceRegistration = new DeviceRegistration(keyserverClient)
val repositoryCreation = new RepositoryCreation(keyserverClient)

val paginationParameters: Directive[(Long, Long)] =
(parameters('limit.as[Long].?) & parameters('offset.as[Long].?)).tmap { case (mLimit, mOffset) =>
val limit = mLimit.getOrElse(50L).min(1000)
val offset = mOffset.getOrElse(0L)
(limit, offset)
}

def repoRoute(ns: Namespace): Route =
pathPrefix("repo") {
(post & pathEnd) {
Expand Down Expand Up @@ -113,7 +107,7 @@ class AdminResource(extractNamespace: Directive1[Namespace], val keyserverClient
}
} ~
(get & path("hardware_identifiers")) {
paginationParameters { (limit, offset) =>
PaginationParameters { (limit, offset) =>
val f = ecuRepository.findAllHardwareIdentifiers(ns, offset, limit)
complete(f)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ import com.advancedtelematic.libats.messaging_datatype.Messages.{DeviceUpdateAss
import slick.jdbc.MySQLProfile.api._
import com.advancedtelematic.libats.http.UUIDKeyAkka._
import akka.http.scaladsl.server.Directives._

import scala.concurrent.{ExecutionContext, Future}
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
import PaginationParametersDirectives._
import com.advancedtelematic.director.db.EcuRepositorySupport

// Implements routes provided by old director that ota-web-app still uses
class LegacyRoutes(extractNamespace: Directive1[Namespace])(implicit val db: Database, ec: ExecutionContext, messageBusPublisher: MessageBusPublisher) {
class LegacyRoutes(extractNamespace: Directive1[Namespace])(implicit val db: Database, val ec: ExecutionContext, messageBusPublisher: MessageBusPublisher)
extends EcuRepositorySupport {
private val deviceAssignments = new DeviceAssignments()

private def createDeviceAssignment(ns: Namespace, deviceId: DeviceId, mtuId: UpdateId): Future[Unit] = {
Expand All @@ -42,6 +46,11 @@ class LegacyRoutes(extractNamespace: Directive1[Namespace])(implicit val db: Dat
val a = deviceAssignments.cancel(ns, List(deviceId))
complete(a.map(_.map(_.deviceId)))
}
} ~
(path("admin" / "devices") & PaginationParameters) { (limit, offset) =>
get {
complete(ecuRepository.findAllDeviceIds(ns, offset, limit))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.advancedtelematic.director.http


import akka.http.scaladsl.server.Directive
import akka.http.scaladsl.server.Directives._

object PaginationParametersDirectives {
val PaginationParameters: Directive[(Long, Long)] =
(parameters('limit.as[Long].?) & parameters('offset.as[Long].?)).tmap { case (mLimit, mOffset) =>
val limit = mLimit.getOrElse(50L).min(1000)
val offset = mOffset.getOrElse(0L)
(limit, offset)
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
package com.advancedtelematic.director.http

import akka.http.scaladsl.model.StatusCodes
import com.advancedtelematic.director.util._
import com.advancedtelematic.libats.data.DataType.{MultiTargetUpdateId, Namespace}
import com.advancedtelematic.libats.messaging_datatype.DataType.{DeviceId, UpdateId}
import com.advancedtelematic.director.data.Generators._
import com.advancedtelematic.director.data.GeneratorOps._
import com.advancedtelematic.director.data.Codecs._
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
import org.scalatest.Assertion
import cats.syntax.option._
import cats.syntax.show._
import com.advancedtelematic.director.data.AdminDataType.{EcuInfoResponse, FindImageCount, RegisterDevice}
import com.advancedtelematic.director.data.Codecs._
import com.advancedtelematic.director.data.DbDataType.Ecu
import com.advancedtelematic.director.data.GeneratorOps._
import com.advancedtelematic.director.data.Generators._
import com.advancedtelematic.director.db.{DbSignedRoleRepositorySupport, RepoNamespaceRepositorySupport}
import com.advancedtelematic.director.http.AdminResources.RegisterDeviceResult
import com.advancedtelematic.director.util._
import com.advancedtelematic.libats.codecs.CirceCodecs._
import com.advancedtelematic.libats.data.DataType.Namespace
import com.advancedtelematic.libats.data.{EcuIdentifier, PaginationResult}
import com.advancedtelematic.libtuf.data.ClientDataType.RootRole
import com.advancedtelematic.libtuf.data.TufDataType.{HardwareIdentifier, SignedPayload, TargetFilename, TufKey, TufKeyPair}
import com.advancedtelematic.libats.messaging_datatype.DataType.{DeviceId, UpdateId}
import com.advancedtelematic.libtuf.data.ClientCodecs._
import com.advancedtelematic.libtuf.data.ClientDataType.RootRole
import com.advancedtelematic.libtuf.data.TufCodecs._
import cats.syntax.show._
import com.advancedtelematic.director.data.AdminDataType.{EcuInfoResponse, FindImageCount, MultiTargetUpdate, QueueResponse, RegisterDevice}
import com.advancedtelematic.libtuf.data.TufDataType.{HardwareIdentifier, SignedPayload, TargetFilename, TufKey, TufKeyPair}
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
import org.scalactic.source.Position
import com.advancedtelematic.director.data.Codecs._
import com.advancedtelematic.libats.codecs.CirceCodecs._
import org.scalatest.OptionValues._
import com.advancedtelematic.libats.messaging_datatype.Messages._
import org.scalatest.Assertion

object AdminResources {
case class RegisterDeviceResult(deviceId: DeviceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.advancedtelematic.director.data.Codecs._
import com.advancedtelematic.libats.codecs.CirceCodecs._
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
import cats.syntax.show._
import com.advancedtelematic.libats.data.PaginationResult
import org.scalatest.OptionValues._
import com.advancedtelematic.libats.messaging_datatype.Messages._

Expand Down Expand Up @@ -75,4 +76,18 @@ class LegacyApiResourceSpec extends DirectorSpec
msg shouldBe defined
msg.get shouldBe a [DeviceUpdateCanceled]
}

testWithRepo("get admin devices") { implicit ns =>
val regDev = registerAdminDeviceOk()

Get(apiUri("admin/devices")).namespaced ~> routes ~> check {
status shouldBe StatusCodes.OK
val devices = responseAs[PaginationResult[DeviceId]]
devices.total shouldBe 1
devices.offset shouldBe 0
devices.limit shouldBe 50
devices.values should have size(1)
devices.values should contain(regDev.deviceId)
}
}
}

0 comments on commit f552eef

Please sign in to comment.