aboutsummaryrefslogtreecommitdiffstats
path: root/lib/agent/apiv1.go
blob: d0e5a1cf84a78750cee4f41d72b60edcd42fd61b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Copyright (C) 2017 "IoT.bzh"
 * Author Sebastien Douheret <sebastien@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package agent

import (
	"fmt"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/iotbzh/xds-agent/lib/xdsconfig"
	"github.com/iotbzh/xds-server/lib/xsapiv1"
)

const apiBaseURL = "/api/v1"

// APIService .
type APIService struct {
	*Context
	apiRouter   *gin.RouterGroup
	serverIndex int
}

// NewAPIV1 creates a new instance of API service
func NewAPIV1(ctx *Context) *APIService {
	s := &APIService{
		Context:     ctx,
		apiRouter:   ctx.webServer.router.Group(apiBaseURL),
		serverIndex: 0,
	}

	s.apiRouter.GET("/version", s.getVersion)

	s.apiRouter.GET("/config", s.getConfig)
	s.apiRouter.POST("/config", s.setConfig)

	s.apiRouter.GET("/browse", s.browseFS)

	s.apiRouter.GET("/projects", s.getProjects)
	s.apiRouter.GET("/projects/:id", s.getProject)
	s.apiRouter.PUT("/projects/:id", s.updateProject)
	s.apiRouter.POST("/projects", s.addProject)
	s.apiRouter.POST("/projects/sync/:id", s.syncProject)
	s.apiRouter.DELETE("/projects/:id", s.delProject)

	s.apiRouter.POST("/exec", s.execCmd)
	s.apiRouter.POST("/exec/:id", s.execCmd)
	s.apiRouter.POST("/signal", s.execSignalCmd)

	s.apiRouter.GET("/events", s.eventsList)
	s.apiRouter.POST("/events/register", s.eventsRegister)
	s.apiRouter.POST("/events/unregister", s.eventsUnRegister)

	return s
}

// Stop Used to stop/close created services
func (s *APIService) Stop() {
	for _, svr := range s.xdsServers {
		svr.Close()
	}
}

// AddXdsServer Add a new XDS Server to the list of a server
func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, error) {
	var svr *XdsServer
	var exist, tempoID bool
	tempoID = false

	// First check if not already exist and update it
	if svr, exist = s.xdsServers[cfg.ID]; exist {

		// Update: Found, so just update some settings
		svr.ConnRetry = cfg.ConnRetry

		tempoID = svr.IsTempoID()
		if svr.Connected && !svr.Disabled && svr.BaseURL == cfg.URL && tempoID {
			return svr, nil
		}

		// URL differ or not connected, so need to reconnect
		svr.BaseURL = cfg.URL

	} else {

		// Create a new server object
		if cfg.APIBaseURL == "" {
			cfg.APIBaseURL = apiBaseURL
		}
		if cfg.APIPartialURL == "" {
			cfg.APIPartialURL = "/servers/" + strconv.Itoa(s.serverIndex)
			s.serverIndex = s.serverIndex + 1
		}

		// Create a new XDS Server
		svr = NewXdsServer(s.Context, cfg)

		svr.SetLoggerOutput(s.Config.LogVerboseOut)

		// Passthrough routes (handle by XDS Server)
		grp := s.apiRouter.Group(svr.PartialURL)
		svr.SetAPIRouterGroup(grp)

		// Declare passthrough routes
		s.sdksPassthroughInit(svr)
	}

	// Established connection
	err := svr.Connect()

	// Delete temporary ID with it has been replaced by right Server ID
	if tempoID && !svr.IsTempoID() {
		delete(s.xdsServers, cfg.ID)
	}

	// Add to map
	s.xdsServers[svr.ID] = svr

	// Register event forwarder
	s.sdksEventsForwardInit(svr)

	// Load projects
	if err == nil && svr.Connected {
		err = s.projects.Init(svr)
	}

	// Registered to all events
	if err == nil && svr.Connected {
		if err = svr.EventRegister(xsapiv1.EVTAll, ""); err != nil {
			s.Log.Errorf("XDS Server %v - register all events error: %v", svr.ID, err)
		}
	}

	return svr, err
}

// DelXdsServer Delete an XDS Server from the list of a server
func (s *APIService) DelXdsServer(id string) error {
	if _, exist := s.xdsServers[id]; !exist {
		return fmt.Errorf("Unknown Server ID %s", id)
	}
	// Don't really delete, just disable it
	s.xdsServers[id].Close()
	return nil
}