aboutsummaryrefslogtreecommitdiffstats
path: root/lib/xdsserver/apiv1-targets.go
blob: a7048eb8a0b2d86827a15baaf30cbf8a18328192 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright (C) 2018 "IoT.bzh"
 * Author Sebastien Douheret <sebastien@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package xdsserver

import (
	"net/http"

	common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
	"gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
	"github.com/gin-gonic/gin"
)

/***
 * Targets
 ***/

// getTargets returns all targets configuration
func (s *APIService) getTargets(c *gin.Context) {
	c.JSON(http.StatusOK, s.targets.GetConfigArr())
}

// getTarget returns a specific target configuration
func (s *APIService) getTarget(c *gin.Context) {
	id, err := s.targets.ResolveID(c.Param("id"))
	if err != nil {
		common.APIError(c, err.Error())
		return
	}
	f := s.targets.Get(id)
	if f == nil {
		common.APIError(c, "Invalid id")
		return
	}

	c.JSON(http.StatusOK, (*f).GetConfig())
}

// addTarget adds a new target to server config
func (s *APIService) addTarget(c *gin.Context) {
	var cfgArg xsapiv1.TargetConfig
	if c.BindJSON(&cfgArg) != nil {
		common.APIError(c, "Invalid arguments")
		return
	}

	// Retrieve session info
	sess := s.sessions.Get(c)
	if sess == nil {
		common.APIError(c, "Unknown sessions")
		return
	}

	s.Log.Debugln("Add target config: ", cfgArg)

	newTgt, err := s.targets.Add(cfgArg, sess)
	if err != nil {
		common.APIError(c, err.Error())
		return
	}

	c.JSON(http.StatusOK, newTgt)
}

// delTarget deletes target from server config
func (s *APIService) delTarget(c *gin.Context) {
	id, err := s.targets.ResolveID(c.Param("id"))
	if err != nil {
		common.APIError(c, err.Error())
		return
	}

	// Retrieve session info
	sess := s.sessions.Get(c)
	if sess == nil {
		common.APIError(c, "Unknown sessions")
		return
	}

	s.Log.Debugln("Delete target id ", id)

	delEntry, err := s.targets.Delete(id, sess)
	if err != nil {
		common.APIError(c, err.Error())
		return
	}
	c.JSON(http.StatusOK, delEntry)
}

/***
 * Terminals
 ***/
// getTgtTerms Get list of all terminals
func (s *APIService) getTgtTerms(c *gin.Context) {
	id, err := s.targets.ResolveID(c.Param("id"))
	if err != nil {
		common.APIError(c, err.Error())
		return
	}

	res, err := s.targets.GetTerminalsArr(id)
	if err != nil {
		common.APIError(c, err.Error())
		return
	}

	c.JSON(http.StatusOK, res)
}

// getTgtTerm Get info a terminal
func (s *APIService) getTgtTerm(c *gin.Context) {
	id, tid, err := s._decodeTermArgs(c)
	if err != nil {
		common.APIError(c, err.Error())
		return
	}

	iTerm, err := s.targets.GetTerminal(id, tid)
	if err != nil {
		common.APIError(c, err.Error())
		return
	}

	c.JSON(http.StatusOK, (*iTerm).GetConfig())
}

// createTgtTerm Create a new terminal
func (s *APIService) createTgtTerm(c *gin.Context) {
	s.updateTgtTerm(c)
}

// updateTgtTerm Update terminal config
func (s *APIService) updateTgtTerm(c *gin.Context) {
	var cfgArg xsapiv1.TerminalConfig

	tgtID, termID, err := s._decodeTermArgs(c)
	if tgtID == "" && err != nil {
		common.APIError(c, err.Error())
		return
	}
	if err := c.BindJSON(&cfgArg); err != nil {
		common.APIError(c, "Invalid arguments")
		return
	}
	if cfgArg.ID == "" {
		cfgArg.ID = termID
	}
	if termID != "" && cfgArg.ID != termID {
		common.APIError(c, "Invalid arguments, inconsistent terminal id ")
		return
	}
	s.Log.Debugln("Add or Update terminal config: ", cfgArg)
	term, err := s.targets.CreateUpdateTerminal(tgtID, cfgArg, false)
	if err != nil {
		common.APIError(c, err.Error())
		return
	}
	c.JSON(http.StatusOK, term)
}

// delTgtTerm Delete a terminal
func (s *APIService) delTgtTerm(c *gin.Context) {

	tgtID, termID, err := s._decodeTermArgs(c)
	if err != nil {
		common.APIError(c, err.Error())
		return
	}
	term, err := s.targets.DeleteTerminal(tgtID, termID)
	if err != nil {
		common.APIError(c, err.Error())
		return
	}

	c.JSON(http.StatusOK, term)
}

// openTgtTerm Open a target terminal/console
func (s *APIService) openTgtTerm(c *gin.Context) {

	id, tid, err := s._decodeTermArgs(c)
	if err != nil {
		common.APIError(c, err.Error())
		return
	}

	// Retrieve session info
	sess := s.sessions.Get(c)
	if sess == nil {
		common.APIError(c, "Unknown sessions")
		return
	}

	term, err := s.targets.OpenTerminal(id, tid, sess)
	if err != nil {
		common.APIError(c, err.Error())
		return
	}
	c.JSON(http.StatusOK, term)
}

// closeTgtTerm Close a terminal
func (s *APIService) closeTgtTerm(c *gin.Context) {
	id, tid, err := s._decodeTermArgs(c)
	if err != nil {
		common.APIError(c, err.Error())
		return
	}

	// Retrieve session info
	sess := s.sessions.Get(c)
	if sess == nil {
		common.APIError(c, "Unknown sessions")
		return
	}

	term, err := s.targets.CloseTerminal(id, tid, sess)
	if err != nil {
		common.APIError(c, err.Error())
		return
	}
	c.JSON(http.StatusOK, term)
}

// resizeTgtTerm Resize a terminal
func (s *APIService) resizeTgtTerm(c *gin.Context) {
	var sizeArg xsapiv1.TerminalResizeArgs

	id, tid, err := s._decodeTermArgs(c)
	if err != nil {
		common.APIError(c, err.Error())
		return
	}
	if err := c.BindJSON(&sizeArg); err != nil {
		common.APIError(c, "Invalid arguments")
		return
	}

	// Retrieve session info
	sess := s.sessions.Get(c)
	if sess == nil {
		common.APIError(c, "Unknown sessions")
		return
	}

	term, err := s.targets.ResizeTerminal(id, tid, sizeArg.Cols, sizeArg.Rows, sess)
	if err != nil {
		common.APIError(c, err.Error())
		return
	}
	c.JSON(http.StatusOK, term)
}

// signalTgtTerm Send a signal to a terminal
func (s *APIService) signalTgtTerm(c *gin.Context) {
	var sigArg xsapiv1.TerminalSignalArgs

	id, tid, err := s._decodeTermArgs(c)
	if err != nil {
		common.APIError(c, err.Error())
		return
	}

	sigName := c.Param("sig")
	if sigName == "" {
		if err := c.BindJSON(&sigArg); err != nil {
			common.APIError(c, "Invalid arguments")
			return
		}
		sigName = sigArg.Signal
	}
	if sigName == "" {
		common.APIError(c, "Invalid arguments")
		return
	}

	if err := s.targets.SignalTerminal(id, tid, sigName); err != nil {
		common.APIError(c, err.Error())
		return
	}

	c.JSON(http.StatusOK, "")
}

// _decodeTermArgs Helper to decode arguments of Terminal routes
func (s *APIService) _decodeTermArgs(c *gin.Context) (string, string, error) {
	id, err := s.targets.ResolveID(c.Param("id"))
	if err != nil {
		return "", "", err
	}

	termID, err := s.targets.ResolveTerminalID(c.Param("tid"))
	if err != nil {
		return id, "", err
	}

	return id, termID, nil
}