2 * Copyright (C) 2018 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
23 common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
24 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
25 "github.com/gin-gonic/gin"
32 // getTargets returns all targets configuration
33 func (s *APIService) getTargets(c *gin.Context) {
34 c.JSON(http.StatusOK, s.targets.GetConfigArr())
37 // getTarget returns a specific target configuration
38 func (s *APIService) getTarget(c *gin.Context) {
39 id, err := s.targets.ResolveID(c.Param("id"))
41 common.APIError(c, err.Error())
44 f := s.targets.Get(id)
46 common.APIError(c, "Invalid id")
50 c.JSON(http.StatusOK, (*f).GetConfig())
53 // addTarget adds a new target to server config
54 func (s *APIService) addTarget(c *gin.Context) {
55 var cfgArg xsapiv1.TargetConfig
56 if c.BindJSON(&cfgArg) != nil {
57 common.APIError(c, "Invalid arguments")
61 s.Log.Debugln("Add target config: ", cfgArg)
63 newTgt, err := s.targets.Add(cfgArg)
65 common.APIError(c, err.Error())
69 c.JSON(http.StatusOK, newTgt)
72 // delTarget deletes target from server config
73 func (s *APIService) delTarget(c *gin.Context) {
74 id, err := s.targets.ResolveID(c.Param("id"))
76 common.APIError(c, err.Error())
80 s.Log.Debugln("Delete target id ", id)
82 delEntry, err := s.targets.Delete(id)
84 common.APIError(c, err.Error())
87 c.JSON(http.StatusOK, delEntry)
93 // getTgtTerms Get list of all terminals
94 func (s *APIService) getTgtTerms(c *gin.Context) {
95 id, err := s.targets.ResolveID(c.Param("id"))
97 common.APIError(c, err.Error())
101 res, err := s.targets.GetTerminalsArr(id)
103 common.APIError(c, err.Error())
107 c.JSON(http.StatusOK, res)
110 // getTgtTerm Get info a terminal
111 func (s *APIService) getTgtTerm(c *gin.Context) {
112 id, tid, err := s._decodeTermArgs(c)
114 common.APIError(c, err.Error())
118 iTerm, err := s.targets.GetTerminal(id, tid)
120 common.APIError(c, err.Error())
124 c.JSON(http.StatusOK, (*iTerm).GetConfig())
127 // createTgtTerm Create a new terminal
128 func (s *APIService) createTgtTerm(c *gin.Context) {
132 // updateTgtTerm Update terminal config
133 func (s *APIService) updateTgtTerm(c *gin.Context) {
134 var cfgArg xsapiv1.TerminalConfig
136 tgtID, termID, err := s._decodeTermArgs(c)
137 if tgtID == "" && err != nil {
138 common.APIError(c, err.Error())
141 if err := c.BindJSON(&cfgArg); err != nil {
142 common.APIError(c, "Invalid arguments")
148 if termID != "" && cfgArg.ID != termID {
149 common.APIError(c, "Invalid arguments, inconsistent terminal id ")
152 s.Log.Debugln("Add or Update terminal config: ", cfgArg)
153 term, err := s.targets.CreateUpdateTerminal(tgtID, cfgArg, false)
155 common.APIError(c, err.Error())
158 c.JSON(http.StatusOK, term)
161 // delTgtTerm Delete a terminal
162 func (s *APIService) delTgtTerm(c *gin.Context) {
164 tgtID, termID, err := s._decodeTermArgs(c)
166 common.APIError(c, err.Error())
169 term, err := s.targets.DeleteTerminal(tgtID, termID)
171 common.APIError(c, err.Error())
175 c.JSON(http.StatusOK, term)
178 // openTgtTerm Open a target terminal/console
179 func (s *APIService) openTgtTerm(c *gin.Context) {
181 id, tid, err := s._decodeTermArgs(c)
183 common.APIError(c, err.Error())
187 // Retrieve session info
188 sess := s.sessions.Get(c)
190 common.APIError(c, "Unknown sessions")
193 sock := sess.IOSocket
195 common.APIError(c, "Websocket not established")
199 term, err := s.targets.OpenTerminal(id, tid, sock, sess.ID)
201 common.APIError(c, err.Error())
204 c.JSON(http.StatusOK, term)
207 // closeTgtTerm Close a terminal
208 func (s *APIService) closeTgtTerm(c *gin.Context) {
209 id, tid, err := s._decodeTermArgs(c)
211 common.APIError(c, err.Error())
214 term, err := s.targets.CloseTerminal(id, tid)
216 common.APIError(c, err.Error())
219 c.JSON(http.StatusOK, term)
222 // resizeTgtTerm Resize a terminal
223 func (s *APIService) resizeTgtTerm(c *gin.Context) {
224 var sizeArg xsapiv1.TerminalResizeArgs
226 id, tid, err := s._decodeTermArgs(c)
228 common.APIError(c, err.Error())
231 if err := c.BindJSON(&sizeArg); err != nil {
232 common.APIError(c, "Invalid arguments")
236 term, err := s.targets.ResizeTerminal(id, tid, sizeArg.Cols, sizeArg.Rows)
238 common.APIError(c, err.Error())
241 c.JSON(http.StatusOK, term)
244 // signalTgtTerm Send a signal to a terminal
245 func (s *APIService) signalTgtTerm(c *gin.Context) {
246 var sigArg xsapiv1.TerminalSignalArgs
248 id, tid, err := s._decodeTermArgs(c)
250 common.APIError(c, err.Error())
254 sigName := c.Param("sig")
256 if err := c.BindJSON(&sigArg); err != nil {
257 common.APIError(c, "Invalid arguments")
260 sigName = sigArg.Signal
263 common.APIError(c, "Invalid arguments")
267 if err := s.targets.SignalTerminal(id, tid, sigName); err != nil {
268 common.APIError(c, err.Error())
272 c.JSON(http.StatusOK, "")
275 // _decodeTermArgs Helper to decode arguments of Terminal routes
276 func (s *APIService) _decodeTermArgs(c *gin.Context) (string, string, error) {
277 id, err := s.targets.ResolveID(c.Param("id"))
282 termID, err := s.targets.ResolveTerminalID(c.Param("tid"))
287 return id, termID, nil