Migration to AGL gerrit (update go import)
[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/golib"
24         "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/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         sdk, err := s.sdks.Install(id, args.Filename, args.Force, args.Timeout, args.InstallArgs, sess)
78         if err != nil {
79                 common.APIError(c, err.Error())
80                 return
81         }
82
83         c.JSON(http.StatusOK, sdk)
84 }
85
86 // abortInstallSdk Abort a SDK installation
87 func (s *APIService) abortInstallSdk(c *gin.Context) {
88         var args xsapiv1.SDKInstallArgs
89
90         if err := c.BindJSON(&args); err != nil {
91                 common.APIError(c, "Invalid arguments")
92                 return
93         }
94         id, err := s.sdks.ResolveID(args.ID)
95         if err != nil {
96                 common.APIError(c, err.Error())
97                 return
98         }
99
100         sdk, err := s.sdks.AbortInstall(id, args.Timeout)
101         if err != nil {
102                 common.APIError(c, err.Error())
103                 return
104         }
105
106         c.JSON(http.StatusOK, sdk)
107 }
108
109 // removeSdk Uninstall a Sdk
110 func (s *APIService) removeSdk(c *gin.Context) {
111         id, err := s.sdks.ResolveID(c.Param("id"))
112         if err != nil {
113                 common.APIError(c, err.Error())
114                 return
115         }
116
117         // Retrieve session info
118         sess := s.sessions.Get(c)
119         if sess == nil {
120                 common.APIError(c, "Unknown sessions")
121                 return
122         }
123
124         s.Log.Debugln("Remove SDK id ", id)
125
126         delEntry, err := s.sdks.Remove(id, -1, sess)
127         if err != nil {
128                 common.APIError(c, err.Error())
129                 return
130         }
131         c.JSON(http.StatusOK, delEntry)
132 }