427b06fc56fc82ab181dcae3f2a3229b72c0ff1a
[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 incase of error
78      */
79     @PostMapping(path = "/install", consumes = MediaType.APPLICATION_JSON_VALUE,
80             produces = MediaType.APPLICATION_JSON_VALUE)
81     @ApiOperation(value = "Install the chart")
82     @ApiResponses(value = {@ApiResponse(code = 201, message = "chart Installed")})
83     public ResponseEntity<Object> installChart(@RequestBody InstallationInfo info)
84             throws ServiceException, IOException {
85         ChartInfo chart = chartService.getChart(info.getName(), info.getVersion());
86         if (chart == null) {
87             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
88         }
89
90         chartService.installChart(chart);
91         return new ResponseEntity<>(HttpStatus.CREATED);
92     }
93
94     /**
95      * REST endpoint to uninstall a specific chart.
96      *
97      * @param name name of the chart
98      * @param version version of the chart
99      * @return Status of operation
100      * @throws ServiceException incase of error.
101      */
102     @DeleteMapping(path = "/uninstall/{name}/{version}", produces = MediaType.APPLICATION_JSON_VALUE)
103     @ApiOperation(value = "Uninstall the Chart")
104     @ApiResponses(value = {@ApiResponse(code = 201, message = "chart Uninstalled")})
105     public ResponseEntity<Object> uninstallChart(@PathVariable("name") String name,
106             @PathVariable("version") String version) throws ServiceException {
107         ChartInfo chart = chartService.getChart(name, version);
108         if (chart == null) {
109             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
110         }
111
112         chartService.uninstallChart(chart);
113         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
114     }
115
116     /**
117      * REST endpoint to onboard a chart.
118      *
119      * @param chartFile Multipart file for the helm chart
120      * @param infoJson AppInfo of the chart
121      * @return Status of onboard operation
122      * @throws ServiceException incase of error
123      * @throws IOException incase of IO error
124      */
125     @PostMapping(path = "/charts", consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
126             produces = MediaType.APPLICATION_JSON_VALUE)
127     @ApiOperation(value = "Onboard the Chart")
128     @ApiResponses(value = {@ApiResponse(code = 201, message = "Chart Onboarded")})
129     public ResponseEntity<String> onboardChart(@RequestPart("chart") MultipartFile chartFile,
130             @RequestParam(name = "values", required = false) MultipartFile overrideFile,
131             @RequestParam("info") String infoJson) throws ServiceException, IOException {
132
133         ChartInfo info;
134         try {
135             info = CODER.decode(infoJson, ChartInfo.class);
136         } catch (CoderException e) {
137             throw new ServiceException("Error parsing the chart information", e);
138         }
139
140         chartService.saveChart(info, chartFile, overrideFile);
141         return new ResponseEntity<>(HttpStatus.OK);
142     }
143
144     /**
145      * REST endpoint to delete a specific helm chart.
146      *
147      * @param name name of the chart
148      * @param version version of the chart
149      * @return Status of operation
150      * @throws ServiceException incase of error.
151      */
152     @DeleteMapping(path = "/charts/{name}/{version}")
153     @ApiOperation(value = "Delete the chart")
154     @ApiResponses(value = {@ApiResponse(code = 204, message = "Chart Deleted")})
155     public ResponseEntity<Object> deleteChart(@PathVariable("name") String name,
156             @PathVariable("version") String version) throws ServiceException {
157
158         ChartInfo chart = chartService.getChart(name, version);
159         if (chart == null) {
160             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
161         }
162
163         chartService.deleteChart(chart);
164         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
165     }
166 }