77import org .springframework .http .MediaType ;
88import org .springframework .http .ResponseEntity ;
99import org .springframework .web .bind .annotation .*;
10+ import org .springframework .web .util .WebUtils ;
1011import pl .simplemethod .codebin .linkDeploy .LinkClient ;
1112import pl .simplemethod .codebin .model .Containers ;
1213import pl .simplemethod .codebin .model .Images ;
14+ import pl .simplemethod .codebin .model .Users ;
1315import pl .simplemethod .codebin .repository .ContainersRepository ;
1416import pl .simplemethod .codebin .repository .ImagesRepository ;
17+ import pl .simplemethod .codebin .repository .UsersRepository ;
1518
19+ import javax .servlet .http .Cookie ;
20+ import javax .servlet .http .HttpServletResponse ;
1621import java .time .Instant ;
1722import java .util .ArrayList ;
1823import java .util .List ;
@@ -33,6 +38,9 @@ public class SrvRestController {
3338 @ Autowired
3439 private LinkClient linkClient ;
3540
41+ @ Autowired
42+ private UsersRepository usersRepository ;
43+
3644/*
3745 @GetMapping("/createtest")
3846 public @ResponseBody
@@ -79,6 +87,35 @@ ResponseEntity createContainer(@RequestParam("dockerimage") String dockerImage,
7987 return new ResponseEntity <>(response .toString (), headers , HttpStatus .valueOf (status ));
8088 }
8189
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+
82119 /**
83120 * The method restarts the container
84121 *
0 commit comments