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
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.
16 * ========================LICENSE_END===================================
19 package org.onap.policy.clamp.controlloop.participant.kubernetes.controller;
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.InstallationInfo;
31 import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartService;
32 import org.onap.policy.common.utils.coder.CoderException;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.MediaType;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.web.bind.annotation.DeleteMapping;
39 import org.springframework.web.bind.annotation.GetMapping;
40 import org.springframework.web.bind.annotation.PathVariable;
41 import org.springframework.web.bind.annotation.PostMapping;
42 import org.springframework.web.bind.annotation.RequestBody;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import org.springframework.web.bind.annotation.RequestParam;
45 import org.springframework.web.bind.annotation.RequestPart;
46 import org.springframework.web.bind.annotation.RestController;
47 import org.springframework.web.multipart.MultipartFile;
49 @RestController("chartController")
50 @RequestMapping("helm")
51 @Api(tags = {"chart"})
52 public class ChartController {
55 private ChartService chartService;
57 private static final StandardCoder CODER = new StandardCoder();
60 * REST endpoint to get all the charts.
62 * @return List of charts installed
64 @GetMapping(path = "/charts", produces = MediaType.APPLICATION_JSON_VALUE)
65 @ApiOperation(value = "Return all Charts")
66 @ApiResponses(value = {@ApiResponse(code = 200, message = "chart List")})
67 public ResponseEntity<ChartList> getAllCharts() {
68 return new ResponseEntity<>(ChartList.builder().charts(new ArrayList<>(chartService.getAllCharts())).build(),
73 * REST endpoint to install a helm chart.
75 * @param info Info of the chart to be installed
76 * @return Status of the install operation
77 * @throws ServiceException in case of error
78 * @throws IOException in case of IO error
80 @PostMapping(path = "/install", consumes = MediaType.APPLICATION_JSON_VALUE,
81 produces = MediaType.APPLICATION_JSON_VALUE)
82 @ApiOperation(value = "Install the chart")
83 @ApiResponses(value = {@ApiResponse(code = 201, message = "chart Installed")})
84 public ResponseEntity<Object> installChart(@RequestBody InstallationInfo info)
85 throws ServiceException, IOException {
86 ChartInfo chart = chartService.getChart(info.getName(), info.getVersion());
88 return new ResponseEntity<>(HttpStatus.NOT_FOUND);
91 chartService.installChart(chart);
92 return new ResponseEntity<>(HttpStatus.CREATED);
96 * REST endpoint to uninstall a specific chart.
98 * @param name name of the chart
99 * @param version version of the chart
100 * @return Status of operation
101 * @throws ServiceException in case of error.
103 @DeleteMapping(path = "/uninstall/{name}/{version}", produces = MediaType.APPLICATION_JSON_VALUE)
104 @ApiOperation(value = "Uninstall the Chart")
105 @ApiResponses(value = {@ApiResponse(code = 201, message = "chart Uninstalled")})
106 public ResponseEntity<Object> uninstallChart(@PathVariable("name") String name,
107 @PathVariable("version") String version) throws ServiceException {
108 ChartInfo chart = chartService.getChart(name, version);
110 return new ResponseEntity<>(HttpStatus.NOT_FOUND);
113 chartService.uninstallChart(chart);
114 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
118 * REST endpoint to onboard a chart.
120 * @param chartFile Multipart file for the helm chart
121 * @param infoJson AppInfo of the chart
122 * @param overrideFile the file for overriding the chart
123 * @return Status of onboard operation
124 * @throws ServiceException in case of error
125 * @throws IOException in case of IO error
127 @PostMapping(path = "/charts", consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
128 produces = MediaType.APPLICATION_JSON_VALUE)
129 @ApiOperation(value = "Onboard the Chart")
130 @ApiResponses(value = {@ApiResponse(code = 201, message = "Chart Onboarded")})
131 public ResponseEntity<String> onboardChart(@RequestPart("chart") MultipartFile chartFile,
132 @RequestParam(name = "values", required = false) MultipartFile overrideFile,
133 @RequestParam("info") String infoJson) throws ServiceException, IOException {
137 info = CODER.decode(infoJson, ChartInfo.class);
138 } catch (CoderException e) {
139 throw new ServiceException("Error parsing the chart information", e);
142 chartService.saveChart(info, chartFile, overrideFile);
143 return new ResponseEntity<>(HttpStatus.OK);
147 * REST endpoint to delete a specific helm chart.
149 * @param name name of the chart
150 * @param version version of the chart
151 * @return Status of operation
152 * @throws ServiceException in case of error.
154 @DeleteMapping(path = "/charts/{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) throws ServiceException {
160 ChartInfo chart = chartService.getChart(name, version);
162 return new ResponseEntity<>(HttpStatus.NOT_FOUND);
165 chartService.deleteChart(chart);
166 return new ResponseEntity<>(HttpStatus.NO_CONTENT);