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.
23 "github.com/gin-gonic/gin"
24 common "github.com/iotbzh/xds-common/golib"
25 "github.com/iotbzh/xds-server/lib/xsapiv1"
28 // getSdks returns all SDKs configuration
29 func (s *APIService) getSdks(c *gin.Context) {
30 c.JSON(http.StatusOK, s.sdks.GetAll())
33 // getSdk returns a specific Sdk configuration
34 func (s *APIService) getSdk(c *gin.Context) {
35 id, err := s.sdks.ResolveID(c.Param("id"))
37 common.APIError(c, err.Error())
41 if sdk.Profile == "" {
42 common.APIError(c, "Invalid id")
46 c.JSON(http.StatusOK, sdk)
49 // installSdk Install a new Sdk
50 func (s *APIService) installSdk(c *gin.Context) {
51 var args xsapiv1.SDKInstallArgs
53 if err := c.BindJSON(&args); err != nil {
54 common.APIError(c, "Invalid arguments")
57 id, err := s.sdks.ResolveID(args.ID)
59 common.APIError(c, err.Error())
63 // Support install from ID->URL or from local file
65 s.Log.Debugf("Installing SDK id %s (force %v)", id, args.Force)
66 } else if args.Filename != "" {
67 s.Log.Debugf("Installing SDK filename %s (force %v)", args.Filename, args.Force)
70 // Retrieve session info
71 sess := s.sessions.Get(c)
73 common.APIError(c, "Unknown sessions")
77 sdk, err := s.sdks.Install(id, args.Filename, args.Force, args.Timeout, sess)
79 common.APIError(c, err.Error())
83 c.JSON(http.StatusOK, sdk)
86 // abortInstallSdk Abort a SDK installation
87 func (s *APIService) abortInstallSdk(c *gin.Context) {
88 var args xsapiv1.SDKInstallArgs
90 if err := c.BindJSON(&args); err != nil {
91 common.APIError(c, "Invalid arguments")
94 id, err := s.sdks.ResolveID(args.ID)
96 common.APIError(c, err.Error())
100 sdk, err := s.sdks.AbortInstall(id, args.Timeout)
102 common.APIError(c, err.Error())
106 c.JSON(http.StatusOK, sdk)
109 // removeSdk Uninstall a Sdk
110 func (s *APIService) removeSdk(c *gin.Context) {
111 id, err := s.sdks.ResolveID(c.Param("id"))
113 common.APIError(c, err.Error())
117 // Retrieve session info
118 sess := s.sessions.Get(c)
120 common.APIError(c, "Unknown sessions")
124 s.Log.Debugln("Remove SDK id ", id)
126 delEntry, err := s.sdks.Remove(id, -1, sess)
128 common.APIError(c, err.Error())
131 c.JSON(http.StatusOK, delEntry)