Use go module as dependency tool instead of glide
[src/xds/xds-agent.git] / lib / agent / apiv1.go
1 /*
2  * Copyright (C) 2017-2018 "IoT.bzh"
3  * Author Sebastien Douheret <sebastien@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package agent
19
20 import (
21         "fmt"
22         "strconv"
23         "strings"
24
25         "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1"
26         "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xdsconfig"
27         "gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1"
28         "github.com/gin-gonic/gin"
29 )
30
31 const apiBaseURL = "/api/v1"
32
33 // APIService .
34 type APIService struct {
35         *Context
36         apiRouter   *gin.RouterGroup
37         serverIndex int
38 }
39
40 // NewAPIV1 creates a new instance of API service
41 func NewAPIV1(ctx *Context) *APIService {
42         s := &APIService{
43                 Context:     ctx,
44                 apiRouter:   ctx.webServer.router.Group(apiBaseURL),
45                 serverIndex: 0,
46         }
47
48         s.apiRouter.GET("/version", s.getVersion)
49
50         s.apiRouter.GET("/config", s.getConfig)
51         s.apiRouter.POST("/config", s.setConfig)
52
53         // s.apiRouter.GET("/browse", s.browseFS)
54
55         s.apiRouter.GET("/projects", s.getProjects)
56         s.apiRouter.GET("/projects/:id", s.getProject)
57         s.apiRouter.PUT("/projects/:id", s.updateProject)
58         s.apiRouter.POST("/projects", s.addProject)
59         s.apiRouter.POST("/projects/sync/:id", s.syncProject)
60         s.apiRouter.DELETE("/projects/:id", s.delProject)
61
62         s.apiRouter.POST("/exec", s.execCmd)
63         s.apiRouter.POST("/exec/:id", s.execCmd)
64         s.apiRouter.POST("/signal", s.execSignalCmd)
65
66         s.apiRouter.GET("/events", s.eventsList)
67         s.apiRouter.POST("/events/register", s.eventsRegister)
68         s.apiRouter.POST("/events/unregister", s.eventsUnRegister)
69
70         s.apiRouter.GET("/supervisor/topo", s.getSupervisorTopo)
71         s.apiRouter.POST("/supervisor/trace/start", s.startSupervisor)
72         s.apiRouter.POST("/supervisor/trace/stop", s.stopSupervisor)
73         return s
74 }
75
76 // Stop Used to stop/close created services
77 func (s *APIService) Stop() {
78         for _, svr := range s.xdsServers {
79                 svr.Close()
80         }
81 }
82
83 // AddXdsServer Add a new XDS Server to the list of a server
84 func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, error) {
85         var svr *XdsServer
86         var exist, tempoID bool
87         tempoID = false
88
89         // First check if not already exist and update it
90         if svr, exist = s.xdsServers[cfg.ID]; exist {
91
92                 // Update: Found, so just update some settings
93                 svr.ConnRetry = cfg.ConnRetry
94
95                 tempoID = svr.IsTempoID()
96                 if svr.Connected && !svr.Disabled && svr.BaseURL == cfg.URL && tempoID {
97                         return svr, nil
98                 }
99
100                 // URL differ or not connected, so need to reconnect
101                 svr.BaseURL = cfg.URL
102
103         } else {
104
105                 // Create a new server object
106                 cfg.URLIndex = strconv.Itoa(s.serverIndex)
107                 s.serverIndex = s.serverIndex + 1
108                 if cfg.APIBaseURL == "" {
109                         cfg.APIBaseURL = apiBaseURL
110                 }
111                 if cfg.APIPartialURL == "" {
112                         cfg.APIPartialURL = "/servers/" + cfg.URLIndex
113                 }
114
115                 // Create a new XDS Server
116                 svr = NewXdsServer(s.Context, cfg)
117
118                 svr.SetLoggerOutput(s.Config.LogVerboseOut)
119
120                 // Define API group for this XDS Server
121                 grp := s.apiRouter.Group(svr.PartialURL)
122                 svr.SetAPIRouterGroup(grp)
123
124                 // Define servers API processed locally
125                 s.apiRouter.GET("/servers", s.getServersList)       // API /servers
126                 svr.apiRouter.GET("", s.getServer)                  // API /servers/:id
127                 svr.apiRouter.POST("/reconnect", s.reconnectServer) // API /servers/:id/reconnect
128
129                 // Declare passthrough API/routes
130                 s.sdksPassthroughInit(svr)
131                 s.targetsPassthroughInit(svr)
132
133                 // Register callback on Connection
134                 svr.ConnectOn(func(server *XdsServer) error {
135
136                         // Add server to list
137                         s.xdsServers[server.ID] = svr
138
139                         // Register events forwarder
140                         if err := s.sdksEventsForwardInit(server); err != nil {
141                                 s.Log.Errorf("XDS Server %v - sdk events forwarding error: %v", server.ID, err)
142                         }
143                         if err := s.targetsEventsForwardInit(server); err != nil {
144                                 s.Log.Errorf("XDS Server %v - target events forwarding error: %v", server.ID, err)
145                         }
146                         if err := s.terminalsEventsForwardInit(server); err != nil {
147                                 s.Log.Errorf("XDS Server %v - terminal events forwarding error: %v", server.ID, err)
148                         }
149
150                         // Load projects
151                         if err := s.projects.Init(server); err != nil {
152                                 s.Log.Errorf("XDS Server %v - project init error: %v", server.ID, err)
153                         }
154
155                         // Registered to all events
156                         if err := server.EventRegister(xsapiv1.EVTAll, ""); err != nil {
157                                 s.Log.Errorf("XDS Server %v - register all events error: %v", server.ID, err)
158                         }
159
160                         return nil
161                 })
162         }
163
164         // Established connection
165         err := svr.Connect()
166
167         // Delete temporary ID with it has been replaced by right Server ID
168         if tempoID && !svr.IsTempoID() {
169                 delete(s.xdsServers, cfg.ID)
170         }
171
172         return svr, err
173 }
174
175 // DelXdsServer Delete an XDS Server from the list of a server
176 func (s *APIService) DelXdsServer(id string) error {
177         if _, exist := s.xdsServers[id]; !exist {
178                 return fmt.Errorf("Unknown Server ID %s", id)
179         }
180         // Don't really delete, just disable it
181         s.xdsServers[id].Close()
182         return nil
183 }
184
185 // UpdateXdsServer Update XDS Server configuration settings
186 func (s *APIService) UpdateXdsServer(cfg xaapiv1.ServerCfg) error {
187         if _, exist := s.xdsServers[cfg.ID]; !exist {
188                 return fmt.Errorf("Unknown Server ID %s", cfg.ID)
189         }
190
191         svr := s.xdsServers[cfg.ID]
192
193         // Update only some configurable fields
194         svr.ConnRetry = cfg.ConnRetry
195
196         return nil
197 }
198
199 // GetXdsServerFromURLIndex Retrieve XdsServer from URLIndex value
200 func (s *APIService) GetXdsServerFromURLIndex(urlIdx string) *XdsServer {
201         for _, svr := range s.xdsServers {
202                 if svr.URLIndex == urlIdx {
203                         return svr
204                 }
205         }
206         return nil
207 }
208
209 // ParamGetIndex Retrieve numerical parameter in request url
210 func (s *APIService) ParamGetIndex(c *gin.Context) string {
211         uri := c.Request.RequestURI
212         for idx := strings.LastIndex(uri, "/"); idx > 0; {
213                 id := uri[idx+1:]
214                 if _, err := strconv.Atoi(id); err == nil {
215                         return id
216                 }
217                 uri = uri[:idx]
218                 idx = strings.LastIndex(uri, "/")
219         }
220         return ""
221 }