@@ -22,15 +22,14 @@ import akka.actor.{ActorRef, ActorSystem}
2222import javax .inject .Inject
2323import play .api .{Configuration , Logger }
2424import play .api .libs .concurrent .CustomExecutionContext
25- import play .api .libs .ws .WSClient
25+ import play .api .libs .ws .{ WSAuthScheme , WSClient }
2626import akka .stream .Materializer
2727import play .api .libs .streams .ActorFlow
2828import actors .{ClientSocketActor , PublishSocketMessageActor }
2929import play .api .mvc ._
30- import scala .concurrent .ExecutionContext
31- import authorization .AuthProvider
32- import play .api .libs .json .Json
33-
30+ import scala .concurrent .{Future , ExecutionContext }
31+ import authorization .{AuthProvider , AuthAction }
32+ import play .api .libs .json .{Json , JsValue }
3433
3534
3635trait MyExecutionContext extends ExecutionContext
@@ -55,7 +54,7 @@ class MyExecutionContextImpl @Inject()(implicit system: ActorSystem)
5554
5655class InstanceRegistryController @ Inject ()(implicit system : ActorSystem , mat : Materializer , myExecutionContext : MyExecutionContext ,
5756 val controllerComponents : ControllerComponents ,
58- ws : WSClient , config : Configuration )
57+ ws : WSClient , config : Configuration , authAction : AuthAction )
5958 extends BaseController {
6059
6160
@@ -64,12 +63,13 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
6463 val instanceRegistryUri = config.get[String ](" app.instanceRegistryUri" )
6564 val instanceRegistryBasePath = config.get[String ](" app.instanceRegistryBasePath" )
6665
67- /** This method maps list of instances with specific componentType.
66+
67+ /** This method maps list of instances with specific componentType.
6868 *
6969 * @param componentType
7070 * @return
7171 */
72- def instances (componentType : String ): Action [AnyContent ] = Action .async {
72+ def instances (componentType : String ): Action [AnyContent ] = authAction .async {
7373 ws.url(instanceRegistryUri).addQueryStringParameters(" ComponentType" -> componentType)
7474 .withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
7575 .get().map { response =>
@@ -87,13 +87,13 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
8787 }
8888 }
8989
90- /** Called to fetch network graph of current registry. Contains a list of all instances and all links
90+ /** Called to fetch network graph of current registry. Contains a list of all instances and all links
9191 * currently registered.
9292 *
9393 * @return
9494 */
9595
96- def getNetwork (): Action [AnyContent ] = Action .async {
96+ def getNetwork (): Action [AnyContent ] = authAction .async {
9797 ws.url(instanceRegistryUri + " /instances/network" ).withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
9898 .get().map { response =>
9999 // TODO: possible handling of parsing the data can be done here
@@ -114,7 +114,7 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
114114 * @return
115115 */
116116
117- def numberOfInstances (componentType : String ): Action [AnyContent ] = Action .async {
117+ def numberOfInstances (componentType : String ): Action [AnyContent ] = authAction .async {
118118 // TODO: handle what should happen if the instance registry is not reachable.
119119 // TODO: create constants for the urls
120120 ws.url(instanceRegistryUri + " /count" ).addQueryStringParameters(" ComponentType" -> componentType)
@@ -137,7 +137,7 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
137137 */
138138
139139
140- def handleRequest (action : String , instanceID : String ): Action [AnyContent ] = Action .async { request =>
140+ def handleRequest (action : String , instanceID : String ): Action [AnyContent ] = authAction .async { request =>
141141 ws.url(instanceRegistryUri + " /instances/" + instanceID + action)
142142 .withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
143143 .post(" " )
@@ -146,7 +146,14 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
146146 }(myExecutionContext)
147147 }
148148
149- def reconnect (from : Int , to : Int ): Action [AnyContent ] = Action .async { request =>
149+ /**
150+ * This method is called to assign a new instance to the instance with specified ID.
151+ * @param from
152+ * @param to
153+ * @return
154+ */
155+
156+ def reconnect (from : Int , to : Int ): Action [AnyContent ] = authAction.async { request =>
150157
151158 ws.url(instanceRegistryUri + " /instances/" + from + " /assignInstance"
152159 )
@@ -161,6 +168,7 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
161168 }
162169 }(myExecutionContext)
163170 }
171+
164172 /**
165173 * This function is for handling an POST request for adding an instance to the Scala web server
166174 * (E.g. .../instances/deploy
@@ -169,12 +177,70 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
169177 * @param name
170178 */
171179
172- def postInstance (compType : String , name : String ): Action [AnyContent ] = Action .async
180+ def postInstance (compType : String , name : String ): Action [AnyContent ] = authAction.async {
181+ request =>
182+ ws.url(instanceRegistryUri + " /instances/deploy" )
183+ .withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
184+ .post(Json .obj(" ComponentType" -> compType, " InstanceName" -> name))
185+ .map { response =>
186+ response.status match {
187+ // scalastyle:off magic.number
188+ case 202 =>
189+ // scalastyle:on magic.number
190+ Ok (response.body)
191+ case x : Any =>
192+ new Status (x)
193+ }
194+ }(myExecutionContext)
195+ }
196+
197+ /**
198+ * This function sends JWT token and Username:Password encoded into the headers to Instance Registry
199+ * Instance registry returns a valid JWT token.
200+ *
201+ * @return
202+ */
203+
204+ def authentication (): Action [AnyContent ] = Action .async {
205+ request =>
206+ // val json = request.body.asJson.get
207+
208+
209+ val jsonBody : Option [JsValue ] = request.body.asJson
210+
211+ jsonBody.map { json =>
212+
213+ val username = (json \ " username" ).as[String ]
214+ val password = (json \ " password" ).as[String ]
215+
216+ ws.url(instanceRegistryUri + " /users" + " /authenticate" )
217+ .withAuth(username, password, WSAuthScheme .BASIC )
218+ .withHttpHeaders((" Delphi-Authorization" , s " ${AuthProvider .generateJwt()}" ))
219+ .post(" " )
220+ .map { response =>
221+ if (response.status == 200 ) {
222+ Ok (Json .obj(" token" -> response.body, " refreshToken" -> " " ))
223+ } else {
224+ new Status (response.status)
225+ }
226+ }
227+ }.getOrElse{ Future (BadRequest (" Invalid body" ))}
228+
229+ }
230+
231+ /**
232+ * This function is used to add a label to specific instance.
233+ * @param instanceID ID of the instance on which label is to be added
234+ * @param label
235+ * @return
236+ */
237+
238+ def labelInstance (instanceID : String , label : String ): Action [AnyContent ] = authAction.async
173239 {
174240 request =>
175- ws.url(instanceRegistryUri + " /instances/deploy " )
241+ ws.url(instanceRegistryUri + " /instances/" + instanceID + " /label " )
176242 .withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
177- .post(Json .obj(" ComponentType " -> compType, " InstanceName " -> name ))
243+ .post(Json .obj(" Label " -> label ))
178244 .map { response =>
179245 response.status match {
180246 // scalastyle:off magic.number
0 commit comments