2 * # ============LICENSE_START=======================================================
3 * # Copyright (c) 2021 AT&T Intellectual Property. 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.dcaegen2.platform.helmchartgenerator.distribution;
21 import lombok.extern.slf4j.Slf4j;
22 import okhttp3.Credentials;
23 import okhttp3.MediaType;
24 import okhttp3.OkHttpClient;
25 import okhttp3.Request;
26 import okhttp3.RequestBody;
27 import okhttp3.Response;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.stereotype.Component;
32 import java.io.IOException;
33 import java.util.Objects;
36 * Distributes helm chart to Chart Museum through REST
37 * @author Dhrumin Desai
41 public class ChartMuseumDistributor implements ChartDistributor {
43 private final String chartMuseumUrl;
45 private final String username;
47 private final String password;
49 public ChartMuseumDistributor( @Value("${chartmuseum.baseurl}") String chartMuseumUrl,
50 @Value("${chartmuseum.auth.basic.username}") String username,
51 @Value("${chartmuseum.auth.basic.password}")String password) {
52 this.chartMuseumUrl = chartMuseumUrl;
53 this.username = username;
54 this.password = password;
58 * distributes chart to Chart Museum
59 * @param chartFile packaged helm chart tgz file
62 public void distribute(File chartFile) {
63 OkHttpClient client = new OkHttpClient().newBuilder().build();
64 Request request = createRequestBody(chartFile);
66 Response response = client.newCall(request).execute();
67 log.info(Objects.requireNonNull(response.body()).string());
68 if(!response.isSuccessful()){
69 throw new RuntimeException("Distribution Failed.");
71 } catch (IOException e) {
72 throw new RuntimeException(e.getMessage());
76 private Request createRequestBody(File chartFile) {
77 String credential = Credentials.basic(username, password);
78 MediaType mediaType = MediaType.parse("application/octet-stream");
79 RequestBody body = RequestBody.create(chartFile, mediaType);
80 return new Request.Builder()
81 .url(chartMuseumUrl + "/api/charts")
83 .addHeader("Content-Type", "application/octet-stream")
84 .addHeader("Authorization", credential)