1
+ require " yaml"
2
+
3
+ module CNFInstall
4
+ class ConfigTransformer
5
+ # Initialize with the original configuration hash
6
+ def initialize (@old_config : Hash )
7
+ end
8
+
9
+ # Perform the transformation to the new config format
10
+ def transform : Hash
11
+ transformed_config = {
12
+ " config_version" => " 2.0" , # New version identifier
13
+ " common_parameters" => transform_common_parameters,
14
+ " dynamic_parameters" => transform_dynamic_parameters,
15
+ " deployments" => transform_deployments
16
+ }
17
+
18
+ transformed_config
19
+ end
20
+
21
+ private def transform_common_parameters : Hash
22
+ {
23
+ " service_name" => @old_config [" service_name" ]?.as_s,
24
+ " rolling_update_tag" => @old_config [" rolling_update_tag" ]?.as_s,
25
+ " white_list_container_names" => @old_config [" white_list_container_names" ]?.as_a.map(& .as_s),
26
+ " docker_insecure_registries" => @old_config [" docker_insecure_registries" ]?.as_a.map(& .as_s),
27
+ " image_registry_fqdns" => @old_config [" image_registry_fqdns" ]?.as_h(String , String ),
28
+ " container_names" => transform_container_names(@old_config [" container_names" ]?.as_a),
29
+ " five_g_parameters" => transform_five_g_parameters
30
+ }
31
+ end
32
+
33
+ private def transform_dynamic_parameters : Hash
34
+ {
35
+ " source_cnf_dir" => @old_config [" source_cnf_dir" ]?.as_s,
36
+ " yml_file_path" => @old_config [" yml_file_path" ]?.as_s,
37
+ " destination_cnf_dir" => @old_config [" destination_cnf_dir" ]?.as_s,
38
+ " install_method" => transform_install_method(@old_config [" install_method" ])
39
+ }
40
+ end
41
+
42
+ private def transform_deployment : Hash
43
+ case
44
+ when @old_config .has_key?(" manifest_directory" )
45
+ transform_manifests
46
+ when @old_config .has_key?(" helm_directory" )
47
+ transform_helm_directories
48
+ else
49
+ transform_helm_charts
50
+ end
51
+ end
52
+
53
+ private def transform_container_names (container_names : Array (Hash )?) : Array (Hash )
54
+ return nil unless container_names
55
+
56
+ container_names.map do |container |
57
+ {
58
+ " name" => container[" name" ]?.as_s,
59
+ " rolling_update_test_tag" => container[" rolling_update_test_tag" ]?.as_s,
60
+ " rolling_downgrade_test_tag" => container[" rolling_downgrade_test_tag" ]?.as_s,
61
+ " rolling_version_change_test_tag" => container[" rolling_version_change_test_tag" ]?.as_s,
62
+ " rollback_from_tag" => container[" rollback_from_tag" ]?.as_s
63
+ }
64
+ end
65
+ end
66
+
67
+ private def transform_five_g_parameters : Hash
68
+ {
69
+ " amf_label" => @old_config [" amf_label" ]?.as_s,
70
+ " smf_label" => @old_config [" smf_label" ]?.as_s,
71
+ " upf_label" => @old_config [" upf_label" ]?.as_s,
72
+ " ric_label" => @old_config [" ric_label" ]?.as_s,
73
+ " amf_service_name" => @old_config [" amf_service_name" ]?.as_s,
74
+ " mmc" => @old_config [" mmc" ]?.as_s,
75
+ " mnc" => @old_config [" mnc" ]?.as_s,
76
+ " sst" => @old_config [" sst" ]?.as_s,
77
+ " sd" => @old_config [" sd" ]?.as_s,
78
+ " tac" => @old_config [" tac" ]?.as_s,
79
+ " protectionScheme" => @old_config [" protectionScheme" ]?.as_s,
80
+ " publicKey" => @old_config [" publicKey" ]?.as_s,
81
+ " publicKeyId" => @old_config [" publicKeyId" ]?.as_s,
82
+ " routingIndicator" => @old_config [" routingIndicator" ]?.as_s,
83
+ " enabled" => @old_config [" enabled" ]?.as_s,
84
+ " count" => @old_config [" count" ]?.as_s,
85
+ " initialMSISDN" => @old_config [" initialMSISDN" ]?.as_s,
86
+ " key" => @old_config [" key" ]?.as_s,
87
+ " op" => @old_config [" op" ]?.as_s,
88
+ " opType" => @old_config [" opType" ]?.as_s,
89
+ " type" => @old_config [" type" ]?.as_s,
90
+ " apn" => @old_config [" apn" ]?.as_s,
91
+ " emergency" => @old_config [" emergency" ]?.as_s
92
+ }
93
+ end
94
+
95
+ private def transform_install_method (install_method : Tuple ?) : Hash (String , String | Nil )
96
+ return nil unless install_method
97
+
98
+ # Not sure about this one
99
+ {
100
+ " type" => install_method[0 ]?.as_s,
101
+ " description" => install_method[1 ]?.as_s
102
+ }
103
+ end
104
+
105
+ private def transform_helm_charts : Array
106
+ [{
107
+ " name" => @old_config [" release_name" ]?.as_s,
108
+ " helm_repo_name" => @old_config [" release_name" ]?.as_s,
109
+ " helm_repo_url" => @old_config [" release_name" ]?.as_s,
110
+ " helm_chart_name" => @old_config [" release_name" ]?.as_s,
111
+ " helm_values" => @old_config [" release_name" ]?.as_s,
112
+ " namespace" => @old_config [" release_name" ]?.as_s
113
+ }]
114
+ end
115
+
116
+ private def transform_helm_directories : Array
117
+ [{
118
+ " name" => @old_config [" release_name" ]?.as_s,
119
+ " helm_directory" => @old_config [" helm_directory" ]?.as_s,
120
+ " namespace" => @old_config [" namespace" ]?.as_s
121
+ }]
122
+ end
123
+
124
+ private def transform_manifests : Array
125
+ [{
126
+ " name" => @old_config [" release_name" ]?.as_s,
127
+ " manifest_directory" => @old_config [" manifest_directory" ]?.as_s
128
+ }]
129
+ end
130
+ end
131
+ end
0 commit comments