Added target and terminal support.
[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
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"
28 )
29
30 const apiBaseURL = "/api/v1"
31
32 // APIService .
33 type APIService struct {
34         *Context
35         apiRouter   *gin.RouterGroup
36         serverIndex int
37 }
38
39 // NewAPIV1 creates a new instance of API service
40 func NewAPIV1(ctx *Context) *APIService {
41         s := &APIService{
42                 Context:     ctx,
43                 apiRouter:   ctx.webServer.router.Group(apiBaseURL),
44                 serverIndex: 0,
45         }
46
47         s.apiRouter.GET("/version", s.getVersion)
48
49         s.apiRouter.GET("/config", s.getConfig)
50         s.apiRouter.POST("/config", s.setConfig)
51
52         // s.apiRouter.GET("/browse", s.browseFS)
53
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)
60
61         s.apiRouter.POST("/exec", s.execCmd)
62         s.apiRouter.POST("/exec/:id", s.execCmd)
63         s.apiRouter.POST("/signal", s.execSignalCmd)
64
65         s.apiRouter.GET("/events", s.eventsList)
66         s.apiRouter.POST("/events/register", s.eventsRegister)
67         s.apiRouter.POST("/events/unregister", s.eventsUnRegister)
68
69         return s
70 }
71
72 // Stop Used to stop/close created services
73 func (s *APIService) Stop() {
74         for _, svr := range s.xdsServers {
75                 svr.Close()
76         }
77 }
78
79 // AddXdsServer Add a new XDS Server to the list of a server
80 func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, error) {
81         var svr *XdsServer
82         var exist, tempoID bool
83         tempoID = false
84
85         // First check if not already exist and update it
86         if svr, exist = s.xdsServers[cfg.ID]; exist {
87
88                 // Update: Found, so just update some settings
89                 svr.ConnRetry = cfg.ConnRetry
90
91                 tempoID = svr.IsTempoID()
92                 if svr.Connected && !svr.Disabled && svr.BaseURL == cfg.URL && tempoID {
93                         return svr, nil
94                 }
95
96                 // URL differ or not connected, so need to reconnect
97                 svr.BaseURL = cfg.URL
98
99         } else {
100
101                 // Create a new server object
102                 if cfg.APIBaseURL == "" {
103                         cfg.APIBaseURL = apiBaseURL
104                 }
105                 if cfg.APIPartialURL == "" {
106                         cfg.APIPartialURL = "/servers/" + strconv.Itoa(s.serverIndex)
107                         s.serverIndex = s.serverIndex + 1
108                 }
109
110                 // Create a new XDS Server
111                 svr = NewXdsServer(s.Context, cfg)
112
113                 svr.SetLoggerOutput(s.Config.LogVerboseOut)
114
115                 // Passthrough routes (handle by XDS Server)
116                 grp := s.apiRouter.Group(svr.PartialURL)
117                 svr.SetAPIRouterGroup(grp)
118
119                 // Declare passthrough routes
120                 s.sdksPassthroughInit(svr)
121                 s.targetsPassthroughInit(svr)
122
123                 // Register callback on Connection
124                 svr.ConnectOn(func(server *XdsServer) error {
125
126                         // Add server to list
127                         s.xdsServers[server.ID] = svr
128
129                         // Register events forwarder
130                         if err := s.sdksEventsForwardInit(server); err != nil {
131                                 s.Log.Errorf("XDS Server %v - sdk events forwarding error: %v", server.ID, err)
132                         }
133
134                         // Load projects
135                         if err := s.projects.Init(server); err != nil {
136                                 s.Log.Errorf("XDS Server %v - project init error: %v", server.ID, err)
137                         }
138
139                         // Registered to all events
140                         if err := server.EventRegister(xsapiv1.EVTAll, ""); err != nil {
141                                 s.Log.Errorf("XDS Server %v - register all events error: %v", server.ID, err)
142                         }
143
144                         return nil
145                 })
146         }
147
148         // Established connection
149         err := svr.Connect()
150
151         // Delete temporary ID with it has been replaced by right Server ID
152         if tempoID && !svr.IsTempoID() {
153                 delete(s.xdsServers, cfg.ID)
154         }
155
156         return svr, err
157 }
158
159 // DelXdsServer Delete an XDS Server from the list of a server
160 func (s *APIService) DelXdsServer(id string) error {
161         if _, exist := s.xdsServers[id]; !exist {
162                 return fmt.Errorf("Unknown Server ID %s", id)
163         }
164         // Don't really delete, just disable it
165         s.xdsServers[id].Close()
166         return nil
167 }
168
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)
173         }
174
175         svr := s.xdsServers[cfg.ID]
176
177         // Update only some configurable fields
178         svr.ConnRetry = cfg.ConnRetry
179
180         return nil
181 }