2 * Copyright (C) 2017-2018 "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 "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1"
24 common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
25 "github.com/gin-gonic/gin"
28 // getProjects returns all projects configuration
29 func (s *APIService) getProjects(c *gin.Context) {
30 c.JSON(http.StatusOK, s.projects.GetProjectArr())
33 // getProject returns a specific project configuration
34 func (s *APIService) getProject(c *gin.Context) {
35 id, err := s.projects.ResolveID(c.Param("id"))
37 common.APIError(c, err.Error())
40 prj := s.projects.Get(id)
42 common.APIError(c, "Invalid id")
46 c.JSON(http.StatusOK, (*prj).GetProject())
49 // addProject adds a new project to server config
50 func (s *APIService) addProject(c *gin.Context) {
51 var cfgArg xaapiv1.ProjectConfig
52 if c.BindJSON(&cfgArg) != nil {
53 common.APIError(c, "Invalid arguments")
57 s.Log.Debugln("Add project config: ", cfgArg)
59 newFld, err := s.projects.Add(cfgArg, s.sessions.GetID(c), c.Request.Host)
61 common.APIError(c, err.Error())
65 c.JSON(http.StatusOK, newFld)
68 // syncProject force synchronization of project files
69 func (s *APIService) syncProject(c *gin.Context) {
70 id, err := s.projects.ResolveID(c.Param("id"))
72 common.APIError(c, err.Error())
76 s.Log.Debugln("Sync project id: ", id)
78 err = s.projects.ForceSync(id)
80 common.APIError(c, err.Error())
84 c.JSON(http.StatusOK, nil)
87 // delProject deletes project from server config
88 func (s *APIService) delProject(c *gin.Context) {
89 id, err := s.projects.ResolveID(c.Param("id"))
91 common.APIError(c, err.Error())
95 s.Log.Debugln("Delete project id ", id)
97 delEntry, err := s.projects.Delete(id, s.sessions.GetID(c))
99 common.APIError(c, err.Error())
102 c.JSON(http.StatusOK, delEntry)
105 // updateProject Update some field of a specific project
106 func (s *APIService) updateProject(c *gin.Context) {
107 id, err := s.projects.ResolveID(c.Param("id"))
109 common.APIError(c, err.Error())
113 var cfgArg xaapiv1.ProjectConfig
114 if c.BindJSON(&cfgArg) != nil {
115 common.APIError(c, "Invalid arguments")
119 s.Log.Debugln("Update project id ", id)
121 upPrj, err := s.projects.Update(id, cfgArg, s.sessions.GetID(c))
123 common.APIError(c, err.Error())
126 c.JSON(http.StatusOK, upPrj)