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