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