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()
81 newFld.DefaultSdk = sdks[0].ID
83 fd.WriteString("export XDS_SDK_ID=" + newFld.DefaultSdk + "\n")
87 c.JSON(http.StatusOK, newFld)
90 // syncFolder force synchronization of folder files
91 func (s *APIService) syncFolder(c *gin.Context) {
92 id, err := s.mfolders.ResolveID(c.Param("id"))
94 common.APIError(c, err.Error())
97 s.Log.Debugln("Sync folder id: ", id)
99 err = s.mfolders.ForceSync(id)
101 common.APIError(c, err.Error())
105 c.JSON(http.StatusOK, "")
108 // delFolder deletes folder from server config
109 func (s *APIService) delFolder(c *gin.Context) {
110 id, err := s.mfolders.ResolveID(c.Param("id"))
112 common.APIError(c, err.Error())
116 s.Log.Debugln("Delete folder id ", id)
118 delEntry, err := s.mfolders.Delete(id)
120 common.APIError(c, err.Error())
123 c.JSON(http.StatusOK, delEntry)
126 // updateFolder update some field of a folder
127 func (s *APIService) updateFolder(c *gin.Context) {
128 id, err := s.mfolders.ResolveID(c.Param("id"))
130 common.APIError(c, err.Error())
134 s.Log.Debugln("Update folder id ", id)
136 var cfgArg xsapiv1.FolderConfig
137 if c.BindJSON(&cfgArg) != nil {
138 common.APIError(c, "Invalid arguments")
142 upFld, err := s.mfolders.Update(id, cfgArg)
144 common.APIError(c, err.Error())
147 c.JSON(http.StatusOK, upFld)