Use plural nouns for all routes.
[src/xds/xds-agent.git] / lib / agent / apiv1.go
1 package agent
2
3 import (
4         "fmt"
5         "strconv"
6
7         "github.com/gin-gonic/gin"
8         "github.com/iotbzh/xds-agent/lib/xdsconfig"
9 )
10
11 const apiBaseUrl = "/api/v1"
12
13 // APIService .
14 type APIService struct {
15         *Context
16         apiRouter   *gin.RouterGroup
17         serverIndex int
18 }
19
20 // NewAPIV1 creates a new instance of API service
21 func NewAPIV1(ctx *Context) *APIService {
22         s := &APIService{
23                 Context:     ctx,
24                 apiRouter:   ctx.webServer.router.Group(apiBaseUrl),
25                 serverIndex: 0,
26         }
27
28         s.apiRouter.GET("/version", s.getVersion)
29
30         s.apiRouter.GET("/config", s.getConfig)
31         s.apiRouter.POST("/config", s.setConfig)
32
33         s.apiRouter.GET("/browse", s.browseFS)
34
35         s.apiRouter.GET("/projects", s.getProjects)
36         s.apiRouter.GET("/projects/:id", s.getProject)
37         s.apiRouter.POST("/projects", s.addProject)
38         s.apiRouter.POST("/projects/sync/:id", s.syncProject)
39         s.apiRouter.DELETE("/projects/:id", s.delProject)
40
41         s.apiRouter.POST("/exec", s.execCmd)
42         s.apiRouter.POST("/exec/:id", s.execCmd)
43         s.apiRouter.POST("/signal", s.execSignalCmd)
44
45         s.apiRouter.GET("/events", s.eventsList)
46         s.apiRouter.POST("/events/register", s.eventsRegister)
47         s.apiRouter.POST("/events/unregister", s.eventsUnRegister)
48
49         return s
50 }
51
52 // Stop Used to stop/close created services
53 func (s *APIService) Stop() {
54         for _, svr := range s.xdsServers {
55                 svr.Close()
56         }
57 }
58
59 // AddXdsServer Add a new XDS Server to the list of a server
60 func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, error) {
61         var svr *XdsServer
62         var exist, tempoID bool
63         tempoID = false
64
65         // First check if not already exist and update it
66         if svr, exist = s.xdsServers[cfg.ID]; exist {
67
68                 // Update: Found, so just update some settings
69                 svr.ConnRetry = cfg.ConnRetry
70
71                 tempoID = svr.IsTempoID()
72                 if svr.Connected && !svr.Disabled && svr.BaseURL == cfg.URL && tempoID {
73                         return svr, nil
74                 }
75
76                 // URL differ or not connected, so need to reconnect
77                 svr.BaseURL = cfg.URL
78
79         } else {
80
81                 // Create a new server object
82                 if cfg.APIBaseURL == "" {
83                         cfg.APIBaseURL = apiBaseUrl
84                 }
85                 if cfg.APIPartialURL == "" {
86                         cfg.APIPartialURL = "/server/" + strconv.Itoa(s.serverIndex)
87                         s.serverIndex = s.serverIndex + 1
88                 }
89
90                 // Create a new XDS Server
91                 svr = NewXdsServer(s.Context, cfg)
92
93                 svr.SetLoggerOutput(s.Config.LogVerboseOut)
94
95                 // Passthrough routes (handle by XDS Server)
96                 grp := s.apiRouter.Group(svr.PartialURL)
97                 svr.SetAPIRouterGroup(grp)
98
99                 // Declare passthrough routes
100                 s.sdksPassthroughInit(svr)
101         }
102
103         // Established connection
104         err := svr.Connect()
105
106         // Delete temporary ID with it has been replaced by right Server ID
107         if tempoID && !svr.IsTempoID() {
108                 delete(s.xdsServers, cfg.ID)
109         }
110
111         // Add to map
112         s.xdsServers[svr.ID] = svr
113
114         // Load projects
115         if err == nil && svr.Connected {
116                 err = s.projects.Init(svr)
117         }
118
119         return svr, err
120 }
121
122 // DelXdsServer Delete an XDS Server from the list of a server
123 func (s *APIService) DelXdsServer(id string) error {
124         if _, exist := s.xdsServers[id]; !exist {
125                 return fmt.Errorf("Unknown Server ID %s", id)
126         }
127         // Don't really delete, just disable it
128         s.xdsServers[id].Close()
129         return nil
130 }