7 "github.com/gin-gonic/gin"
8 "github.com/iotbzh/xds-agent/lib/xdsconfig"
9 common "github.com/iotbzh/xds-common/golib"
12 var confMut sync.Mutex
14 // APIConfig parameters (json format) of /config command
15 type APIConfig struct {
16 Servers []ServerCfg `json:"servers"`
18 // Not exposed outside in JSON
19 Version string `json:"-"`
20 APIVersion string `json:"-"`
21 VersionGitTag string `json:"-"`
25 type ServerCfg struct {
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"`
35 // GetConfig returns the configuration
36 func (s *APIService) getConfig(c *gin.Context) {
38 defer confMut.Unlock()
42 c.JSON(http.StatusOK, cfg)
45 // SetConfig sets configuration
46 func (s *APIService) setConfig(c *gin.Context) {
48 if c.BindJSON(&cfgArg) != nil {
49 common.APIError(c, "Invalid arguments")
54 defer confMut.Unlock()
56 s.Log.Debugln("SET config: ", cfgArg)
58 // First delete/disable XDS Server that are no longer listed
59 for _, svr := range s.xdsServers {
61 for _, svrArg := range cfgArg.Servers {
62 if svr.ID == svrArg.ID {
68 s.DelXdsServer(svr.ID)
73 for _, svr := range cfgArg.Servers {
74 cfg := xdsconfig.XDSServerConf{
77 ConnRetry: svr.ConnRetry,
79 if _, err := s.AddXdsServer(cfg); err != nil {
80 common.APIError(c, err.Error())
85 c.JSON(http.StatusOK, s._getConfig())
88 func (s *APIService) _getConfig() APIConfig {
90 Version: s.Config.Version,
91 APIVersion: s.Config.APIVersion,
92 VersionGitTag: s.Config.VersionGitTag,
93 Servers: []ServerCfg{},
96 for _, svr := range s.xdsServers {
97 cfg.Servers = append(cfg.Servers, ServerCfg{
101 PartialURL: svr.PartialURL,
102 ConnRetry: svr.ConnRetry,
103 Connected: svr.Connected,
104 Disabled: svr.Disabled,