7
7
import org .springframework .http .MediaType ;
8
8
import org .springframework .http .ResponseEntity ;
9
9
import org .springframework .web .bind .annotation .*;
10
+ import org .springframework .web .util .WebUtils ;
10
11
import pl .simplemethod .codebin .linkDeploy .LinkClient ;
11
12
import pl .simplemethod .codebin .model .Containers ;
12
13
import pl .simplemethod .codebin .model .Images ;
14
+ import pl .simplemethod .codebin .model .Users ;
13
15
import pl .simplemethod .codebin .repository .ContainersRepository ;
14
16
import pl .simplemethod .codebin .repository .ImagesRepository ;
17
+ import pl .simplemethod .codebin .repository .UsersRepository ;
15
18
19
+ import javax .servlet .http .Cookie ;
20
+ import javax .servlet .http .HttpServletResponse ;
16
21
import java .time .Instant ;
17
22
import java .util .ArrayList ;
18
23
import java .util .List ;
@@ -33,6 +38,9 @@ public class SrvRestController {
33
38
@ Autowired
34
39
private LinkClient linkClient ;
35
40
41
+ @ Autowired
42
+ private UsersRepository usersRepository ;
43
+
36
44
/*
37
45
@GetMapping("/createtest")
38
46
public @ResponseBody
@@ -79,6 +87,35 @@ ResponseEntity createContainer(@RequestParam("dockerimage") String dockerImage,
79
87
return new ResponseEntity <>(response .toString (), headers , HttpStatus .valueOf (status ));
80
88
}
81
89
90
+ @ GetMapping ("container/new" )
91
+ public @ ResponseBody
92
+ ResponseEntity createContainerForUser (@ RequestParam ("dockerimage" ) String dockerImage , @ RequestParam ("exposedports" ) Integer exposedPorts , @ RequestParam ("hostports" ) Integer hostPorts , @ RequestParam ("name" ) String name , @ RequestParam ("rammemory" ) Long ramMemory , @ RequestParam ("diskquota" ) Long diskQuota , @ CookieValue ("id" ) String id ) {
93
+ HttpHeaders headers = new HttpHeaders ();
94
+ headers .setContentType (MediaType .APPLICATION_JSON );
95
+ org .json .JSONObject response = srvClient .createAndRunContainer (srvClient .generateCreateConfig (dockerImage , exposedPorts , hostPorts , ramMemory , diskQuota ), name );
96
+ int status ;
97
+ if (response .get ("status" ).toString ().equals ("204" )) {
98
+ status = 200 ;
99
+ List <Containers > containers = new ArrayList <>();
100
+ Images images = imagesRepository .getFirstByName (dockerImage );
101
+ Containers newContainer = new Containers (name , response .get ("id" ).toString (), images , exposedPorts , hostPorts , ramMemory , diskQuota , "XD" , 1 , Instant .now ().getEpochSecond ());
102
+ containers .add (newContainer );
103
+ containers .forEach (containersRepository ::save );
104
+
105
+ try {
106
+ // TODO: 01/06/2019 oczekuje integera, a dajemy mu longa, pojebane
107
+ Users users = usersRepository .getOne ((long ) Integer .parseInt (id ));
108
+ users .getContainers ().add (newContainer );
109
+ usersRepository .save (users );
110
+ } catch (NumberFormatException e ) {
111
+ return new ResponseEntity <>(e .toString (), headers , HttpStatus .NOT_FOUND );
112
+ }
113
+ } else {
114
+ status = Integer .valueOf (response .get ("status" ).toString ());
115
+ }
116
+ return new ResponseEntity <>(response .toString (), headers , HttpStatus .valueOf (status ));
117
+ }
118
+
82
119
/**
83
120
* The method restarts the container
84
121
*
0 commit comments