d4b5e74ae3d366d4c0c0752f7b566a6a896c2b94
[src/xds/xds-agent.git] / lib / agent / apiv1-projects.go
1 package agent
2
3 import (
4         "net/http"
5
6         "github.com/gin-gonic/gin"
7         common "github.com/iotbzh/xds-common/golib"
8 )
9
10 // getProjects returns all projects configuration
11 func (s *APIService) getProjects(c *gin.Context) {
12         c.JSON(http.StatusOK, s.projects.GetProjectArr())
13 }
14
15 // getProject returns a specific project configuration
16 func (s *APIService) getProject(c *gin.Context) {
17         prj := s.projects.Get(c.Param("id"))
18         if prj == nil {
19                 common.APIError(c, "Invalid id")
20                 return
21         }
22
23         c.JSON(http.StatusOK, (*prj).GetProject())
24 }
25
26 // addProject adds a new project to server config
27 func (s *APIService) addProject(c *gin.Context) {
28         var cfgArg ProjectConfig
29         if c.BindJSON(&cfgArg) != nil {
30                 common.APIError(c, "Invalid arguments")
31                 return
32         }
33
34         s.Log.Debugln("Add project config: ", cfgArg)
35
36         newFld, err := s.projects.Add(cfgArg)
37         if err != nil {
38                 common.APIError(c, err.Error())
39                 return
40         }
41
42         c.JSON(http.StatusOK, newFld)
43 }
44
45 // syncProject force synchronization of project files
46 func (s *APIService) syncProject(c *gin.Context) {
47         id := c.Param("id")
48
49         s.Log.Debugln("Sync project id: ", id)
50
51         err := s.projects.ForceSync(id)
52         if err != nil {
53                 common.APIError(c, err.Error())
54                 return
55         }
56
57         c.JSON(http.StatusOK, "")
58 }
59
60 // delProject deletes project from server config
61 func (s *APIService) delProject(c *gin.Context) {
62         id := c.Param("id")
63
64         s.Log.Debugln("Delete project id ", id)
65
66         delEntry, err := s.projects.Delete(id)
67         if err != nil {
68                 common.APIError(c, err.Error())
69                 return
70         }
71         c.JSON(http.StatusOK, delEntry)
72 }