@@ -22,15 +22,14 @@ import akka.actor.{ActorRef, ActorSystem}
22
22
import javax .inject .Inject
23
23
import play .api .{Configuration , Logger }
24
24
import play .api .libs .concurrent .CustomExecutionContext
25
- import play .api .libs .ws .WSClient
25
+ import play .api .libs .ws .{ WSAuthScheme , WSClient }
26
26
import akka .stream .Materializer
27
27
import play .api .libs .streams .ActorFlow
28
28
import actors .{ClientSocketActor , PublishSocketMessageActor }
29
29
import 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 }
34
33
35
34
36
35
trait MyExecutionContext extends ExecutionContext
@@ -55,7 +54,7 @@ class MyExecutionContextImpl @Inject()(implicit system: ActorSystem)
55
54
56
55
class InstanceRegistryController @ Inject ()(implicit system : ActorSystem , mat : Materializer , myExecutionContext : MyExecutionContext ,
57
56
val controllerComponents : ControllerComponents ,
58
- ws : WSClient , config : Configuration )
57
+ ws : WSClient , config : Configuration , authAction : AuthAction )
59
58
extends BaseController {
60
59
61
60
@@ -64,12 +63,13 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
64
63
val instanceRegistryUri = config.get[String ](" app.instanceRegistryUri" )
65
64
val instanceRegistryBasePath = config.get[String ](" app.instanceRegistryBasePath" )
66
65
67
- /** This method maps list of instances with specific componentType.
66
+
67
+ /** This method maps list of instances with specific componentType.
68
68
*
69
69
* @param componentType
70
70
* @return
71
71
*/
72
- def instances (componentType : String ): Action [AnyContent ] = Action .async {
72
+ def instances (componentType : String ): Action [AnyContent ] = authAction .async {
73
73
ws.url(instanceRegistryUri).addQueryStringParameters(" ComponentType" -> componentType)
74
74
.withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
75
75
.get().map { response =>
@@ -87,13 +87,13 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
87
87
}
88
88
}
89
89
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
91
91
* currently registered.
92
92
*
93
93
* @return
94
94
*/
95
95
96
- def getNetwork (): Action [AnyContent ] = Action .async {
96
+ def getNetwork (): Action [AnyContent ] = authAction .async {
97
97
ws.url(instanceRegistryUri + " /instances/network" ).withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
98
98
.get().map { response =>
99
99
// TODO: possible handling of parsing the data can be done here
@@ -114,7 +114,7 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
114
114
* @return
115
115
*/
116
116
117
- def numberOfInstances (componentType : String ): Action [AnyContent ] = Action .async {
117
+ def numberOfInstances (componentType : String ): Action [AnyContent ] = authAction .async {
118
118
// TODO: handle what should happen if the instance registry is not reachable.
119
119
// TODO: create constants for the urls
120
120
ws.url(instanceRegistryUri + " /count" ).addQueryStringParameters(" ComponentType" -> componentType)
@@ -137,7 +137,7 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
137
137
*/
138
138
139
139
140
- def handleRequest (action : String , instanceID : String ): Action [AnyContent ] = Action .async { request =>
140
+ def handleRequest (action : String , instanceID : String ): Action [AnyContent ] = authAction .async { request =>
141
141
ws.url(instanceRegistryUri + " /instances/" + instanceID + action)
142
142
.withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
143
143
.post(" " )
@@ -146,7 +146,14 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
146
146
}(myExecutionContext)
147
147
}
148
148
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 =>
150
157
151
158
ws.url(instanceRegistryUri + " /instances/" + from + " /assignInstance"
152
159
)
@@ -161,6 +168,7 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
161
168
}
162
169
}(myExecutionContext)
163
170
}
171
+
164
172
/**
165
173
* This function is for handling an POST request for adding an instance to the Scala web server
166
174
* (E.g. .../instances/deploy
@@ -169,25 +177,65 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
169
177
* @param name
170
178
*/
171
179
172
- def postInstance (compType : String , name : String ): Action [AnyContent ] = Action .async
173
- {
180
+ def postInstance (compType : String , name : String ): Action [AnyContent ] = authAction.async {
174
181
request =>
175
- ws.url(instanceRegistryUri + " /instances/deploy" )
176
- .withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
177
- .post(Json .obj(" ComponentType" -> compType, " InstanceName" -> name))
178
- .map { response =>
179
- response.status match {
180
- // scalastyle:off magic.number
181
- case 202 =>
182
- // scalastyle:on magic.number
183
- Ok (response.body)
184
- case x : Any =>
185
- new Status (x)
186
- }
187
- }(myExecutionContext)
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)
188
195
}
189
196
190
- def labelInstance (instanceID : String , label : String ): Action [AnyContent ] = Action .async
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
191
239
{
192
240
request =>
193
241
ws.url(instanceRegistryUri + " /instances/" + instanceID + " /label" )
0 commit comments