2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2024 OpenInfra Foundation Europe. 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========================================================================
20 package com.oransc.rappmanager.service;
22 import com.oransc.rappmanager.models.csar.DeploymentItem;
23 import com.oransc.rappmanager.models.csar.DeploymentItemArtifactType;
24 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
25 import com.oransc.rappmanager.models.exception.RappHandlerException;
26 import com.oransc.rappmanager.models.rapp.Rapp;
27 import java.util.List;
28 import lombok.RequiredArgsConstructor;
29 import org.springframework.core.io.ByteArrayResource;
30 import org.springframework.http.HttpEntity;
31 import org.springframework.http.HttpHeaders;
32 import org.springframework.http.HttpMethod;
33 import org.springframework.http.HttpStatus;
34 import org.springframework.http.MediaType;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.stereotype.Service;
37 import org.springframework.web.client.HttpClientErrorException;
38 import org.springframework.web.client.RestTemplate;
41 @RequiredArgsConstructor
42 public class DeploymentArtifactsService {
44 private final RestTemplate restTemplate;
45 private final RappCsarConfigurationHandler rappCsarConfigurationHandler;
47 public boolean configureDeploymentArtifacts(Rapp rapp) {
48 List<DeploymentItem> deploymentItems = rappCsarConfigurationHandler.getDeploymentItems(rapp);
49 return deploymentItems.stream().filter(deploymentItem -> deploymentItem.getArtifactType()
50 .equals(DeploymentItemArtifactType.HELMCHART))
51 .allMatch(deploymentItem -> uploadHelmChart(rapp, deploymentItem));
54 boolean uploadHelmChart(Rapp rApp, DeploymentItem deploymentItem) throws RappHandlerException {
56 HttpHeaders httpHeaders = new HttpHeaders();
57 httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
58 HttpEntity<ByteArrayResource> requestHttpEntity =
59 new HttpEntity<>(rappCsarConfigurationHandler.getArtifactPayload(rApp, deploymentItem.getFile()),
61 ResponseEntity<String> responseEntity =
62 restTemplate.exchange(deploymentItem.getTargetServerUri(), HttpMethod.POST, requestHttpEntity,
64 if (responseEntity.getStatusCode().is2xxSuccessful()) {
67 } catch (HttpClientErrorException exception) {
68 if (exception.getStatusCode().equals(HttpStatus.CONFLICT)) {
71 } catch (Exception e) {
72 throw new RappHandlerException(HttpStatus.BAD_REQUEST,
73 String.format("Unable to connect to the chartmuseum server %s to upload helm artifact %s",
74 deploymentItem.getTargetServerUri(), deploymentItem.getFile()));