2 * Copyright (C) 2017 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 "github.com/gin-gonic/gin"
25 common "github.com/iotbzh/xds-common/golib"
26 "github.com/iotbzh/xds-server/lib/xsapiv1"
29 // getFolders returns all folders configuration
30 func (s *APIService) getFolders(c *gin.Context) {
31 c.JSON(http.StatusOK, s.mfolders.GetConfigArr())
34 // getFolder returns a specific folder configuration
35 func (s *APIService) getFolder(c *gin.Context) {
36 id, err := s.mfolders.ResolveID(c.Param("id"))
38 common.APIError(c, err.Error())
41 f := s.mfolders.Get(id)
43 common.APIError(c, "Invalid id")
47 c.JSON(http.StatusOK, (*f).GetConfig())
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")
58 s.Log.Debugln("Add folder config: ", cfgArg)
60 newFld, err := s.mfolders.Add(cfgArg)
62 common.APIError(c, err.Error())
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)
73 common.APIError(c, err.Error())
76 fd.WriteString("# XDS project settings\n")
77 fd.WriteString("export XDS_SERVER_URL=" + c.Request.Host + "\n")
78 fd.WriteString("export XDS_PROJECT_ID=" + newFld.ID + "\n")
79 if newFld.DefaultSdk == "" {
80 sdks := s.sdks.GetAll()
82 newFld.DefaultSdk = sdks[0].ID
85 fd.WriteString("export XDS_SDK_ID=" + newFld.DefaultSdk + "\n")
89 c.JSON(http.StatusOK, newFld)
92 // syncFolder force synchronization of folder files
93 func (s *APIService) syncFolder(c *gin.Context) {
94 id, err := s.mfolders.ResolveID(c.Param("id"))
96 common.APIError(c, err.Error())
99 s.Log.Debugln("Sync folder id: ", id)
101 err = s.mfolders.ForceSync(id)
103 common.APIError(c, err.Error())
107 c.JSON(http.StatusOK, "")
110 // delFolder deletes folder from server config
111 func (s *APIService) delFolder(c *gin.Context) {
112 id, err := s.mfolders.ResolveID(c.Param("id"))
114 common.APIError(c, err.Error())
118 s.Log.Debugln("Delete folder id ", id)
120 delEntry, err := s.mfolders.Delete(id)
122 common.APIError(c, err.Error())
125 c.JSON(http.StatusOK, delEntry)
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"))
132 common.APIError(c, err.Error())
136 s.Log.Debugln("Update folder id ", id)
138 var cfgArg xsapiv1.FolderConfig
139 if c.BindJSON(&cfgArg) != nil {
140 common.APIError(c, "Invalid arguments")
144 upFld, err := s.mfolders.Update(id, cfgArg)
146 common.APIError(c, err.Error())
149 c.JSON(http.StatusOK, upFld)