Skip to content

Commit 348e972

Browse files
authored
Merge pull request #79 from delphi-hub/feature/add_delete_instance
Docker control functionalities with basic test cases
2 parents a4a4b58 + 955b9bd commit 348e972

36 files changed

+877
-196
lines changed

app/controllers/ApiRouter.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,12 @@ class ApiRouter @Inject()(irController: InstanceRegistryController, sysControlle
3636
case GET(p"/numberOfInstances" ? q"componentType=$componentType") => irController.numberOfInstances(componentType)
3737
case GET(p"/instances" ? q"componentType=$componentType") => irController.instances(componentType)
3838
case GET(p"/systemInfo") => sysController.getInfo()
39+
case POST(p"/postInstance" ? q"componentType=$componentType"& q"name=$name") => irController.postInstance(componentType, name)
40+
case POST(p"/startInstance" ? q"instanceID=$instanceID") => irController.handleRequest(action="/start", instanceID)
41+
case POST(p"/stopInstance" ? q"instanceID=$instanceID") => irController.handleRequest(action="/stop", instanceID)
42+
case POST(p"/pauseInstance" ? q"instanceID=$instanceID") => irController.handleRequest(action="/pause", instanceID)
43+
case POST(p"/resumeInstance" ? q"instanceID=$instanceID") => irController.handleRequest(action="/resume", instanceID)
44+
case POST(p"/deleteInstance" ? q"instanceID=$instanceID") => irController.handleRequest(action="/delete", instanceID)
45+
3946
}
4047
}

app/controllers/InstanceRegistryController.scala

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,31 @@ package controllers
2121
import akka.actor.{ActorRef, ActorSystem}
2222
import javax.inject.Inject
2323
import play.api.Configuration
24-
25-
import scala.concurrent.ExecutionContext
2624
import play.api.libs.concurrent.CustomExecutionContext
25+
import play.api.libs.json._
2726
import play.api.libs.ws.WSClient
2827
import akka.stream.Materializer
2928
import play.api.libs.streams.ActorFlow
3029
import actors.{ClientSocketActor, PublishSocketMessageActor}
3130
import play.api.mvc._
3231

32+
import scala.concurrent.ExecutionContext
33+
3334

3435
trait MyExecutionContext extends ExecutionContext
3536

3637
/**
3738
* Custom execution context. Used to prevent overflowing of the thread pool,
3839
* which should be used to handle client connections.
40+
*
3941
* @param system
4042
*/
4143
class MyExecutionContextImpl @Inject()(implicit system: ActorSystem)
4244
extends CustomExecutionContext(system, "my.executor") with MyExecutionContext
4345

4446
/**
4547
* Controller used to manage the requests regarding the instance registry.
48+
*
4649
* @param myExecutionContext
4750
* @param controllerComponents
4851
* @param ws
@@ -91,4 +94,45 @@ class InstanceRegistryController @Inject()(implicit system: ActorSystem, mat: Ma
9194
}(myExecutionContext)
9295
}
9396

97+
/**
98+
* This function is for handling all(start, stop, play, pause, resume) POST request.
99+
* To control the instance State
100+
* @param componentId
101+
*/
102+
103+
104+
def handleRequest(action: String, instanceID: String): Action[AnyContent] = Action.async { request =>
105+
ws.url(instanceRegistryUri + action)
106+
.addQueryStringParameters("Id" -> instanceID)
107+
.post("")
108+
.map { response =>
109+
response.status match {
110+
case 202 =>
111+
Ok(response.body)
112+
case x =>
113+
new Status(x)
114+
}
115+
}(myExecutionContext)
116+
}
117+
118+
/**
119+
* This function is for handling an POST request for adding an instance to the Scala web server
120+
*
121+
* @param componentType
122+
* @param name
123+
*/
124+
def postInstance(compType: String, name: String): Action[AnyContent] = Action.async { request =>
125+
ws.url(instanceRegistryUri + "/deploy")
126+
.addQueryStringParameters("ComponentType" -> compType, "InstanceName" -> name)
127+
.post("")
128+
.map { response =>
129+
response.status match {
130+
case 202 =>
131+
Ok(response.body)
132+
case x =>
133+
new Status(x)
134+
}
135+
}(myExecutionContext)
136+
}
137+
94138
}

client/package-lock.json

Lines changed: 38 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"karma-chrome-launcher": "^2.2.0",
4848
"karma-cli": "^1.0.1",
4949
"karma-coverage-istanbul-reporter": "^2.0.4",
50+
"karma-firefox-launcher": "^1.1.0",
5051
"karma-jasmine": "^2.0.0",
5152
"karma-jasmine-html-reporter": "^1.4.0",
5253
"protractor": "^5.4.1",

client/src/app/api/api/api.service.spec.ts

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,62 @@
1616
* limitations under the License.
1717
*/
1818

19-
import { TestBed, inject } from '@angular/core/testing';
20-
import { HttpClientTestingModule } from '@angular/common/http/testing';
19+
import { TestBed, async,fakeAsync, inject } from '@angular/core/testing';
20+
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2121
import { ApiService } from './api.service';
22-
import {HttpClientModule} from '@angular/common/http';
22+
import { HttpClientModule } from '@angular/common/http';
2323

2424
describe('ApiService', () => {
25+
26+
let service, http;
27+
2528
beforeEach(() => {
2629
TestBed.configureTestingModule({
2730
imports: [HttpClientTestingModule, HttpClientModule],
28-
providers: [ApiService]
31+
providers: [ApiService],
2932
});
33+
34+
service = TestBed.get(ApiService);
35+
http = TestBed.get(HttpTestingController);
3036
});
3137

32-
it('should be created', inject([ApiService], (service: ApiService) => {
33-
expect(service).toBeTruthy();
38+
39+
afterEach(inject([HttpTestingController], (backend: HttpTestingController) => {
40+
backend.verify();
3441
}));
42+
43+
it('should be created an instance', inject([ApiService], (apiService: ApiService) => {
44+
expect(apiService).toBeTruthy();
45+
}));
46+
47+
it(`should create an instance`,
48+
async(inject([ApiService], (apiService: ApiService) => {
49+
expect(apiService.postInstance('', '')).not.toBeNull();
50+
})));
51+
52+
it(`should start an instance`,
53+
async(inject([ApiService], (apiService: ApiService) => {
54+
expect(apiService.startInstance('', '')).not.toBeNull();
55+
})));
56+
57+
it(`should stop an instance`,
58+
async(inject([ApiService], (apiService: ApiService) => {
59+
expect(apiService.stopInstance('', '')).not.toBeNull();
60+
})));
61+
62+
it(`should pause an instance`,
63+
async(inject([ApiService], (apiService: ApiService) => {
64+
expect(apiService.pauseInstance('')).not.toBeNull();
65+
})));
66+
67+
it(`should resume an instance`,
68+
async(inject([ApiService], (apiService: ApiService) => {
69+
expect(apiService.resumeInstance('')).not.toBeNull();
70+
})));
71+
72+
it(`should delete an instance`,
73+
async(inject([ApiService], (apiService: ApiService) => {
74+
expect(apiService.deleteInstance('')).not.toBeNull();
75+
})));
76+
3577
});

0 commit comments

Comments
 (0)