Renamed apiv1 lib to xaapiv1.
[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/xaapiv1"
9         "github.com/iotbzh/xds-agent/lib/xdsconfig"
10         common "github.com/iotbzh/xds-common/golib"
11 )
12
13 var confMut sync.Mutex
14
15 // GetConfig returns the configuration
16 func (s *APIService) getConfig(c *gin.Context) {
17         confMut.Lock()
18         defer confMut.Unlock()
19
20         cfg := s._getConfig()
21
22         c.JSON(http.StatusOK, cfg)
23 }
24
25 // SetConfig sets configuration
26 func (s *APIService) setConfig(c *gin.Context) {
27         var cfgArg xaapiv1.APIConfig
28         if c.BindJSON(&cfgArg) != nil {
29                 common.APIError(c, "Invalid arguments")
30                 return
31         }
32
33         confMut.Lock()
34         defer confMut.Unlock()
35
36         s.Log.Debugln("SET config: ", cfgArg)
37
38         // First delete/disable XDS Server that are no longer listed
39         for _, svr := range s.xdsServers {
40                 found := false
41                 for _, svrArg := range cfgArg.Servers {
42                         if svr.ID == svrArg.ID {
43                                 found = true
44                                 break
45                         }
46                 }
47                 if !found {
48                         s.DelXdsServer(svr.ID)
49                 }
50         }
51
52         // Add new XDS Server
53         for _, svr := range cfgArg.Servers {
54                 cfg := xdsconfig.XDSServerConf{
55                         ID:        svr.ID,
56                         URL:       svr.URL,
57                         ConnRetry: svr.ConnRetry,
58                 }
59                 if _, err := s.AddXdsServer(cfg); err != nil {
60                         common.APIError(c, err.Error())
61                         return
62                 }
63         }
64
65         c.JSON(http.StatusOK, s._getConfig())
66 }
67
68 func (s *APIService) _getConfig() xaapiv1.APIConfig {
69         cfg := xaapiv1.APIConfig{
70                 Version:       s.Config.Version,
71                 APIVersion:    s.Config.APIVersion,
72                 VersionGitTag: s.Config.VersionGitTag,
73                 Servers:       []xaapiv1.ServerCfg{},
74         }
75
76         for _, svr := range s.xdsServers {
77                 cfg.Servers = append(cfg.Servers, xaapiv1.ServerCfg{
78                         ID:         svr.ID,
79                         URL:        svr.BaseURL,
80                         APIURL:     svr.APIURL,
81                         PartialURL: svr.PartialURL,
82                         ConnRetry:  svr.ConnRetry,
83                         Connected:  svr.Connected,
84                         Disabled:   svr.Disabled,
85                 })
86         }
87         return cfg
88 }