Added Copyright header.
[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_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
82                 }
83                 fd.WriteString("export XDS_SDK_ID=" + newFld.DefaultSdk + "\n")
84                 fd.Close()
85         }
86
87         c.JSON(http.StatusOK, newFld)
88 }
89
90 // syncFolder force synchronization of folder files
91 func (s *APIService) syncFolder(c *gin.Context) {
92         id, err := s.mfolders.ResolveID(c.Param("id"))
93         if err != nil {
94                 common.APIError(c, err.Error())
95                 return
96         }
97         s.Log.Debugln("Sync folder id: ", id)
98
99         err = s.mfolders.ForceSync(id)
100         if err != nil {
101                 common.APIError(c, err.Error())
102                 return
103         }
104
105         c.JSON(http.StatusOK, "")
106 }
107
108 // delFolder deletes folder from server config
109 func (s *APIService) delFolder(c *gin.Context) {
110         id, err := s.mfolders.ResolveID(c.Param("id"))
111         if err != nil {
112                 common.APIError(c, err.Error())
113                 return
114         }
115
116         s.Log.Debugln("Delete folder id ", id)
117
118         delEntry, err := s.mfolders.Delete(id)
119         if err != nil {
120                 common.APIError(c, err.Error())
121                 return
122         }
123         c.JSON(http.StatusOK, delEntry)
124 }
125
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"))
129         if err != nil {
130                 common.APIError(c, err.Error())
131                 return
132         }
133
134         s.Log.Debugln("Update folder id ", id)
135
136         var cfgArg xsapiv1.FolderConfig
137         if c.BindJSON(&cfgArg) != nil {
138                 common.APIError(c, "Invalid arguments")
139                 return
140         }
141
142         upFld, err := s.mfolders.Update(id, cfgArg)
143         if err != nil {
144                 common.APIError(c, err.Error())
145                 return
146         }
147         c.JSON(http.StatusOK, upFld)
148 }