Use go module as dependency tool instead of glide
[src/xds/xds-server.git] / lib / xdsserver / apiv1-sdks.go
1 /*
2  * Copyright (C) 2017-2018 "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
23         common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
24         "gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1"
25         "github.com/gin-gonic/gin"
26 )
27
28 // getSdks returns all SDKs configuration
29 func (s *APIService) getSdks(c *gin.Context) {
30         c.JSON(http.StatusOK, s.sdks.GetAll())
31 }
32
33 // getSdk returns a specific Sdk configuration
34 func (s *APIService) getSdk(c *gin.Context) {
35         id, err := s.sdks.ResolveID(c.Param("id"))
36         if err != nil {
37                 common.APIError(c, err.Error())
38                 return
39         }
40         sdk := s.sdks.Get(id)
41         if sdk.Profile == "" {
42                 common.APIError(c, "Invalid id")
43                 return
44         }
45
46         c.JSON(http.StatusOK, sdk)
47 }
48
49 // installSdk Install a new Sdk
50 func (s *APIService) installSdk(c *gin.Context) {
51         var args xsapiv1.SDKInstallArgs
52
53         if err := c.BindJSON(&args); err != nil {
54                 common.APIError(c, "Invalid arguments")
55                 return
56         }
57         id, err := s.sdks.ResolveID(args.ID)
58         if err != nil {
59                 common.APIError(c, err.Error())
60                 return
61         }
62
63         // Support install from ID->URL or from local file
64         if id != "" {
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)
68         }
69
70         // Retrieve session info
71         sess := s.sessions.Get(c)
72         if sess == nil {
73                 common.APIError(c, "Unknown sessions")
74                 return
75         }
76
77         //increment lock xds counter: it will be decremented,
78         //when SDK is installed in file sdk.go in function ExitCB
79         //at line 308 or when install is aborted in following function
80         LockXdsUpdateCounter(s.Context, true)
81         sdk, err := s.sdks.Install(id, args.Filename, args.Force, args.Timeout, args.InstallArgs, sess)
82         if err != nil {
83                 LockXdsUpdateCounter(s.Context, false)
84                 common.APIError(c, err.Error())
85                 return
86         }
87
88         c.JSON(http.StatusOK, sdk)
89 }
90
91 // abortInstallSdk Abort a SDK installation
92 func (s *APIService) abortInstallSdk(c *gin.Context) {
93         var args xsapiv1.SDKInstallArgs
94
95         if err := c.BindJSON(&args); err != nil {
96                 common.APIError(c, "Invalid arguments")
97                 return
98         }
99         id, err := s.sdks.ResolveID(args.ID)
100         if err != nil {
101                 common.APIError(c, err.Error())
102                 return
103         }
104
105         sdk, err := s.sdks.AbortInstall(id, args.Timeout)
106         if err != nil {
107                 common.APIError(c, err.Error())
108                 return
109         }
110
111         LockXdsUpdateCounter(s.Context, false)
112         c.JSON(http.StatusOK, sdk)
113 }
114
115 // removeSdk Uninstall a Sdk
116 func (s *APIService) removeSdk(c *gin.Context) {
117         id, err := s.sdks.ResolveID(c.Param("id"))
118         if err != nil {
119                 common.APIError(c, err.Error())
120                 return
121         }
122
123         // Retrieve session info
124         sess := s.sessions.Get(c)
125         if sess == nil {
126                 common.APIError(c, "Unknown sessions")
127                 return
128         }
129
130         s.Log.Debugln("Remove SDK id ", id)
131
132         LockXdsUpdateCounter(s.Context, true)
133         delEntry, err := s.sdks.Remove(id, -1, sess)
134         if err != nil {
135                 common.APIError(c, err.Error())
136                 return
137         }
138         LockXdsUpdateCounter(s.Context, false)
139         c.JSON(http.StatusOK, delEntry)
140 }