X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=src%2Fxds%2Fxds-agent.git;a=blobdiff_plain;f=lib%2Fagent%2Fapiv1.go;h=730e7c0f1b18e8e5ff175826a6bc0bce067942a8;hp=3ca84b954c84ff35e553070a9497d46daee9a2ed;hb=80708aa2578f3598fc8abd4d08e576947da22872;hpb=1499569abc3e8440592add96f96e01791282a246 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 "" +}