aboutsummaryrefslogtreecommitdiffstats
path: root/lib/agent/apiv1.go
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2018-03-09 17:28:43 +0100
committerSebastien Douheret <sebastien.douheret@iot.bzh>2018-03-13 12:39:57 +0100
commit80708aa2578f3598fc8abd4d08e576947da22872 (patch)
tree5ec6e414a3f41bea2d157b91f7ef4ac6cb8284a0 /lib/agent/apiv1.go
parent1499569abc3e8440592add96f96e01791282a246 (diff)
Added api to list and reconnect a XDS-Server.
Added following routes: /servers => list all known XDS-Server /servers/:id => get info of a XDS-Server /servers/:id/reconnect => force reconnection of a XDS-Server Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'lib/agent/apiv1.go')
-rw-r--r--lib/agent/apiv1.go39
1 files changed, 35 insertions, 4 deletions
diff --git a/lib/agent/apiv1.go b/lib/agent/apiv1.go
index 3ca84b9..730e7c0 100644
--- a/lib/agent/apiv1.go
+++ b/lib/agent/apiv1.go
@@ -20,6 +20,7 @@ package agent
import (
"fmt"
"strconv"
+ "strings"
"gerrit.automotivelinux.org/gerrit/src/xds/xds-agent/lib/xaapiv1"
"gerrit.automotivelinux.org/gerrit/src/xds/xds-agent/lib/xdsconfig"
@@ -99,12 +100,13 @@ func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, erro
} else {
// Create a new server object
+ cfg.URLIndex = strconv.Itoa(s.serverIndex)
+ s.serverIndex = s.serverIndex + 1
if cfg.APIBaseURL == "" {
cfg.APIBaseURL = apiBaseURL
}
if cfg.APIPartialURL == "" {
- cfg.APIPartialURL = "/servers/" + strconv.Itoa(s.serverIndex)
- s.serverIndex = s.serverIndex + 1
+ cfg.APIPartialURL = "/servers/" + cfg.URLIndex
}
// Create a new XDS Server
@@ -112,11 +114,16 @@ func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, erro
svr.SetLoggerOutput(s.Config.LogVerboseOut)
- // Passthrough routes (handle by XDS Server)
+ // Define API group for this XDS Server
grp := s.apiRouter.Group(svr.PartialURL)
svr.SetAPIRouterGroup(grp)
- // Declare passthrough routes
+ // Define servers API processed locally
+ s.apiRouter.GET("/servers", s.getServersList) // API /servers
+ svr.apiRouter.GET("", s.getServer) // API /servers/:id
+ svr.apiRouter.POST("/reconnect", s.reconnectServer) // API /servers/:id/reconnect
+
+ // Declare passthrough API/routes
s.sdksPassthroughInit(svr)
s.targetsPassthroughInit(svr)
@@ -179,3 +186,27 @@ func (s *APIService) UpdateXdsServer(cfg xaapiv1.ServerCfg) error {
return nil
}
+
+// GetXdsServerFromURLIndex Retrieve XdsServer from URLIndex value
+func (s *APIService) GetXdsServerFromURLIndex(urlIdx string) *XdsServer {
+ for _, svr := range s.xdsServers {
+ if svr.URLIndex == urlIdx {
+ return svr
+ }
+ }
+ return nil
+}
+
+// ParamGetIndex Retrieve numerical parameter in request url
+func (s *APIService) ParamGetIndex(c *gin.Context) string {
+ uri := c.Request.RequestURI
+ for idx := strings.LastIndex(uri, "/"); idx > 0; {
+ id := uri[idx+1:]
+ if _, err := strconv.Atoi(id); err == nil {
+ return id
+ }
+ uri = uri[:idx]
+ idx = strings.LastIndex(uri, "/")
+ }
+ return ""
+}