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