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/xaapiv1"
25 "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent/lib/xdsconfig"
26 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1"
27 "github.com/gin-gonic/gin"
30 const apiBaseURL = "/api/v1"
33 type APIService struct {
35 apiRouter *gin.RouterGroup
39 // NewAPIV1 creates a new instance of API service
40 func NewAPIV1(ctx *Context) *APIService {
43 apiRouter: ctx.webServer.router.Group(apiBaseURL),
47 s.apiRouter.GET("/version", s.getVersion)
49 s.apiRouter.GET("/config", s.getConfig)
50 s.apiRouter.POST("/config", s.setConfig)
52 s.apiRouter.GET("/browse", s.browseFS)
54 s.apiRouter.GET("/projects", s.getProjects)
55 s.apiRouter.GET("/projects/:id", s.getProject)
56 s.apiRouter.PUT("/projects/:id", s.updateProject)
57 s.apiRouter.POST("/projects", s.addProject)
58 s.apiRouter.POST("/projects/sync/:id", s.syncProject)
59 s.apiRouter.DELETE("/projects/:id", s.delProject)
61 s.apiRouter.POST("/exec", s.execCmd)
62 s.apiRouter.POST("/exec/:id", s.execCmd)
63 s.apiRouter.POST("/signal", s.execSignalCmd)
65 s.apiRouter.GET("/events", s.eventsList)
66 s.apiRouter.POST("/events/register", s.eventsRegister)
67 s.apiRouter.POST("/events/unregister", s.eventsUnRegister)
72 // Stop Used to stop/close created services
73 func (s *APIService) Stop() {
74 for _, svr := range s.xdsServers {
79 // AddXdsServer Add a new XDS Server to the list of a server
80 func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, error) {
82 var exist, tempoID bool
85 // First check if not already exist and update it
86 if svr, exist = s.xdsServers[cfg.ID]; exist {
88 // Update: Found, so just update some settings
89 svr.ConnRetry = cfg.ConnRetry
91 tempoID = svr.IsTempoID()
92 if svr.Connected && !svr.Disabled && svr.BaseURL == cfg.URL && tempoID {
96 // URL differ or not connected, so need to reconnect
101 // Create a new server object
102 if cfg.APIBaseURL == "" {
103 cfg.APIBaseURL = apiBaseURL
105 if cfg.APIPartialURL == "" {
106 cfg.APIPartialURL = "/servers/" + strconv.Itoa(s.serverIndex)
107 s.serverIndex = s.serverIndex + 1
110 // Create a new XDS Server
111 svr = NewXdsServer(s.Context, cfg)
113 svr.SetLoggerOutput(s.Config.LogVerboseOut)
115 // Passthrough routes (handle by XDS Server)
116 grp := s.apiRouter.Group(svr.PartialURL)
117 svr.SetAPIRouterGroup(grp)
119 // Declare passthrough routes
120 s.sdksPassthroughInit(svr)
122 // Register callback on Connection
123 svr.ConnectOn(func(server *XdsServer) error {
125 // Add server to list
126 s.xdsServers[server.ID] = svr
128 // Register event forwarder
129 if err := s.sdksEventsForwardInit(server); err != nil {
130 s.Log.Errorf("XDS Server %v - sdk event forwarding error: %v", server.ID, err)
134 if err := s.projects.Init(server); err != nil {
135 s.Log.Errorf("XDS Server %v - project init error: %v", server.ID, err)
138 // Registered to all events
139 if err := server.EventRegister(xsapiv1.EVTAll, ""); err != nil {
140 s.Log.Errorf("XDS Server %v - register all events error: %v", server.ID, err)
147 // Established connection
150 // Delete temporary ID with it has been replaced by right Server ID
151 if tempoID && !svr.IsTempoID() {
152 delete(s.xdsServers, cfg.ID)
158 // DelXdsServer Delete an XDS Server from the list of a server
159 func (s *APIService) DelXdsServer(id string) error {
160 if _, exist := s.xdsServers[id]; !exist {
161 return fmt.Errorf("Unknown Server ID %s", id)
163 // Don't really delete, just disable it
164 s.xdsServers[id].Close()
169 // UpdateXdsServer Update XDS Server configuration settings
170 func (s *APIService) UpdateXdsServer(cfg xaapiv1.ServerCfg) error {
171 if _, exist := s.xdsServers[cfg.ID]; !exist {
172 return fmt.Errorf("Unknown Server ID %s", cfg.ID)
175 svr := s.xdsServers[cfg.ID]
177 // Update only some configurable fields
178 svr.ConnRetry = cfg.ConnRetry