@@ -17,6 +17,7 @@ func addCustomRoutes(m *http.ServeMux, s *server) {
1717m .HandleFunc ("GET /custom-api/v1/lxc/{id}/ip" , getLXCIPHandlerV1 (s ))
1818m .HandleFunc ("POST /custom-api/v1/lxc/{id}/exec" , postLXCCMDHandlerV1 (s ))
1919m .HandleFunc ("POST /custom-api/v1/lxc/{id}/exec-async" , postLXCCMDAsyncHandlerV1 (s ))
20+ m .HandleFunc ("GET /custom-api/v1/cmd/{id}" , getCMDResultHandlerV1 (s ))
2021}
2122
2223// Get LXC IP godoc
@@ -71,6 +72,7 @@ func getLXCIPHandlerV1(s *server) http.HandlerFunc {
7172)
7273b , _ := httperr .Marshall ()
7374log .Println (r .Method , r .URL .Path , "failed" , string (b ))
75+ httperr .WriteResponse (w )
7476return
7577}
7678
@@ -83,6 +85,7 @@ func getLXCIPHandlerV1(s *server) http.HandlerFunc {
8385)
8486b , _ := httperr .Marshall ()
8587log .Println (r .Method , r .URL .Path , "failed" , fmt .Sprintf ("ip=%s" , ip ), fmt .Sprintf ("err=%s" , string (b )))
88+ httperr .WriteResponse (w )
8689return
8790}
8891
@@ -94,6 +97,7 @@ func getLXCIPHandlerV1(s *server) http.HandlerFunc {
9497)
9598b , _ := httperr .Marshall ()
9699log .Println (r .Method , r .URL .Path , "failed" , string (b ))
100+ httperr .WriteResponse (w )
97101return
98102}
99103
@@ -134,6 +138,7 @@ func postLXCCMDHandlerV1(s *server) http.HandlerFunc {
134138)
135139b , _ := httperr .Marshall ()
136140log .Println (r .Method , r .URL .Path , "failed" , string (b ))
141+ httperr .WriteResponse (w )
137142return
138143}
139144
@@ -145,6 +150,7 @@ func postLXCCMDHandlerV1(s *server) http.HandlerFunc {
145150)
146151b , _ := httperr .Marshall ()
147152log .Println (r .Method , r .URL .Path , "failed" , string (b ))
153+ httperr .WriteResponse (w )
148154return
149155}
150156
@@ -158,6 +164,7 @@ func postLXCCMDHandlerV1(s *server) http.HandlerFunc {
158164)
159165b , _ := httperr .Marshall ()
160166log .Println (r .Method , r .URL .Path , "failed" , string (b ))
167+ httperr .WriteResponse (w )
161168return
162169}
163170
@@ -170,6 +177,7 @@ func postLXCCMDHandlerV1(s *server) http.HandlerFunc {
170177)
171178b , _ := httperr .Marshall ()
172179log .Println (r .Method , r .URL .Path , "failed" , string (b ))
180+ httperr .WriteResponse (w )
173181return
174182}
175183in := apidef.PostLXCExecRequest {}
@@ -181,6 +189,7 @@ func postLXCCMDHandlerV1(s *server) http.HandlerFunc {
181189)
182190b , _ := httperr .Marshall ()
183191log .Println (r .Method , r .URL .Path , "failed" , string (b ))
192+ httperr .WriteResponse (w )
184193return
185194}
186195
@@ -192,6 +201,7 @@ func postLXCCMDHandlerV1(s *server) http.HandlerFunc {
192201)
193202b , _ := httperr .Marshall ()
194203log .Println (r .Method , r .URL .Path , "failed" , string (b ))
204+ httperr .WriteResponse (w )
195205return
196206}
197207
@@ -204,6 +214,7 @@ func postLXCCMDHandlerV1(s *server) http.HandlerFunc {
204214)
205215b , _ := httperr .Marshall ()
206216log .Println (r .Method , r .URL .Path , "failed" , string (b ))
217+ httperr .WriteResponse (w )
207218return
208219}
209220
@@ -215,6 +226,7 @@ func postLXCCMDHandlerV1(s *server) http.HandlerFunc {
215226)
216227b , _ := httperr .Marshall ()
217228log .Println (r .Method , r .URL .Path , "failed" , string (b ))
229+ httperr .WriteResponse (w )
218230return
219231}
220232
@@ -242,7 +254,7 @@ func postLXCCMDHandlerV1(s *server) http.HandlerFunc {
242254// @Failure 401 {object} errors.HTTPError
243255// @Failure 404 {object} errors.HTTPError
244256// @Failure 500 {object} errors.HTTPError
245- // @Router /lxc/{id}/exec [post]
257+ // @Router /lxc/{id}/exec-async [post]
246258func postLXCCMDAsyncHandlerV1 (s * server ) http.HandlerFunc {
247259return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
248260log .Println (r .Method , r .URL .Path , "started" )
@@ -255,6 +267,7 @@ func postLXCCMDAsyncHandlerV1(s *server) http.HandlerFunc {
255267)
256268b , _ := httperr .Marshall ()
257269log .Println (r .Method , r .URL .Path , "failed" , string (b ))
270+ httperr .WriteResponse (w )
258271return
259272}
260273
@@ -266,6 +279,7 @@ func postLXCCMDAsyncHandlerV1(s *server) http.HandlerFunc {
266279)
267280b , _ := httperr .Marshall ()
268281log .Println (r .Method , r .URL .Path , "failed" , string (b ))
282+ httperr .WriteResponse (w )
269283return
270284}
271285
@@ -279,6 +293,7 @@ func postLXCCMDAsyncHandlerV1(s *server) http.HandlerFunc {
279293)
280294b , _ := httperr .Marshall ()
281295log .Println (r .Method , r .URL .Path , "failed" , string (b ))
296+ httperr .WriteResponse (w )
282297return
283298}
284299
@@ -291,6 +306,7 @@ func postLXCCMDAsyncHandlerV1(s *server) http.HandlerFunc {
291306)
292307b , _ := httperr .Marshall ()
293308log .Println (r .Method , r .URL .Path , "failed" , string (b ))
309+ httperr .WriteResponse (w )
294310return
295311}
296312in := apidef.PostLXCExecRequest {}
@@ -302,6 +318,7 @@ func postLXCCMDAsyncHandlerV1(s *server) http.HandlerFunc {
302318)
303319b , _ := httperr .Marshall ()
304320log .Println (r .Method , r .URL .Path , "failed" , string (b ))
321+ httperr .WriteResponse (w )
305322return
306323}
307324
@@ -313,6 +330,7 @@ func postLXCCMDAsyncHandlerV1(s *server) http.HandlerFunc {
313330)
314331b , _ := httperr .Marshall ()
315332log .Println (r .Method , r .URL .Path , "failed" , string (b ))
333+ httperr .WriteResponse (w )
316334return
317335}
318336
@@ -366,3 +384,92 @@ func postLXCCMDAsyncHandlerV1(s *server) http.HandlerFunc {
366384}()
367385})
368386}
387+
388+ // Get CMD execution result godoc
389+ //
390+ // @Tags CMD
391+ // @Summary Get cmd execution result.
392+ // @Description Requires VM.Audit scope. Gets a cmd execution result using the id returned by an exec-async endpoint.
393+ // @Param id path string true "execution id"
394+ // @Accept json
395+ // @Produce json
396+ // @Success 200 {object} models.CMDExecution
397+ // @Failure 400 {object} errors.HTTPError
398+ // @Failure 401 {object} errors.HTTPError
399+ // @Failure 404 {object} errors.HTTPError
400+ // @Failure 500 {object} errors.HTTPError
401+ // @Router /cmd/{id} [get]
402+ func getCMDResultHandlerV1 (s * server ) http.HandlerFunc {
403+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
404+ log .Println (r .Method , r .URL .Path , "started" )
405+ authorized , err := s .IsUserAuthorized (r , "VMS" , "VM.Audit" )
406+ if err != nil {
407+ httperr := errors .NewHTTPError (
408+ http .StatusInternalServerError ,
409+ "unable to authorize request" ,
410+ err ,
411+ )
412+ httperr .WriteResponse (w )
413+ b , _ := httperr .Marshall ()
414+ log .Println (r .Method , r .URL .Path , "failed" , string (b ))
415+ return
416+ }
417+
418+ if ! authorized {
419+ httperr := errors .NewHTTPError (
420+ http .StatusUnauthorized ,
421+ "unauthorized" ,
422+ err ,
423+ )
424+ httperr .WriteResponse (w )
425+ b , _ := httperr .Marshall ()
426+ log .Println (r .Method , r .URL .Path , "failed" , string (b ))
427+ return
428+ }
429+
430+ id := r .PathValue ("id" )
431+ if id == "" {
432+ httperr := errors .NewHTTPError (
433+ http .StatusBadRequest ,
434+ "id property is empty" ,
435+ err ,
436+ )
437+ b , _ := httperr .Marshall ()
438+ log .Println (r .Method , r .URL .Path , "failed" , string (b ))
439+ httperr .WriteResponse (w )
440+ return
441+ }
442+
443+ result , err := s .models .CMDExecution .Get (id )
444+ if err != nil {
445+ httperr := errors .NewHTTPError (
446+ http .StatusInternalServerError ,
447+ "failed to retrieve cmd result" ,
448+ err ,
449+ )
450+ b , _ := httperr .Marshall ()
451+ log .Println (r .Method , r .URL .Path , "failed" , fmt .Sprintf ("id=%s" , id ), fmt .Sprintf ("err=%s" , string (b )))
452+ httperr .WriteResponse (w )
453+ return
454+ }
455+
456+ if result == nil {
457+ httperr := errors .NewHTTPError (
458+ http .StatusNotFound ,
459+ "cmd execution result not found" ,
460+ err ,
461+ )
462+ b , _ := httperr .Marshall ()
463+ log .Println (r .Method , r .URL .Path , "failed" , fmt .Sprintf ("id=%s" , id ), fmt .Sprintf ("err=%s" , string (b )))
464+ httperr .WriteResponse (w )
465+ return
466+ }
467+
468+ b , _ := json .Marshal (result )
469+ w .Header ().Add ("content-type" , "application/json" )
470+ w .WriteHeader (http .StatusOK )
471+ w .Write (b )
472+ log .Println (r .Method , r .URL .Path , "succeeded" )
473+ return
474+ })
475+ }
0 commit comments