Renamed apiv1 lib to xaapiv1.
[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/xaapiv1"
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         id, err := s.projects.ResolveID(c.Param("id"))
19         if err != nil {
20                 common.APIError(c, err.Error())
21                 return
22         }
23         prj := s.projects.Get(id)
24         if prj == nil {
25                 common.APIError(c, "Invalid id")
26                 return
27         }
28
29         c.JSON(http.StatusOK, (*prj).GetProject())
30 }
31
32 // addProject adds a new project to server config
33 func (s *APIService) addProject(c *gin.Context) {
34         var cfgArg xaapiv1.ProjectConfig
35         if c.BindJSON(&cfgArg) != nil {
36                 common.APIError(c, "Invalid arguments")
37                 return
38         }
39
40         s.Log.Debugln("Add project config: ", cfgArg)
41
42         newFld, err := s.projects.Add(cfgArg, s.sessions.GetID(c))
43         if err != nil {
44                 common.APIError(c, err.Error())
45                 return
46         }
47
48         c.JSON(http.StatusOK, newFld)
49 }
50
51 // syncProject force synchronization of project files
52 func (s *APIService) syncProject(c *gin.Context) {
53         id, err := s.projects.ResolveID(c.Param("id"))
54         if err != nil {
55                 common.APIError(c, err.Error())
56                 return
57         }
58
59         s.Log.Debugln("Sync project id: ", id)
60
61         err = s.projects.ForceSync(id)
62         if err != nil {
63                 common.APIError(c, err.Error())
64                 return
65         }
66
67         c.JSON(http.StatusOK, nil)
68 }
69
70 // delProject deletes project from server config
71 func (s *APIService) delProject(c *gin.Context) {
72         id, err := s.projects.ResolveID(c.Param("id"))
73         if err != nil {
74                 common.APIError(c, err.Error())
75                 return
76         }
77
78         s.Log.Debugln("Delete project id ", id)
79
80         delEntry, err := s.projects.Delete(id, s.sessions.GetID(c))
81         if err != nil {
82                 common.APIError(c, err.Error())
83                 return
84         }
85         c.JSON(http.StatusOK, delEntry)
86 }
87
88 // updateProject Update some field of a specific project
89 func (s *APIService) updateProject(c *gin.Context) {
90         id, err := s.projects.ResolveID(c.Param("id"))
91         if err != nil {
92                 common.APIError(c, err.Error())
93                 return
94         }
95
96         var cfgArg xaapiv1.ProjectConfig
97         if c.BindJSON(&cfgArg) != nil {
98                 common.APIError(c, "Invalid arguments")
99                 return
100         }
101
102         s.Log.Debugln("Update project id ", id)
103
104         upPrj, err := s.projects.Update(id, cfgArg, s.sessions.GetID(c))
105         if err != nil {
106                 common.APIError(c, err.Error())
107                 return
108         }
109         c.JSON(http.StatusOK, upPrj)
110 }