2 * Copyright (C) 2017-2018 "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 "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1"
25 "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xdsconfig"
26 common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
27 "github.com/gin-gonic/gin"
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)
69 // Add new / unconnected XDS Server
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 // Update XdsServer config
86 for _, svrCfg := range cfgArg.Servers {
87 if err := s.UpdateXdsServer(svrCfg); err != nil {
88 // willingly ignore error
89 // s.Log.Debugf("Error while updating XDS Server config: %v", err)
93 c.JSON(http.StatusOK, s._getConfig())
96 func (s *APIService) _getConfig() xaapiv1.APIConfig {
97 cfg := xaapiv1.APIConfig{
98 Version: s.Config.Version,
99 APIVersion: s.Config.APIVersion,
100 VersionGitTag: s.Config.VersionGitTag,
101 Servers: []xaapiv1.ServerCfg{},
104 for _, svr := range s.xdsServers {
105 cfg.Servers = append(cfg.Servers, xaapiv1.ServerCfg{
109 PartialURL: svr.PartialURL,
110 ConnRetry: svr.ConnRetry,
111 Connected: svr.Connected,
112 Disabled: svr.Disabled,