1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
/*
* # ============LICENSE_START=======================================================
* # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved.
* # ================================================================================
* # Licensed under the Apache License, Version 2.0 (the "License");
* # you may not use this file except in compliance with the License.
* # You may obtain a copy of the License at
* #
* # http://www.apache.org/licenses/LICENSE-2.0
* #
* # Unless required by applicable law or agreed to in writing, software
* # distributed under the License is distributed on an "AS IS" BASIS,
* # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* # See the License for the specific language governing permissions and
* # limitations under the License.
* # ============LICENSE_END=========================================================
*/
package org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder;
import lombok.extern.slf4j.Slf4j;
import org.onap.dcaegen2.platform.helmchartgenerator.models.chartinfo.ChartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.nio.file.Paths;
import java.util.Map;
/**
* KeyValueMerger merges helm base templates key-values with key-values parsed from component specification file
* @author Dhrumin Desai
*/
@Component
@Slf4j
public class KeyValueMerger {
@Autowired
private Yaml yaml;
public KeyValueMerger(Yaml yaml) {
this.yaml = yaml;
}
/**
* merges helm base templates key-values with key-values parsed from component specification file
* @param chartInfo populated ChartInfo object which holds key-values parsed from component spec file
* @param chartDir location of the base helm chart template
*/
public void mergeValuesToChart(ChartInfo chartInfo, File chartDir) {
mergeChartYamlFile(chartInfo, chartDir);
mergeValuesYamlFile(chartInfo, chartDir);
}
private void mergeChartYamlFile(ChartInfo chartInfo, File chartDir) {
String chartYamlFilePath = Paths.get(chartDir.getAbsolutePath(), "Chart.yaml").toString();
checkIfFIleExists(chartYamlFilePath, "Chart.yaml is not found in the given chart template.");
Map<String, Object> chartYamlKV;
try {
chartYamlKV = yaml.load(new FileInputStream(chartYamlFilePath));
chartYamlKV.put("name", chartInfo.getMetadata().getName());
chartYamlKV.put("version", chartInfo.getMetadata().getVersion());
chartYamlKV.put("description", chartInfo.getMetadata().getDescription());
yaml.dump(chartYamlKV, new PrintWriter(chartYamlFilePath));
} catch (FileNotFoundException e) {
log.error(e.getMessage());
}
}
private void mergeValuesYamlFile(ChartInfo chartInfo, File chartDir) {
String valuesYamlFilePath = Paths.get(chartDir.getAbsolutePath(), "values.yaml").toString();
checkIfFIleExists(valuesYamlFilePath, "values.yaml is not found in the given chart template.");
Map<String, Object> valuesYamlKv;
try {
valuesYamlKv = yaml.load(new FileInputStream(valuesYamlFilePath));
valuesYamlKv.putAll(chartInfo.getValues());
yaml.dump(valuesYamlKv, new PrintWriter(valuesYamlFilePath));
} catch (FileNotFoundException e) {
log.error(e.getMessage());
}
}
private void checkIfFIleExists(String filePath, String message) {
File valuesYaml = new File(filePath);
if (!valuesYaml.exists()) {
throw new RuntimeException(message);
}
}
}
|