Helm-generator - seedcode delivery for helm chart generation tool from component...
[dcaegen2/platform.git] / mod2 / helm-generator / helmchartgenerator-core / src / main / java / org / onap / dcaegen2 / platform / helmchartgenerator / distribution / ChartMuseumDistributor.java
1 /*
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
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.dcaegen2.platform.helmchartgenerator.distribution;
20
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;
30
31 import java.io.File;
32 import java.io.IOException;
33 import java.util.Objects;
34
35 /**
36  * Distributes helm chart to Chart Museum through REST
37  * @author Dhrumin Desai
38  */
39 @Component
40 @Slf4j
41 public class ChartMuseumDistributor implements ChartDistributor {
42
43     @Value("${chartmuseum.baseurl}")
44     private String chartMuseumUrl;
45
46     @Value("${chartmuseum.auth.basic.username}")
47     private String username;
48
49     @Value("${chartmuseum.auth.basic.password}")
50     private String password;
51
52     /**
53      * distributes chart to Chart Museum
54      * @param chartFile packaged helm chart tgz file
55      */
56     @Override
57     public void distribute(File chartFile) {
58         OkHttpClient client = new OkHttpClient().newBuilder().build();
59         Request request = createRequestBody(chartFile);
60         try {
61             Response response = client.newCall(request).execute();
62             log.info(Objects.requireNonNull(response.body()).string());
63         } catch (IOException e) {
64             throw new RuntimeException(e.getMessage());
65         }
66     }
67
68     private Request createRequestBody(File chartFile) {
69         String credential = Credentials.basic(username, password);
70         MediaType mediaType = MediaType.parse("application/octet-stream");
71         RequestBody body = RequestBody.create(chartFile, mediaType);
72         return new Request.Builder()
73                 .url(chartMuseumUrl)
74                 .method("POST", body)
75                 .addHeader("Content-Type", "application/octet-stream")
76                 .addHeader("Authorization", credential)
77                 .build();
78     }
79 }