8aad18a6049f0dc048b87acf4a5b69fe91fb23e5
[src/xds/xds-agent.git] / lib / agent / apiv1-events.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 // EventRegisterArgs is the parameters (json format) of /events/register command
11 type EventRegisterArgs struct {
12         Name      string `json:"name"`
13         ProjectID string `json:"filterProjectID"`
14 }
15
16 // EventUnRegisterArgs is the parameters (json format) of /events/unregister command
17 type EventUnRegisterArgs struct {
18         Name string `json:"name"`
19         ID   int    `json:"id"`
20 }
21
22 // eventsList Registering for events that will be send over a WS
23 func (s *APIService) eventsList(c *gin.Context) {
24         c.JSON(http.StatusOK, s.events.GetList())
25 }
26
27 // eventsRegister Registering for events that will be send over a WS
28 func (s *APIService) eventsRegister(c *gin.Context) {
29         var args EventRegisterArgs
30
31         if c.BindJSON(&args) != nil || args.Name == "" {
32                 common.APIError(c, "Invalid arguments")
33                 return
34         }
35
36         sess := s.webServer.sessions.Get(c)
37         if sess == nil {
38                 common.APIError(c, "Unknown sessions")
39                 return
40         }
41
42         // Register to all or to a specific events
43         if err := s.events.Register(args.Name, sess.ID); err != nil {
44                 common.APIError(c, err.Error())
45                 return
46         }
47
48         c.JSON(http.StatusOK, gin.H{"status": "OK"})
49 }
50
51 // eventsRegister Registering for events that will be send over a WS
52 func (s *APIService) eventsUnRegister(c *gin.Context) {
53         var args EventUnRegisterArgs
54
55         if c.BindJSON(&args) != nil || args.Name == "" {
56                 common.APIError(c, "Invalid arguments")
57                 return
58         }
59
60         sess := s.webServer.sessions.Get(c)
61         if sess == nil {
62                 common.APIError(c, "Unknown sessions")
63                 return
64         }
65
66         // Register to all or to a specific events
67         if err := s.events.UnRegister(args.Name, sess.ID); err != nil {
68                 common.APIError(c, err.Error())
69                 return
70         }
71
72         c.JSON(http.StatusOK, gin.H{"status": "OK"})
73 }