Improved helm-generator code to make it more testable and improved code coverage
[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     private final String chartMuseumUrl;
44
45     private final String username;
46
47     private final String password;
48
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;
55     }
56
57     /**
58      * distributes chart to Chart Museum
59      * @param chartFile packaged helm chart tgz file
60      */
61     @Override
62     public void distribute(File chartFile) {
63         OkHttpClient client = new OkHttpClient().newBuilder().build();
64         Request request = createRequestBody(chartFile);
65         try {
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.");
70             }
71         } catch (IOException e) {
72             throw new RuntimeException(e.getMessage());
73         }
74     }
75
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")
82                 .method("POST", body)
83                 .addHeader("Content-Type", "application/octet-stream")
84                 .addHeader("Authorization", credential)
85                 .build();
86     }
87 }