d041300a884a9c36ad4263ffa1fa1ec1bc92f239
[policy/clamp.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * Copyright (C) 2021 Nordix Foundation. All rights reserved.
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  * ========================LICENSE_END===================================
17  */
18
19 package org.onap.policy.clamp.controlloop.participant.kubernetes.controller;
20
21 import io.swagger.annotations.Api;
22 import io.swagger.annotations.ApiOperation;
23 import io.swagger.annotations.ApiResponse;
24 import io.swagger.annotations.ApiResponses;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException;
28 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
29 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList;
30 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.HelmRepository;
31 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.InstallationInfo;
32 import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartService;
33 import org.onap.policy.common.utils.coder.CoderException;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.MediaType;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.web.bind.annotation.DeleteMapping;
40 import org.springframework.web.bind.annotation.GetMapping;
41 import org.springframework.web.bind.annotation.PathVariable;
42 import org.springframework.web.bind.annotation.PostMapping;
43 import org.springframework.web.bind.annotation.RequestBody;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RequestParam;
46 import org.springframework.web.bind.annotation.RequestPart;
47 import org.springframework.web.bind.annotation.RestController;
48 import org.springframework.web.multipart.MultipartFile;
49
50 @RestController("chartController")
51 @RequestMapping("helm")
52 @Api(tags = {"k8s-participant"})
53 public class ChartController {
54
55     @Autowired
56     private ChartService chartService;
57
58     private static final StandardCoder CODER = new StandardCoder();
59
60     /**
61      * REST endpoint to get all the charts.
62      *
63      * @return List of charts installed
64      */
65     @GetMapping(path = "/charts", produces = MediaType.APPLICATION_JSON_VALUE)
66     @ApiOperation(value = "Return all Charts")
67     @ApiResponses(value = {@ApiResponse(code = 200, message = "chart List")})
68     public ResponseEntity<ChartList> getAllCharts() {
69         return new ResponseEntity<>(ChartList.builder().charts(new ArrayList<>(chartService.getAllCharts())).build(),
70                 HttpStatus.OK);
71     }
72
73     /**
74      * REST endpoint to install a helm chart.
75      *
76      * @param info Info of the chart to be installed
77      * @return Status of the install operation
78      * @throws ServiceException in case of error
79      * @throws IOException in case of IO error
80      */
81     @PostMapping(path = "/install", consumes = MediaType.APPLICATION_JSON_VALUE,
82             produces = MediaType.APPLICATION_JSON_VALUE)
83     @ApiOperation(value = "Install the chart")
84     @ApiResponses(value = {@ApiResponse(code = 201, message = "chart Installed")})
85     public ResponseEntity<Object> installChart(@RequestBody InstallationInfo info)
86             throws ServiceException, IOException {
87         ChartInfo chart = chartService.getChart(info.getName(), info.getVersion());
88         if (chart == null) {
89             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
90         }
91
92         chartService.installChart(chart);
93         return new ResponseEntity<>(HttpStatus.CREATED);
94     }
95
96     /**
97      * REST endpoint to uninstall a specific chart.
98      *
99      * @param name name of the chart
100      * @param version version of the chart
101      * @return Status of operation
102      * @throws ServiceException in case of error.
103      */
104     @DeleteMapping(path = "/uninstall/{name}/{version}", produces = MediaType.APPLICATION_JSON_VALUE)
105     @ApiOperation(value = "Uninstall the Chart")
106     @ApiResponses(value = {@ApiResponse(code = 201, message = "chart Uninstalled")})
107     public ResponseEntity<Object> uninstallChart(@PathVariable("name") String name,
108             @PathVariable("version") String version) throws ServiceException {
109         ChartInfo chart = chartService.getChart(name, version);
110         if (chart == null) {
111             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
112         }
113
114         chartService.uninstallChart(chart);
115         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
116     }
117
118     /**
119      * REST endpoint to onboard a chart.
120      *
121      * @param chartFile Multipart file for the helm chart
122      * @param infoJson AppInfo of the chart
123      * @param overrideFile the file for overriding the chart
124      * @return Status of onboard operation
125      * @throws ServiceException in case of error
126      * @throws IOException in case of IO error
127      */
128     @PostMapping(path = "/onboard/chart", consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
129             produces = MediaType.APPLICATION_JSON_VALUE)
130     @ApiOperation(value = "Onboard the Chart")
131     @ApiResponses(value = {@ApiResponse(code = 201, message = "Chart Onboarded")})
132     public ResponseEntity<String> onboardChart(@RequestPart("chart") MultipartFile chartFile,
133             @RequestParam(name = "values", required = false) MultipartFile overrideFile,
134             @RequestParam("info") String infoJson) throws ServiceException, IOException {
135
136         ChartInfo info;
137         try {
138             info = CODER.decode(infoJson, ChartInfo.class);
139         } catch (CoderException e) {
140             throw new ServiceException("Error parsing the chart information", e);
141         }
142
143         chartService.saveChart(info, chartFile, overrideFile);
144         return new ResponseEntity<>(HttpStatus.OK);
145     }
146
147     /**
148      * REST endpoint to delete a specific helm chart.
149      *
150      * @param name name of the chart
151      * @param version version of the chart
152      * @return Status of operation
153      */
154     @DeleteMapping(path = "/chart/{name}/{version}")
155     @ApiOperation(value = "Delete the chart")
156     @ApiResponses(value = {@ApiResponse(code = 204, message = "Chart Deleted")})
157     public ResponseEntity<Object> deleteChart(@PathVariable("name") String name,
158             @PathVariable("version") String version) {
159
160         ChartInfo chart = chartService.getChart(name, version);
161         if (chart == null) {
162             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
163         }
164
165         chartService.deleteChart(chart);
166         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
167     }
168
169     /**
170      * REST endpoint to configure a helm Repository.
171      *
172      * @param repo Helm repository to be configured
173      * @return Status of the operation
174      * @throws ServiceException in case of error
175      * @throws IOException in case of IO error
176      */
177     @PostMapping(path = "/repo", consumes = MediaType.APPLICATION_JSON_VALUE,
178             produces = MediaType.APPLICATION_JSON_VALUE)
179     @ApiOperation(value = "Configure helm repository")
180     @ApiResponses(value = {@ApiResponse(code = 201, message = "Repository added")})
181     public ResponseEntity<Object> configureRepo(@RequestBody String repo)
182             throws ServiceException, IOException {
183         HelmRepository repository;
184         try {
185             repository = CODER.decode(repo, HelmRepository.class);
186         } catch (CoderException e) {
187             throw new ServiceException("Error parsing the repository information", e);
188         }
189         chartService.configureRepository(repository);
190
191         return new ResponseEntity<>(HttpStatus.CREATED);
192     }
193 }