be02d68df20cd5f22f54c71e538dbb5fc1b078c4
[dcaegen2/platform.git] / mod2 / helm-generator / helmchartgenerator-core / src / main / java / org / onap / dcaegen2 / platform / helmchartgenerator / chartbuilder / ChartGenerator.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.chartbuilder;
20
21 import lombok.extern.slf4j.Slf4j;
22 import org.onap.dcaegen2.platform.helmchartgenerator.Utils;
23 import org.onap.dcaegen2.platform.helmchartgenerator.models.chartinfo.ChartInfo;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.stereotype.Component;
26
27 import java.io.File;
28
29 /**
30  * ChartGenerator interacts with HelmClient and generates a packaged helm chart
31  * @author Dhrumin Desai
32  */
33 @Component
34 @Slf4j
35 public class ChartGenerator {
36
37     @Autowired
38     private HelmClient helmClient;
39
40     @Autowired
41     private KeyValueMerger merger;
42
43     @Autowired
44     private Utils utils;
45
46     /**
47      * Constructor for ChartGenerator
48      * @param helmClient HelmClient implementation
49      * @param merger KeyValueMerger implementation
50      * @param utils
51      */
52     public ChartGenerator(HelmClient helmClient, KeyValueMerger merger, Utils utils) {
53         this.helmClient = helmClient;
54         this.merger = merger;
55         this.utils = utils;
56     }
57
58     /**
59      * Merges the key-values from the helm base template and parsed spec file and generates a new packaged helm chart
60      * @param chartBlueprintLocation location of the base helm chart template
61      * @param chartInfo chartInfo object with key-values parsed from the specfile.
62      * @param outputLocation location to store the helm chart
63      * @return generated helm chart tgz file
64      */
65     public File generate(String chartBlueprintLocation, ChartInfo chartInfo, String outputLocation) {
66         File newChartDir = utils.cloneFileToTempLocation(chartBlueprintLocation + "/base");
67         merger.mergeValuesToChart(chartInfo, newChartDir);
68         helmClient.lint(newChartDir);
69         final File chartLocation = helmClient.packageChart(newChartDir, outputLocation);
70         utils.deleteTempFileLocation(newChartDir);
71         return chartLocation;
72     }
73 }