Use go module as dependency tool instead of glide
[src/xds/xds-agent.git] / lib / agent / apiv1-servers.go
1 /*
2  * Copyright (C) 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         "time"
23
24         "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1"
25         common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
26         "github.com/gin-gonic/gin"
27 )
28
29 // getServersList Return the list of XDS Servers
30 func (s *APIService) getServersList(c *gin.Context) {
31         confMut.Lock()
32         defer confMut.Unlock()
33
34         svrList := []xaapiv1.ServerCfg{}
35         for _, svr := range s.xdsServers {
36                 svrList = append(svrList, svr.GetConfig())
37         }
38
39         c.JSON(http.StatusOK, svrList)
40 }
41
42 // getServer Return info of a XDS Servers
43 func (s *APIService) getServer(c *gin.Context) {
44
45         id := s.ParamGetIndex(c)
46         if id == "" {
47                 common.APIError(c, "invalid parameter, id must be set")
48                 return
49         }
50
51         confMut.Lock()
52         defer confMut.Unlock()
53
54         svr := s.GetXdsServerFromURLIndex(id)
55         if svr == nil {
56                 common.APIError(c, "unknown id")
57                 return
58         }
59
60         c.JSON(http.StatusOK, svr.GetConfig())
61 }
62
63 // reconnectServer Force reconnection of a XDS Server
64 func (s *APIService) reconnectServer(c *gin.Context) {
65
66         id := s.ParamGetIndex(c)
67         if id == "" {
68                 common.APIError(c, "invalid parameter, id must be set")
69                 return
70         }
71
72         svr := s.GetXdsServerFromURLIndex(id)
73         if svr == nil {
74                 common.APIError(c, "unknown id")
75                 return
76         }
77
78         s.Log.Debugf("Reconnect XDS Server id %v", svr.ID)
79
80         if err := svr.Close(); err != nil {
81                 common.APIError(c, err.Error())
82                 return
83         }
84
85         time.Sleep(time.Millisecond * 100)
86
87         if err := svr.Connect(); err != nil {
88                 common.APIError(c, err.Error())
89                 return
90         }
91
92         c.JSON(http.StatusOK, svr.GetConfig())
93 }