23605e6413bf61daad4968f79182149057a7f8bc
[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.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;
48
49 @RestController("chartController")
50 @RequestMapping("helm")
51 @Api(tags = {"chart"})
52 public class ChartController {
53
54     @Autowired
55     private ChartService chartService;
56
57     private static final StandardCoder CODER = new StandardCoder();
58
59     /**
60      * REST endpoint to get all the charts.
61      *
62      * @return List of charts installed
63      */
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(),
69                 HttpStatus.OK);
70     }
71
72     /**
73      * REST endpoint to install a helm chart.
74      *
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
79      */
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());
87         if (chart == null) {
88             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
89         }
90
91         chartService.installChart(chart);
92         return new ResponseEntity<>(HttpStatus.CREATED);
93     }
94
95     /**
96      * REST endpoint to uninstall a specific chart.
97      *
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.
102      */
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);
109         if (chart == null) {
110             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
111         }
112
113         chartService.uninstallChart(chart);
114         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
115     }
116
117     /**
118      * REST endpoint to onboard a chart.
119      *
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
126      */
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 {
134
135         ChartInfo info;
136         try {
137             info = CODER.decode(infoJson, ChartInfo.class);
138         } catch (CoderException e) {
139             throw new ServiceException("Error parsing the chart information", e);
140         }
141
142         chartService.saveChart(info, chartFile, overrideFile);
143         return new ResponseEntity<>(HttpStatus.OK);
144     }
145
146     /**
147      * REST endpoint to delete a specific helm chart.
148      *
149      * @param name name of the chart
150      * @param version version of the chart
151      * @return Status of operation
152      */
153     @DeleteMapping(path = "/charts/{name}/{version}")
154     @ApiOperation(value = "Delete the chart")
155     @ApiResponses(value = {@ApiResponse(code = 204, message = "Chart Deleted")})
156     public ResponseEntity<Object> deleteChart(@PathVariable("name") String name,
157             @PathVariable("version") String version) {
158
159         ChartInfo chart = chartService.getChart(name, version);
160         if (chart == null) {
161             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
162         }
163
164         chartService.deleteChart(chart);
165         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
166     }
167 }