summaryrefslogtreecommitdiffstats
path: root/lib/agent/xdsserver.go
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2018-02-23 18:50:31 +0100
committerSebastien Douheret <sebastien.douheret@iot.bzh>2018-02-23 19:15:02 +0100
commitbefb10519daefcbee85950a4e574678fe4b7f402 (patch)
treeb4f95889cfacbdf9909c3ccd11dc1f0f8bedb417 /lib/agent/xdsserver.go
parent38e0baedab9da4516cdc97b526392cbc2c7da67e (diff)
Added target and terminal support.v1.1.0
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'lib/agent/xdsserver.go')
-rw-r--r--lib/agent/xdsserver.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/agent/xdsserver.go b/lib/agent/xdsserver.go
index 1c715f6..c08bfb1 100644
--- a/lib/agent/xdsserver.go
+++ b/lib/agent/xdsserver.go
@@ -208,6 +208,22 @@ func (xs *XdsServer) CommandSignal(args *xsapiv1.ExecSignalArgs, res *xsapiv1.Ex
return xs.client.Post("/signal", args, res)
}
+// CommandTgtTerminalGet Send GET request to retrieve info of a target terminals
+func (xs *XdsServer) CommandTgtTerminalGet(targetID, termID string, res *xsapiv1.TerminalConfig) error {
+ return xs.client.Get("/targets/"+targetID+"/terminals/"+termID, res)
+}
+
+// CommandTgtTerminalOpen Send POST request to open a target terminal
+func (xs *XdsServer) CommandTgtTerminalOpen(targetID string, termID string, res *xsapiv1.TerminalConfig) error {
+ var empty interface{}
+ return xs.client.Post("/targets/"+targetID+"/terminals/"+termID+"/open", &empty, res)
+}
+
+// CommandTgtTerminalSignal Send POST request to send a signal to a target terminal
+func (xs *XdsServer) CommandTgtTerminalSignal(args *xsapiv1.TerminalSignalArgs, res *xsapiv1.TerminalConfig) error {
+ return xs.client.Post("/signal", args, res)
+}
+
// SetAPIRouterGroup .
func (xs *XdsServer) SetAPIRouterGroup(r *gin.RouterGroup) {
xs.apiRouter = r
@@ -290,6 +306,56 @@ func (xs *XdsServer) PassthroughPost(url string) {
})
}
+// PassthroughPut Used to declare a route that sends directly a PUT request to XDS Server
+func (xs *XdsServer) PassthroughPut(url string) {
+ if xs.apiRouter == nil {
+ xs.Log.Errorf("apiRouter not set !")
+ return
+ }
+
+ xs.apiRouter.PUT(url, func(c *gin.Context) {
+ var err error
+ var data interface{}
+
+ // Get raw body
+ body, err := c.GetRawData()
+ if err != nil {
+ common.APIError(c, err.Error())
+ return
+ }
+
+ // Take care of param (eg. id in /projects/:id)
+ nURL := url
+ if strings.Contains(url, ":") {
+ nURL = strings.TrimPrefix(c.Request.URL.Path, xs.APIURL)
+ }
+
+ // Send Put request
+ response, err := xs.client.HTTPPutWithRes(nURL, string(body))
+ if err != nil {
+ goto httpError
+ }
+ if response.StatusCode != 200 {
+ err = fmt.Errorf(response.Status)
+ return
+ }
+ err = json.Unmarshal(xs.client.ResponseToBArray(response), &data)
+ if err != nil {
+ goto httpError
+ }
+
+ c.JSON(http.StatusOK, data)
+ return
+
+ /* Handle error case */
+ httpError:
+ if strings.Contains(err.Error(), "connection refused") {
+ xs._Disconnected()
+ }
+ common.APIError(c, err.Error())
+ })
+}
+
// PassthroughDelete Used to declare a route that sends directly a Delete request to XDS Server
func (xs *XdsServer) PassthroughDelete(url string) {
if xs.apiRouter == nil {
@@ -660,6 +726,9 @@ func (xs *XdsServer) _SocketConnect() error {
// _Disconnected Set XDS Server as disconnected
func (xs *XdsServer) _Disconnected() error {
// Clear all register events as socket is closed
+ xs.sockEventsLock.Lock()
+ defer xs.sockEventsLock.Unlock()
+
for k := range xs.sockEvents {
delete(xs.sockEvents, k)
}