Skip to content

Commit

Permalink
New endpoint to force targets refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
simao committed Jan 30, 2020
1 parent 691df25 commit 96affd1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import com.advancedtelematic.libats.messaging.MessageBusPublisher
import com.advancedtelematic.libats.messaging_datatype.DataType.DeviceId
import com.advancedtelematic.libtuf.data.ClientCodecs._
import com.advancedtelematic.libtuf.data.TufCodecs._
import com.advancedtelematic.libtuf.data.TufDataType.TargetName
import com.advancedtelematic.libtuf.data.TufDataType.{RepoId, TargetName}
import com.advancedtelematic.libtuf_server.keyserver.KeyserverClient
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
import slick.jdbc.MySQLProfile.api._
import PaginationParametersDirectives._
import com.advancedtelematic.director.repo.DeviceRoleGeneration

import scala.concurrent.ExecutionContext

Expand All @@ -37,6 +38,7 @@ class AdminResource(extractNamespace: Directive1[Namespace], val keyserverClient

val deviceRegistration = new DeviceRegistration(keyserverClient)
val repositoryCreation = new RepositoryCreation(keyserverClient)
val deviceRoleGeneration = new DeviceRoleGeneration(keyserverClient)

def repoRoute(ns: Namespace): Route =
pathPrefix("repo") {
Expand All @@ -54,7 +56,7 @@ class AdminResource(extractNamespace: Directive1[Namespace], val keyserverClient
}
}

def devicePath(ns: Namespace): Route =
def devicePath(ns: Namespace, repoId: RepoId): Route =
pathPrefix(DeviceId.Path) { device =>
pathPrefix("ecus") {
pathPrefix(EcuIdPath) { ecuId =>
Expand All @@ -77,9 +79,12 @@ class AdminResource(extractNamespace: Directive1[Namespace], val keyserverClient
}
}
} ~
(pathEnd & get) {
val f = deviceRegistration.findDeviceEcuInfo(ns, device)
complete(f)
get {
val f = deviceRegistration.findDeviceEcuInfo(ns, device)
complete(f)
} ~
(path("targets.json") & put) {
complete(deviceRoleGeneration.forceTargetsRefresh(ns, repoId, device).map(StatusCodes.Created -> _))
}
}

Expand Down Expand Up @@ -112,7 +117,7 @@ class AdminResource(extractNamespace: Directive1[Namespace], val keyserverClient
complete(f)
}
} ~
devicePath(ns)
devicePath(ns, repoId)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class DeviceRoleGeneration(keyserverClient: KeyserverClient)(implicit val db: Da
}
}

def forceTargetsRefresh(ns: Namespace, repoId: RepoId, deviceId: DeviceId): Future[JsonSignedPayload] = {
val refresher = roleRefresher(ns, deviceId)
refresher.refreshTargets(repoId).map(_.content)
}

def findFreshDeviceRole[T : TufRole](ns: Namespace, repoId: RepoId, deviceId: DeviceId): Future[JsonSignedPayload] = {
implicit val refresher = roleRefresher(ns, deviceId)
roleGeneration(ns, deviceId).findRole[T](repoId).map(_.content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.advancedtelematic.libats.data.DataType.Namespace
import com.advancedtelematic.libats.data.{EcuIdentifier, PaginationResult}
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.ClientDataType.{RootRole, TargetsRole}
import com.advancedtelematic.libtuf.data.TufCodecs._
import com.advancedtelematic.libtuf.data.TufDataType.{HardwareIdentifier, SignedPayload, TargetFilename, TufKey, TufKeyPair}
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
Expand Down Expand Up @@ -159,4 +159,23 @@ class AdminResourceSpec extends DirectorSpec
resp.head.image.filepath shouldBe targetUpdate.target
}
}

testWithRepo("PUT devices/id/targets.json forces refresh of devices targets.json") { implicit ns =>
val dev = registerAdminDeviceOk()

Get(apiUri(s"device/${dev.deviceId.show}/targets.json")).namespaced ~> routes ~> check {
status shouldBe StatusCodes.OK
responseAs[SignedPayload[TargetsRole]].signed.version shouldBe 1
}

Put(apiUri(s"admin/devices/${dev.deviceId.show}/targets.json")).namespaced ~> routes ~> check {
status shouldBe StatusCodes.Created
responseAs[SignedPayload[TargetsRole]].signed.version shouldBe 2
}

Get(apiUri(s"device/${dev.deviceId.show}/targets.json")).namespaced ~> routes ~> check {
status shouldBe StatusCodes.OK
responseAs[SignedPayload[TargetsRole]].signed.version shouldBe 2
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.advancedtelematic.director.http

import akka.http.scaladsl.model.StatusCodes
import com.advancedtelematic.director.db.{DbSignedRoleRepositorySupport, RepoNamespaceRepositorySupport}
import com.advancedtelematic.director.util.{DeviceManifestSpec, DirectorSpec, RepositorySpec, RouteResourceSpec}
import com.advancedtelematic.libtuf.data.TufDataType.TargetName
import cats.syntax.show._
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
import com.advancedtelematic.director.util.{DefaultPatience, DirectorSpec, RepositorySpec, RouteResourceSpec}
import com.advancedtelematic.libtuf.data.ClientCodecs._
import com.advancedtelematic.libtuf.data.TufDataType.TargetName
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._

class AutoUpdateResourceSpec extends DirectorSpec
with RouteResourceSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ object NamespacedTests extends NamespacedTests
abstract class DirectorSpec extends FunSuite
with Matchers
with ScalaFutures
with NamespacedTests {
with NamespacedTests
with DefaultPatience {

Security.addProvider(new BouncyCastleProvider())

Expand Down

0 comments on commit 96affd1

Please sign in to comment.