2 * Copyright (C) 2017 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 "github.com/gin-gonic/gin"
25 "github.com/iotbzh/xds-agent/lib/xaapiv1"
26 "github.com/iotbzh/xds-agent/lib/xdsconfig"
27 common "github.com/iotbzh/xds-common/golib"
30 var confMut sync.Mutex
32 // GetConfig returns the configuration
33 func (s *APIService) getConfig(c *gin.Context) {
35 defer confMut.Unlock()
39 c.JSON(http.StatusOK, cfg)
42 // SetConfig sets configuration
43 func (s *APIService) setConfig(c *gin.Context) {
44 var cfgArg xaapiv1.APIConfig
45 if c.BindJSON(&cfgArg) != nil {
46 common.APIError(c, "Invalid arguments")
51 defer confMut.Unlock()
53 s.Log.Debugln("SET config: ", cfgArg)
55 // First delete/disable XDS Server that are no longer listed
56 for _, svr := range s.xdsServers {
58 for _, svrArg := range cfgArg.Servers {
59 if svr.ID == svrArg.ID {
65 s.DelXdsServer(svr.ID)
70 for _, svr := range cfgArg.Servers {
71 if svr.Connected && svr.ID != "" {
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() xaapiv1.APIConfig {
89 cfg := xaapiv1.APIConfig{
90 Version: s.Config.Version,
91 APIVersion: s.Config.APIVersion,
92 VersionGitTag: s.Config.VersionGitTag,
93 Servers: []xaapiv1.ServerCfg{},
96 for _, svr := range s.xdsServers {
97 cfg.Servers = append(cfg.Servers, xaapiv1.ServerCfg{
101 PartialURL: svr.PartialURL,
102 ConnRetry: svr.ConnRetry,
103 Connected: svr.Connected,
104 Disabled: svr.Disabled,