Merge branch 'master' into wip
[src/xds/xds-agent.git] / lib / agent / apiv1-config.go
1 package agent
2
3 import (
4         "net/http"
5         "sync"
6
7         "github.com/gin-gonic/gin"
8         "github.com/iotbzh/xds-agent/lib/xdsconfig"
9         common "github.com/iotbzh/xds-common/golib"
10 )
11
12 var confMut sync.Mutex
13
14 // APIConfig parameters (json format) of /config command
15 type APIConfig struct {
16         Servers []ServerCfg `json:"servers"`
17
18         // Not exposed outside in JSON
19         Version       string `json:"-"`
20         APIVersion    string `json:"-"`
21         VersionGitTag string `json:"-"`
22 }
23
24 // ServerCfg .
25 type ServerCfg struct {
26         ID         string `json:"id"`
27         URL        string `json:"url"`
28         APIURL     string `json:"apiUrl"`
29         PartialURL string `json:"partialUrl"`
30         ConnRetry  int    `json:"connRetry"`
31         Connected  bool   `json:"connected"`
32         Disabled   bool   `json:"disabled"`
33 }
34
35 // GetConfig returns the configuration
36 func (s *APIService) getConfig(c *gin.Context) {
37         confMut.Lock()
38         defer confMut.Unlock()
39
40         cfg := s._getConfig()
41
42         c.JSON(http.StatusOK, cfg)
43 }
44
45 // SetConfig sets configuration
46 func (s *APIService) setConfig(c *gin.Context) {
47         var cfgArg APIConfig
48         if c.BindJSON(&cfgArg) != nil {
49                 common.APIError(c, "Invalid arguments")
50                 return
51         }
52
53         confMut.Lock()
54         defer confMut.Unlock()
55
56         s.Log.Debugln("SET config: ", cfgArg)
57
58         // First delete/disable XDS Server that are no longer listed
59         for _, svr := range s.xdsServers {
60                 found := false
61                 for _, svrArg := range cfgArg.Servers {
62                         if svr.ID == svrArg.ID {
63                                 found = true
64                                 break
65                         }
66                 }
67                 if !found {
68                         s.DelXdsServer(svr.ID)
69                 }
70         }
71
72         // Add new XDS Server
73         for _, svr := range cfgArg.Servers {
74                 cfg := xdsconfig.XDSServerConf{
75                         ID:        svr.ID,
76                         URL:       svr.URL,
77                         ConnRetry: svr.ConnRetry,
78                 }
79                 if _, err := s.AddXdsServer(cfg); err != nil {
80                         common.APIError(c, err.Error())
81                         return
82                 }
83         }
84
85         c.JSON(http.StatusOK, s._getConfig())
86 }
87
88 func (s *APIService) _getConfig() APIConfig {
89         cfg := APIConfig{
90                 Version:       s.Config.Version,
91                 APIVersion:    s.Config.APIVersion,
92                 VersionGitTag: s.Config.VersionGitTag,
93                 Servers:       []ServerCfg{},
94         }
95
96         for _, svr := range s.xdsServers {
97                 cfg.Servers = append(cfg.Servers, ServerCfg{
98                         ID:         svr.ID,
99                         URL:        svr.BaseURL,
100                         APIURL:     svr.APIURL,
101                         PartialURL: svr.PartialURL,
102                         ConnRetry:  svr.ConnRetry,
103                         Connected:  svr.Connected,
104                         Disabled:   svr.Disabled,
105                 })
106         }
107         return cfg
108 }