@@ -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,36 @@ 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
124
149
/**
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("")
150
+ * This function is for handling an POST request for adding an instance to the Scala web server
151
+ * (E.g. .../instances/deploy
152
+ *
153
+ * @param componentType
154
+ * @param name
155
+ */
156
+
157
+ def postInstance(compType: String, name: String): Action[AnyContent] = Action.async
158
+ {
159
+ request =>
160
+ ws.url(instanceRegistryUri + "/instances/deploy")
161
+ .withHttpHeaders(("Authorization", s"Bearer ${AuthProvider.generateJwt()}"))
162
+ .post(Json.obj("ComponentType" -> compType, "InstanceName" -> name))
134
163
.map { response =>
135
164
response.status match {
136
165
// scalastyle:off magic.number
@@ -142,5 +171,4 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
142
171
}
143
172
}(myExecutionContext)
144
173
}
145
-
146
174
}
0 commit comments