Use go module as dependency tool instead of glide
[src/xds/xds-agent.git] / lib / agent / apiv1-config.go
1 /*
2  * Copyright (C) 2017-2018 "IoT.bzh"
3  * Author Sebastien Douheret <sebastien@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package agent
19
20 import (
21         "net/http"
22         "sync"
23
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"
28 )
29
30 var confMut sync.Mutex
31
32 // GetConfig returns the configuration
33 func (s *APIService) getConfig(c *gin.Context) {
34         confMut.Lock()
35         defer confMut.Unlock()
36
37         cfg := s._getConfig()
38
39         c.JSON(http.StatusOK, cfg)
40 }
41
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")
47                 return
48         }
49
50         confMut.Lock()
51         defer confMut.Unlock()
52
53         s.Log.Debugln("SET config: ", cfgArg)
54
55         // First delete/disable XDS Server that are no longer listed
56         for _, svr := range s.xdsServers {
57                 found := false
58                 for _, svrArg := range cfgArg.Servers {
59                         if svr.ID == svrArg.ID {
60                                 found = true
61                                 break
62                         }
63                 }
64                 if !found {
65                         s.DelXdsServer(svr.ID)
66                 }
67         }
68
69         // Add new / unconnected XDS Server
70         for _, svr := range cfgArg.Servers {
71                 if svr.Connected && svr.ID != "" {
72                         continue
73                 }
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         // 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)
90                 }
91         }
92
93         c.JSON(http.StatusOK, s._getConfig())
94 }
95
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{},
102         }
103
104         for _, svr := range s.xdsServers {
105                 cfg.Servers = append(cfg.Servers, xaapiv1.ServerCfg{
106                         ID:         svr.ID,
107                         URL:        svr.BaseURL,
108                         APIURL:     svr.APIURL,
109                         PartialURL: svr.PartialURL,
110                         ConnRetry:  svr.ConnRetry,
111                         Connected:  svr.Connected,
112                         Disabled:   svr.Disabled,
113                 })
114         }
115         return cfg
116 }