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 / 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.Setter;
22 import lombok.extern.slf4j.Slf4j;
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 import static org.onap.dcaegen2.platform.helmchartgenerator.Utils.cloneFileToTempLocation;
30 import static org.onap.dcaegen2.platform.helmchartgenerator.Utils.deleteTempFileLocation;
31
32 /**
33  * ChartGenerator interacts with HelmClient and generates a packaged helm chart
34  * @author Dhrumin Desai
35  */
36 @Component
37 @Slf4j
38 public class ChartGenerator {
39
40     @Setter
41     @Autowired
42     private HelmClient helmClient;
43
44     @Setter
45     @Autowired
46     private KeyValueMerger merger;
47
48     /**
49      * Constructor for ChartGenerator
50      * @param helmClient HelmClient implementation
51      * @param merger KeyValueMerger implementation
52      */
53     public ChartGenerator(HelmClient helmClient, KeyValueMerger merger) {
54         this.helmClient = helmClient;
55         this.merger = merger;
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 = cloneFileToTempLocation(chartBlueprintLocation + "/base");
67         merger.mergeValuesToChart(chartInfo, newChartDir);
68         helmClient.lint(newChartDir);
69         final File chartLocation = helmClient.packageChart(newChartDir, outputLocation);
70         deleteTempFileLocation(newChartDir);
71         return chartLocation;
72     }
73 }