009d2ce62c737df7e974239b0a21bece3490ba46
[src/xds/xds-server.git] / lib / xdsserver / apiv1-folders.go
1 /*
2  * Copyright (C) 2017 "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 xdsserver
19
20 import (
21         "net/http"
22         "os"
23
24         "github.com/gin-gonic/gin"
25         common "github.com/iotbzh/xds-common/golib"
26         "github.com/iotbzh/xds-server/lib/xsapiv1"
27 )
28
29 // getFolders returns all folders configuration
30 func (s *APIService) getFolders(c *gin.Context) {
31         c.JSON(http.StatusOK, s.mfolders.GetConfigArr())
32 }
33
34 // getFolder returns a specific folder configuration
35 func (s *APIService) getFolder(c *gin.Context) {
36         id, err := s.mfolders.ResolveID(c.Param("id"))
37         if err != nil {
38                 common.APIError(c, err.Error())
39                 return
40         }
41         f := s.mfolders.Get(id)
42         if f == nil {
43                 common.APIError(c, "Invalid id")
44                 return
45         }
46
47         c.JSON(http.StatusOK, (*f).GetConfig())
48 }
49
50 // addFolder adds a new folder to server config
51 func (s *APIService) addFolder(c *gin.Context) {
52         var cfgArg xsapiv1.FolderConfig
53         if c.BindJSON(&cfgArg) != nil {
54                 common.APIError(c, "Invalid arguments")
55                 return
56         }
57
58         s.Log.Debugln("Add folder config: ", cfgArg)
59
60         newFld, err := s.mfolders.Add(cfgArg)
61         if err != nil {
62                 common.APIError(c, err.Error())
63                 return
64         }
65
66         // Create xds-project.conf file
67         // FIXME: move to folders.createUpdate func (but gin context needed)
68         fld := s.mfolders.Get(newFld.ID)
69         prjConfFile := (*fld).GetFullPath("xds-project.conf")
70         if !common.Exists(prjConfFile) {
71                 fd, err := os.OpenFile(prjConfFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
72                 if err != nil {
73                         common.APIError(c, err.Error())
74                         return
75                 }
76                 fd.WriteString("# XDS project settings\n")
77                 fd.WriteString("export XDS_AGENT_URL=" + c.Request.Host + "\n")
78                 fd.WriteString("export XDS_PROJECT_ID=" + newFld.ID + "\n")
79                 if newFld.DefaultSdk == "" {
80                         sdks := s.sdks.GetAll()
81                         if len(sdks) > 0 {
82                                 newFld.DefaultSdk = sdks[0].ID
83                         }
84                 }
85                 fd.WriteString("export XDS_SDK_ID=" + newFld.DefaultSdk + "\n")
86                 fd.Close()
87         }
88
89         c.JSON(http.StatusOK, newFld)
90 }
91
92 // syncFolder force synchronization of folder files
93 func (s *APIService) syncFolder(c *gin.Context) {
94         id, err := s.mfolders.ResolveID(c.Param("id"))
95         if err != nil {
96                 common.APIError(c, err.Error())
97                 return
98         }
99         s.Log.Debugln("Sync folder id: ", id)
100
101         err = s.mfolders.ForceSync(id)
102         if err != nil {
103                 common.APIError(c, err.Error())
104                 return
105         }
106
107         c.JSON(http.StatusOK, "")
108 }
109
110 // delFolder deletes folder from server config
111 func (s *APIService) delFolder(c *gin.Context) {
112         id, err := s.mfolders.ResolveID(c.Param("id"))
113         if err != nil {
114                 common.APIError(c, err.Error())
115                 return
116         }
117
118         s.Log.Debugln("Delete folder id ", id)
119
120         delEntry, err := s.mfolders.Delete(id)
121         if err != nil {
122                 common.APIError(c, err.Error())
123                 return
124         }
125         c.JSON(http.StatusOK, delEntry)
126 }
127
128 // updateFolder update some field of a folder
129 func (s *APIService) updateFolder(c *gin.Context) {
130         id, err := s.mfolders.ResolveID(c.Param("id"))
131         if err != nil {
132                 common.APIError(c, err.Error())
133                 return
134         }
135
136         s.Log.Debugln("Update folder id ", id)
137
138         var cfgArg xsapiv1.FolderConfig
139         if c.BindJSON(&cfgArg) != nil {
140                 common.APIError(c, "Invalid arguments")
141                 return
142         }
143
144         upFld, err := s.mfolders.Update(id, cfgArg)
145         if err != nil {
146                 common.APIError(c, err.Error())
147                 return
148         }
149         c.JSON(http.StatusOK, upFld)
150 }