2 * Copyright (C) 2017-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.
24 "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent/lib/xdsconfig"
25 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1"
26 "github.com/gin-gonic/gin"
29 const apiBaseURL = "/api/v1"
32 type APIService struct {
34 apiRouter *gin.RouterGroup
38 // NewAPIV1 creates a new instance of API service
39 func NewAPIV1(ctx *Context) *APIService {
42 apiRouter: ctx.webServer.router.Group(apiBaseURL),
46 s.apiRouter.GET("/version", s.getVersion)
48 s.apiRouter.GET("/config", s.getConfig)
49 s.apiRouter.POST("/config", s.setConfig)
51 s.apiRouter.GET("/browse", s.browseFS)
53 s.apiRouter.GET("/projects", s.getProjects)
54 s.apiRouter.GET("/projects/:id", s.getProject)
55 s.apiRouter.PUT("/projects/:id", s.updateProject)
56 s.apiRouter.POST("/projects", s.addProject)
57 s.apiRouter.POST("/projects/sync/:id", s.syncProject)
58 s.apiRouter.DELETE("/projects/:id", s.delProject)
60 s.apiRouter.POST("/exec", s.execCmd)
61 s.apiRouter.POST("/exec/:id", s.execCmd)
62 s.apiRouter.POST("/signal", s.execSignalCmd)
64 s.apiRouter.GET("/events", s.eventsList)
65 s.apiRouter.POST("/events/register", s.eventsRegister)
66 s.apiRouter.POST("/events/unregister", s.eventsUnRegister)
71 // Stop Used to stop/close created services
72 func (s *APIService) Stop() {
73 for _, svr := range s.xdsServers {
78 // AddXdsServer Add a new XDS Server to the list of a server
79 func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, error) {
81 var exist, tempoID bool
84 // First check if not already exist and update it
85 if svr, exist = s.xdsServers[cfg.ID]; exist {
87 // Update: Found, so just update some settings
88 svr.ConnRetry = cfg.ConnRetry
90 tempoID = svr.IsTempoID()
91 if svr.Connected && !svr.Disabled && svr.BaseURL == cfg.URL && tempoID {
95 // URL differ or not connected, so need to reconnect
100 // Create a new server object
101 if cfg.APIBaseURL == "" {
102 cfg.APIBaseURL = apiBaseURL
104 if cfg.APIPartialURL == "" {
105 cfg.APIPartialURL = "/servers/" + strconv.Itoa(s.serverIndex)
106 s.serverIndex = s.serverIndex + 1
109 // Create a new XDS Server
110 svr = NewXdsServer(s.Context, cfg)
112 svr.SetLoggerOutput(s.Config.LogVerboseOut)
114 // Passthrough routes (handle by XDS Server)
115 grp := s.apiRouter.Group(svr.PartialURL)
116 svr.SetAPIRouterGroup(grp)
118 // Declare passthrough routes
119 s.sdksPassthroughInit(svr)
121 // Register callback on Connection
122 svr.ConnectOn(func(server *XdsServer) error {
124 // Add server to list
125 s.xdsServers[server.ID] = svr
127 // Register event forwarder
128 if err := s.sdksEventsForwardInit(server); err != nil {
129 s.Log.Errorf("XDS Server %v - sdk event forwarding error: %v", server.ID, err)
133 if err := s.projects.Init(server); err != nil {
134 s.Log.Errorf("XDS Server %v - project init error: %v", server.ID, err)
137 // Registered to all events
138 if err := server.EventRegister(xsapiv1.EVTAll, ""); err != nil {
139 s.Log.Errorf("XDS Server %v - register all events error: %v", server.ID, err)
146 // Established connection
149 // Delete temporary ID with it has been replaced by right Server ID
150 if tempoID && !svr.IsTempoID() {
151 delete(s.xdsServers, cfg.ID)
157 // DelXdsServer Delete an XDS Server from the list of a server
158 func (s *APIService) DelXdsServer(id string) error {
159 if _, exist := s.xdsServers[id]; !exist {
160 return fmt.Errorf("Unknown Server ID %s", id)
162 // Don't really delete, just disable it
163 s.xdsServers[id].Close()