Use go module as dependency tool instead of glide
[src/xds/xds-server.git] / lib / xdsserver / apiv1-folders.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 xdsserver
19
20 import (
21         "net/http"
22
23         common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
24         "gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1"
25         "github.com/gin-gonic/gin"
26 )
27
28 // getFolders returns all folders configuration
29 func (s *APIService) getFolders(c *gin.Context) {
30         c.JSON(http.StatusOK, s.mfolders.GetConfigArr())
31 }
32
33 // getFolder returns a specific folder configuration
34 func (s *APIService) getFolder(c *gin.Context) {
35         id, err := s.mfolders.ResolveID(c.Param("id"))
36         if err != nil {
37                 common.APIError(c, err.Error())
38                 return
39         }
40         f := s.mfolders.Get(id)
41         if f == nil {
42                 common.APIError(c, "Invalid id")
43                 return
44         }
45
46         c.JSON(http.StatusOK, (*f).GetConfig())
47 }
48
49 // addFolder adds a new folder to server config
50 func (s *APIService) addFolder(c *gin.Context) {
51         var cfgArg xsapiv1.FolderConfig
52         if c.BindJSON(&cfgArg) != nil {
53                 common.APIError(c, "Invalid arguments")
54                 return
55         }
56
57         s.Log.Debugln("Add folder config: ", cfgArg)
58
59         newFld, err := s.mfolders.Add(cfgArg)
60         if err != nil {
61                 common.APIError(c, err.Error())
62                 return
63         }
64
65         c.JSON(http.StatusOK, newFld)
66 }
67
68 // syncFolder force synchronization of folder files
69 func (s *APIService) syncFolder(c *gin.Context) {
70         id, err := s.mfolders.ResolveID(c.Param("id"))
71         if err != nil {
72                 common.APIError(c, err.Error())
73                 return
74         }
75         s.Log.Debugln("Sync folder id: ", id)
76
77         err = s.mfolders.ForceSync(id)
78         if err != nil {
79                 common.APIError(c, err.Error())
80                 return
81         }
82
83         c.JSON(http.StatusOK, "")
84 }
85
86 // delFolder deletes folder from server config
87 func (s *APIService) delFolder(c *gin.Context) {
88         id, err := s.mfolders.ResolveID(c.Param("id"))
89         if err != nil {
90                 common.APIError(c, err.Error())
91                 return
92         }
93
94         s.Log.Debugln("Delete folder id ", id)
95
96         delEntry, err := s.mfolders.Delete(id)
97         if err != nil {
98                 common.APIError(c, err.Error())
99                 return
100         }
101         c.JSON(http.StatusOK, delEntry)
102 }
103
104 // updateFolder update some field of a folder
105 func (s *APIService) updateFolder(c *gin.Context) {
106         id, err := s.mfolders.ResolveID(c.Param("id"))
107         if err != nil {
108                 common.APIError(c, err.Error())
109                 return
110         }
111
112         s.Log.Debugln("Update folder id ", id)
113
114         var cfgArg xsapiv1.FolderConfig
115         if c.BindJSON(&cfgArg) != nil {
116                 common.APIError(c, "Invalid arguments")
117                 return
118         }
119
120         upFld, err := s.mfolders.Update(id, cfgArg)
121         if err != nil {
122                 common.APIError(c, err.Error())
123                 return
124         }
125         c.JSON(http.StatusOK, upFld)
126 }