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