Skip to content

Commit d6ef548

Browse files
authored
Move cleanup actor to lunagraph. (#73)
* Move cleanup actor to lunagraph. * Remove a leftover debug log. * Simplify token retrieval.
1 parent fa352fc commit d6ef548

File tree

4 files changed

+53
-19
lines changed

4 files changed

+53
-19
lines changed

app/actors/Cleaner.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Cleaner @Inject()(unifiService: UnifiService,
2525
persons <- EitherT(peopleService.getPersons)
2626
accounts <- EitherT(unifiService.getRadiusAccounts)
2727
} yield {
28-
accounts.map(_.name).toSet diff persons.map(_.email).toSet
28+
accounts.map(_.name).toSet diff persons.map(_.emailAddress).toSet
2929
}
3030

3131
oldAccounts.value.map {

app/models/Person.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package models
22

33
import play.api.libs.json.{Format, Json}
44

5-
case class Person(email: String)
5+
case class Person(emailAddress: String)
66

77
object Person {
88
implicit val PersonFormat: Format[Person] = Json.format[Person]

app/services/PeopleService.scala

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,60 @@ package services
33
import com.google.inject.Inject
44
import models.Person
55
import play.api.http.Status
6-
import play.api.libs.json.Format
7-
import play.api.libs.ws.WSClient
6+
import play.api.libs.json.Json
7+
import play.api.libs.ws.{WSAuthScheme, WSClient}
88
import play.api.{Configuration, Logging}
99

10+
import java.time.Instant
1011
import scala.concurrent.{ExecutionContext, Future}
1112

1213
class PeopleService @Inject()(configuration: Configuration,
1314
ws: WSClient)(implicit ec: ExecutionContext) extends Logging {
1415

15-
private val baseUrl: String = configuration.get[String]("people.baseUrl")
16-
private val apiKey: String = configuration.get[String]("people.apiKey")
16+
private val query =
17+
"""
18+
|{
19+
| people(employeeStatuses: ACTIVE) {
20+
| emailAddress
21+
| }
22+
|}
23+
|""".stripMargin
24+
25+
private val baseUrl: String = configuration.get[String]("lunagraph.baseUrl")
26+
27+
private val clientId: String = configuration.get[String]("lunagraph.client.id")
28+
private val clientSecret: String = configuration.get[String]("lunagraph.client.secret")
29+
private val clientIssuer: String = configuration.get[String]("lunagraph.client.issuer")
30+
31+
def getPersons: Future[Either[String, Seq[Person]]] = for {
32+
token <- getToken()
33+
wsRes <- ws.url(s"${baseUrl}/graphql")
34+
.withHttpHeaders("Authorization"-> s"Bearer ${token}")
35+
.post(Json.toJsObject(Map("query" -> query)))
36+
res = wsRes.status match {
37+
case Status.OK =>
38+
val jsonResult = Json.parse(wsRes.body)
39+
Right((jsonResult \ "data" \ "people").get.as[Seq[Person]])
40+
case _ => logger.error(wsRes.body)
41+
Left(wsRes.body)
42+
}
43+
} yield res
1744

18-
def getPersons: Future[Either[String, Seq[Person]]] = {
19-
withAuth[Seq[Person]](s"$baseUrl/api/people")
20-
}
2145

22-
private def withAuth[A](url: String)(implicit fjs: Format[A]): Future[Either[String, A]] = {
23-
ws.url(url).addQueryStringParameters("apiKey" -> apiKey).get().map { response =>
24-
response.status match {
25-
case Status.OK => Right(response.json.as[A])
46+
private def getToken(): Future[Either[String, String]] = {
47+
for {
48+
clientResponse <- ws.url(s"${clientIssuer}/protocol/openid-connect/token")
49+
.withAuth(clientId, clientSecret, WSAuthScheme.BASIC)
50+
.post(Map("grant_type" -> Seq("client_credentials")))
51+
parsedResponse = clientResponse.status match {
52+
case Status.OK =>
53+
val jsonResponse = Json.parse(clientResponse.body)
54+
val token = (jsonResponse \ "access_token").get.as[String]
55+
Right(token)
2656
case _ =>
27-
logger.error(response.body)
28-
Left(response.body)
57+
logger.error(clientResponse.body)
58+
Left(clientResponse.body)
2959
}
30-
}
60+
} yield parsedResponse
3161
}
3262
}

conf/application.conf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ unifi {
3030
]
3131
}
3232

33-
people {
34-
baseUrl = "https://people.lunatech.com"
35-
apiKey = ${?PEOPLE_API_KEY}
33+
lunagraph {
34+
baseUrl = "https://lunagraph.acceptance.lunatech.cloud"
35+
client {
36+
issuer = "https://keycloak.lunatech.com/realms/lunatech"
37+
id = ${LUNAGRAPH_CLIENT_ID}
38+
secret = ${?LUNAGRAPH_CLIENT_SECRET}
39+
}
3640
}
3741

3842
play.http.forwarded {

0 commit comments

Comments
 (0)