aboutsummaryrefslogtreecommitdiffstats
path: root/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/controlloop/participant/kubernetes/controller/ChartController.java
blob: e2ccda4d5e1dd2888f9ae980607e7e1679ca7c5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*-
 * ========================LICENSE_START=================================
 * Copyright (C) 2021-2022 Nordix Foundation. All rights reserved.
 * ======================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================LICENSE_END===================================
 */

package org.onap.policy.clamp.controlloop.participant.kubernetes.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.io.IOException;
import java.util.ArrayList;
import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException;
import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartList;
import org.onap.policy.clamp.controlloop.participant.kubernetes.models.HelmRepository;
import org.onap.policy.clamp.controlloop.participant.kubernetes.models.InstallationInfo;
import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartService;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController("chartController")
@ConditionalOnExpression("${chart.api.enabled:false}")
@RequestMapping("helm")
@Api(tags = {"k8s-participant"})
public class ChartController {

    @Autowired
    private ChartService chartService;

    private static final StandardCoder CODER = new StandardCoder();

    /**
     * REST endpoint to get all the charts.
     *
     * @return List of charts installed
     */
    @GetMapping(path = "/charts", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "Return all Charts")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "chart List")})
    public ResponseEntity<ChartList> getAllCharts() {
        return new ResponseEntity<>(ChartList.builder().charts(new ArrayList<>(chartService.getAllCharts())).build(),
                HttpStatus.OK);
    }

    /**
     * REST endpoint to install a helm chart.
     *
     * @param info Info of the chart to be installed
     * @return Status of the install operation
     * @throws ServiceException in case of error
     * @throws IOException in case of IO error
     */
    @PostMapping(path = "/install", consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "Install the chart")
    @ApiResponses(value = {@ApiResponse(code = 201, message = "chart Installed")})
    public ResponseEntity<Object> installChart(@RequestBody InstallationInfo info)
            throws ServiceException, IOException {
        ChartInfo chart = chartService.getChart(info.getName(), info.getVersion());
        if (chart == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        chartService.installChart(chart);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * REST endpoint to uninstall a specific chart.
     *
     * @param name name of the chart
     * @param version version of the chart
     * @return Status of operation
     * @throws ServiceException in case of error.
     */
    @DeleteMapping(path = "/uninstall/{name}/{version}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "Uninstall the Chart")
    @ApiResponses(value = {@ApiResponse(code = 201, message = "chart Uninstalled")})
    public ResponseEntity<Object> uninstallChart(@PathVariable("name") String name,
            @PathVariable("version") String version) throws ServiceException {
        ChartInfo chart = chartService.getChart(name, version);
        if (chart == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        chartService.uninstallChart(chart);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * REST endpoint to onboard a chart.
     *
     * @param chartFile Multipart file for the helm chart
     * @param infoJson AppInfo of the chart
     * @param overrideFile the file for overriding the chart
     * @return Status of onboard operation
     * @throws ServiceException in case of error
     * @throws IOException in case of IO error
     */
    @PostMapping(path = "/onboard/chart", consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "Onboard the Chart")
    @ApiResponses(value = {@ApiResponse(code = 201, message = "Chart Onboarded")})
    public ResponseEntity<String> onboardChart(@RequestPart("chart") MultipartFile chartFile,
            @RequestParam(name = "values", required = false) MultipartFile overrideFile,
            @RequestParam("info") String infoJson) throws ServiceException, IOException {

        ChartInfo info;
        try {
            info = CODER.decode(infoJson, ChartInfo.class);
        } catch (CoderException e) {
            throw new ServiceException("Error parsing the chart information", e);
        }

        chartService.saveChart(info, chartFile, overrideFile);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * REST endpoint to delete a specific helm chart.
     *
     * @param name name of the chart
     * @param version version of the chart
     * @return Status of operation
     */
    @DeleteMapping(path = "/chart/{name}/{version}")
    @ApiOperation(value = "Delete the chart")
    @ApiResponses(value = {@ApiResponse(code = 204, message = "Chart Deleted")})
    public ResponseEntity<Object> deleteChart(@PathVariable("name") String name,
            @PathVariable("version") String version) {

        ChartInfo chart = chartService.getChart(name, version);
        if (chart == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        chartService.deleteChart(chart);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * REST endpoint to configure a helm Repository.
     *
     * @param repo Helm repository to be configured
     * @return Status of the operation
     * @throws ServiceException in case of error
     * @throws IOException in case of IO error
     */
    @PostMapping(path = "/repo", consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "Configure helm repository")
    @ApiResponses(value = {@ApiResponse(code = 201, message = "Repository added")})
    public ResponseEntity<Object> configureRepo(@RequestBody String repo)
            throws ServiceException, IOException {
        HelmRepository repository;
        try {
            repository = CODER.decode(repo, HelmRepository.class);
        } catch (CoderException e) {
            throw new ServiceException("Error parsing the repository information", e);
        }
        chartService.configureRepository(repository);

        return new ResponseEntity<>(HttpStatus.CREATED);
    }
}