@@ -22,14 +22,15 @@ 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 .json ._
26
25
import play .api .libs .ws .WSClient
27
26
import akka .stream .Materializer
28
27
import play .api .libs .streams .ActorFlow
29
28
import actors .{ClientSocketActor , PublishSocketMessageActor }
30
29
import play .api .mvc ._
31
-
32
30
import scala .concurrent .ExecutionContext
31
+ import authorization .AuthProvider
32
+ import play .api .libs .json .Json
33
+
33
34
34
35
35
36
trait MyExecutionContext extends ExecutionContext
@@ -63,9 +64,15 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
63
64
val instanceRegistryUri = config.get[String ](" app.instanceRegistryUri" )
64
65
val instanceRegistryBasePath = config.get[String ](" app.instanceRegistryBasePath" )
65
66
67
+ /** This method maps list of instances with specific componentType.
68
+ *
69
+ * @param componentType
70
+ * @return
71
+ */
66
72
def instances (componentType : String ): Action [AnyContent ] = Action .async {
67
-
68
- ws.url(instanceRegistryUri + " /instances" ).addQueryStringParameters(" ComponentType" -> componentType).get().map { response =>
73
+ ws.url(instanceRegistryUri).addQueryStringParameters(" ComponentType" -> componentType)
74
+ .withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
75
+ .get().map { response =>
69
76
// TODO: possible handling of parsing the data can be done here
70
77
71
78
Ok (response.body)
@@ -80,8 +87,15 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
80
87
}
81
88
}
82
89
90
+ /** Called to fetch network graph of current registry. Contains a list of all instances and all links
91
+ * currently registered.
92
+ *
93
+ * @return
94
+ */
95
+
83
96
def getNetwork (): Action [AnyContent ] = Action .async {
84
- ws.url(instanceRegistryUri + " /network" ).get().map { response =>
97
+ ws.url(instanceRegistryUri + " /instances/network" ).withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
98
+ .get().map { response =>
85
99
// TODO: possible handling of parsing the data can be done here
86
100
Logger .debug(response.body)
87
101
if (response.status == 200 ) {
@@ -92,10 +106,20 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
92
106
}(myExecutionContext)
93
107
}
94
108
95
- def numberOfInstances (componentType : String ) : Action [AnyContent ] = Action .async {
109
+ /**
110
+ * Fetches the number of instances for the specified ComponentType. The ComponentType is an optional parameter which is passed as an query
111
+ * argument named 'ComponentType'
112
+ *
113
+ * @param componentType
114
+ * @return
115
+ */
116
+
117
+ def numberOfInstances (componentType : String ): Action [AnyContent ] = Action .async {
96
118
// TODO: handle what should happen if the instance registry is not reachable.
97
119
// TODO: create constants for the urls
98
- ws.url(instanceRegistryUri + " /numberOfInstances" ).addQueryStringParameters(" ComponentType" -> componentType).get().map { response =>
120
+ ws.url(instanceRegistryUri + " /count" ).addQueryStringParameters(" ComponentType" -> componentType)
121
+ .withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
122
+ .get().map { response =>
99
123
// TODO: possible handling of parsing the data can be done here
100
124
if (response.status == 200 ) {
101
125
Ok (response.body)
@@ -106,31 +130,51 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
106
130
}
107
131
108
132
/**
109
- * This function is for handling all(start, stop, play, pause, resume) POST request.
110
- * To control the instance State
111
- * @param componentId
112
- */
133
+ * This function is for handling all(start, stop, play, pause, resume) POST request.
134
+ * To control the instance State (E.g. /instances/42/stop )
135
+ *
136
+ * @param componentId
137
+ */
113
138
114
139
115
140
def handleRequest (action : String , instanceID : String ): Action [AnyContent ] = Action .async { request =>
116
- ws.url(instanceRegistryUri + action)
117
- .addQueryStringParameters( " Id " -> instanceID )
141
+ ws.url(instanceRegistryUri + " /instances/ " + instanceID + action)
142
+ .withHttpHeaders(( " Authorization " , s " Bearer ${ AuthProvider .generateJwt()} " ) )
118
143
.post(" " )
119
144
.map { response =>
120
145
new Status (response.status)
121
146
}(myExecutionContext)
122
147
}
123
148
149
+ def reconnect (from : Int , to : Int ): Action [AnyContent ] = Action .async { request =>
150
+
151
+ ws.url(instanceRegistryUri + " /instances/" + from + " /assignInstance"
152
+ )
153
+ .withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
154
+ .post(Json .obj(" AssignedInstanceId" -> to))
155
+ .map { response =>
156
+ response.status match {
157
+ case 200 =>
158
+ Ok (response.body)
159
+ case x =>
160
+ new Status (x)
161
+ }
162
+ }(myExecutionContext)
163
+ }
124
164
/**
125
- * This function is for handling an POST request for adding an instance to the Scala web server
126
- *
127
- * @param componentType
128
- * @param name
129
- */
130
- def postInstance (compType : String , name : String ): Action [AnyContent ] = Action .async { request =>
131
- ws.url(instanceRegistryUri + " /deploy" )
132
- .addQueryStringParameters(" ComponentType" -> compType, " InstanceName" -> name)
133
- .post(" " )
165
+ * This function is for handling an POST request for adding an instance to the Scala web server
166
+ * (E.g. .../instances/deploy
167
+ *
168
+ * @param componentType
169
+ * @param name
170
+ */
171
+
172
+ def postInstance (compType : String , name : String ): Action [AnyContent ] = Action .async
173
+ {
174
+ request =>
175
+ ws.url(instanceRegistryUri + " /instances/deploy" )
176
+ .withHttpHeaders((" Authorization" , s " Bearer ${AuthProvider .generateJwt()}" ))
177
+ .post(Json .obj(" ComponentType" -> compType, " InstanceName" -> name))
134
178
.map { response =>
135
179
response.status match {
136
180
// scalastyle:off magic.number
@@ -142,5 +186,4 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
142
186
}
143
187
}(myExecutionContext)
144
188
}
145
-
146
189
}
0 commit comments