|
32 | 32 | import java.util.stream.Collectors; |
33 | 33 |
|
34 | 34 | import org.apache.commons.lang3.StringUtils; |
| 35 | +import org.apache.commons.lang3.Strings; |
35 | 36 | import org.yaml.snakeyaml.DumperOptions; |
36 | 37 | import org.yaml.snakeyaml.Yaml; |
37 | 38 |
|
|
46 | 47 | * Exports model information for each node in YAML format. |
47 | 48 | * This is useful e.g. for integration with Ansible. |
48 | 49 | */ |
49 | | -<span class="fc" id="L49">public class YamlNodeModelExport implements NodeModelExportPlugin {</span> |
| 50 | +<span class="fc" id="L50">public class YamlNodeModelExport implements NodeModelExportPlugin {</span> |
50 | 51 |
|
51 | 52 | /** |
52 | 53 | * Plugin name |
|
57 | 58 |
|
58 | 59 | @Override |
59 | 60 | public String getName() { |
60 | | -<span class="fc" id="L60"> return NAME;</span> |
| 61 | +<span class="fc" id="L61"> return NAME;</span> |
61 | 62 | } |
62 | 63 |
|
63 | 64 | @Override |
64 | 65 | public void export(NodeModelExportContext context) { |
65 | 66 |
|
66 | 67 | // generate YAML data |
67 | | -<span class="fc" id="L67"> List<Map<String, Object>> roles = new ArrayList<>();</span> |
68 | | -<span class="fc bfc" id="L68" title="All 2 branches covered."> for (ExportNodeRoleData roleData : context.getRoleData()) {</span> |
69 | | -<span class="fc" id="L69"> addRole(roles, roleData, context);</span> |
70 | | -<span class="fc" id="L70"> }</span> |
| 68 | +<span class="fc" id="L68"> List<Map<String, Object>> roles = new ArrayList<>();</span> |
| 69 | +<span class="fc bfc" id="L69" title="All 2 branches covered."> for (ExportNodeRoleData roleData : context.getRoleData()) {</span> |
| 70 | +<span class="fc" id="L70"> addRole(roles, roleData, context);</span> |
| 71 | +<span class="fc" id="L71"> }</span> |
71 | 72 |
|
72 | | -<span class="fc" id="L72"> Map<String, Object> modelMap = new LinkedHashMap<>();</span> |
73 | | -<span class="fc" id="L73"> modelMap.put("roles", roles);</span> |
| 73 | +<span class="fc" id="L73"> Map<String, Object> modelMap = new LinkedHashMap<>();</span> |
| 74 | +<span class="fc" id="L74"> modelMap.put("roles", roles);</span> |
74 | 75 |
|
75 | | -<span class="fc" id="L75"> Map<String, String> versionInfo = context.getContainerVersionInfo();</span> |
76 | | -<span class="pc bpc" id="L76" title="1 of 2 branches missed."> if (versionInfo != null) {</span> |
77 | | -<span class="nc" id="L77"> modelMap.put("versionInfo", Collections.unmodifiableSortedMap(new TreeMap<>(versionInfo)));</span> |
| 76 | +<span class="fc" id="L76"> Map<String, String> versionInfo = context.getContainerVersionInfo();</span> |
| 77 | +<span class="pc bpc" id="L77" title="1 of 2 branches missed."> if (versionInfo != null) {</span> |
| 78 | +<span class="nc" id="L78"> modelMap.put("versionInfo", Collections.unmodifiableSortedMap(new TreeMap<>(versionInfo)));</span> |
78 | 79 | } |
79 | 80 |
|
80 | 81 | // save YAML file |
81 | | -<span class="fc" id="L81"> save(modelMap, context);</span> |
82 | | -<span class="fc" id="L82"> }</span> |
| 82 | +<span class="fc" id="L82"> save(modelMap, context);</span> |
| 83 | +<span class="fc" id="L83"> }</span> |
83 | 84 |
|
84 | 85 | private void addRole(List<Map<String, Object>> modelList, ExportNodeRoleData roleData, NodeModelExportContext context) { |
85 | | -<span class="fc" id="L85"> String nodeDirPath = FileUtil.getCanonicalPath(context.getNodeDir());</span> |
| 86 | +<span class="fc" id="L86"> String nodeDirPath = FileUtil.getCanonicalPath(context.getNodeDir());</span> |
86 | 87 |
|
87 | | -<span class="fc" id="L87"> Map<String, Object> roleMap = new LinkedHashMap<>();</span> |
88 | | -<span class="fc" id="L88"> roleMap.put("role", roleData.getRole());</span> |
| 88 | +<span class="fc" id="L88"> Map<String, Object> roleMap = new LinkedHashMap<>();</span> |
| 89 | +<span class="fc" id="L89"> roleMap.put("role", roleData.getRole());</span> |
89 | 90 |
|
90 | | -<span class="fc" id="L90"> List<String> variants = roleData.getRoleVariants();</span> |
91 | | -<span class="fc bfc" id="L91" title="All 2 branches covered."> if (variants.size() == 1) {</span> |
92 | | -<span class="fc" id="L92"> roleMap.put("variant", variants.get(0));</span> |
| 91 | +<span class="fc" id="L91"> List<String> variants = roleData.getRoleVariants();</span> |
| 92 | +<span class="fc bfc" id="L92" title="All 2 branches covered."> if (variants.size() == 1) {</span> |
| 93 | +<span class="fc" id="L93"> roleMap.put("variant", variants.get(0));</span> |
93 | 94 | } |
94 | | -<span class="fc bfc" id="L94" title="All 2 branches covered."> if (!variants.isEmpty()) {</span> |
95 | | -<span class="fc" id="L95"> roleMap.put("variants", variants);</span> |
| 95 | +<span class="fc bfc" id="L95" title="All 2 branches covered."> if (!variants.isEmpty()) {</span> |
| 96 | +<span class="fc" id="L96"> roleMap.put("variants", variants);</span> |
96 | 97 | } |
97 | 98 |
|
98 | | -<span class="fc" id="L98"> roleMap.put("files", roleData.getFiles().stream()</span> |
99 | | -<span class="fc" id="L99"> .filter(item -> item.getFileContext().getFile().exists())</span> |
100 | | -<span class="fc" id="L100"> .map(item -> {</span> |
101 | | -<span class="fc" id="L101"> Map<String, Object> itemMap = new LinkedHashMap<>();</span> |
102 | | -<span class="fc" id="L102"> itemMap.put("path", cleanupFileName(item.getFileContext().getCanonicalPath(), nodeDirPath));</span> |
103 | | -<span class="pc bpc" id="L103" title="1 of 2 branches missed."> if (!item.getPostProcessors().isEmpty()) {</span> |
104 | | -<span class="nc" id="L104"> itemMap.put("postProcessors", List.copyOf(item.getPostProcessors()));</span> |
| 99 | +<span class="fc" id="L99"> roleMap.put("files", roleData.getFiles().stream()</span> |
| 100 | +<span class="fc" id="L100"> .filter(item -> item.getFileContext().getFile().exists())</span> |
| 101 | +<span class="fc" id="L101"> .map(item -> {</span> |
| 102 | +<span class="fc" id="L102"> Map<String, Object> itemMap = new LinkedHashMap<>();</span> |
| 103 | +<span class="fc" id="L103"> itemMap.put("path", cleanupFileName(item.getFileContext().getCanonicalPath(), nodeDirPath));</span> |
| 104 | +<span class="pc bpc" id="L104" title="1 of 2 branches missed."> if (!item.getPostProcessors().isEmpty()) {</span> |
| 105 | +<span class="nc" id="L105"> itemMap.put("postProcessors", List.copyOf(item.getPostProcessors()));</span> |
105 | 106 | } |
106 | | -<span class="fc" id="L106"> Map<String, Object> modelOptions = item.getFileContext().getModelOptions();</span> |
107 | | -<span class="pc bpc" id="L107" title="1 of 2 branches missed."> if (modelOptions != null) {</span> |
108 | | -<span class="fc bfc" id="L108" title="All 2 branches covered."> for (Map.Entry<String, Object> entry : modelOptions.entrySet()) {</span> |
109 | | -<span class="pc bpc" id="L109" title="1 of 2 branches missed."> if (!itemMap.containsKey(entry.getKey())) {</span> |
110 | | -<span class="fc" id="L110"> itemMap.put(entry.getKey(), entry.getValue());</span> |
| 107 | +<span class="fc" id="L107"> Map<String, Object> modelOptions = item.getFileContext().getModelOptions();</span> |
| 108 | +<span class="pc bpc" id="L108" title="1 of 2 branches missed."> if (modelOptions != null) {</span> |
| 109 | +<span class="fc bfc" id="L109" title="All 2 branches covered."> for (Map.Entry<String, Object> entry : modelOptions.entrySet()) {</span> |
| 110 | +<span class="pc bpc" id="L110" title="1 of 2 branches missed."> if (!itemMap.containsKey(entry.getKey())) {</span> |
| 111 | +<span class="fc" id="L111"> itemMap.put(entry.getKey(), entry.getValue());</span> |
111 | 112 | } |
112 | | -<span class="fc" id="L112"> }</span> |
| 113 | +<span class="fc" id="L113"> }</span> |
113 | 114 | } |
114 | | -<span class="fc" id="L114"> return itemMap;</span> |
| 115 | +<span class="fc" id="L115"> return itemMap;</span> |
115 | 116 | }) |
116 | | -<span class="fc" id="L116"> .collect(Collectors.toList()));</span> |
| 117 | +<span class="fc" id="L117"> .collect(Collectors.toList()));</span> |
117 | 118 |
|
118 | | -<span class="fc" id="L118"> roleMap.put("config", context.getModelExportConfigProcessor().apply(roleData.getConfig()));</span> |
| 119 | +<span class="fc" id="L119"> roleMap.put("config", context.getModelExportConfigProcessor().apply(roleData.getConfig()));</span> |
119 | 120 |
|
120 | | -<span class="fc" id="L120"> addTenants(roleMap, roleData, context);</span> |
| 121 | +<span class="fc" id="L121"> addTenants(roleMap, roleData, context);</span> |
121 | 122 |
|
122 | | -<span class="fc" id="L122"> modelList.add(roleMap);</span> |
123 | | -<span class="fc" id="L123"> }</span> |
| 123 | +<span class="fc" id="L123"> modelList.add(roleMap);</span> |
| 124 | +<span class="fc" id="L124"> }</span> |
124 | 125 |
|
125 | 126 | private void addTenants(Map<String, Object> roleMap, ExportNodeRoleData roleData, NodeModelExportContext context) { |
126 | | -<span class="fc" id="L126"> List<Map<String, Object>> tenants = new ArrayList<>();</span> |
| 127 | +<span class="fc" id="L127"> List<Map<String, Object>> tenants = new ArrayList<>();</span> |
127 | 128 |
|
128 | | -<span class="pc bpc" id="L128" title="1 of 2 branches missed."> if (roleData.getTenantData() != null) {</span> |
129 | | -<span class="fc bfc" id="L129" title="All 2 branches covered."> for (ExportNodeRoleTenantData tenantData : roleData.getTenantData()) {</span> |
130 | | -<span class="fc" id="L130"> addTenant(tenants, tenantData, context);</span> |
131 | | -<span class="fc" id="L131"> }</span> |
| 129 | +<span class="pc bpc" id="L129" title="1 of 2 branches missed."> if (roleData.getTenantData() != null) {</span> |
| 130 | +<span class="fc bfc" id="L130" title="All 2 branches covered."> for (ExportNodeRoleTenantData tenantData : roleData.getTenantData()) {</span> |
| 131 | +<span class="fc" id="L131"> addTenant(tenants, tenantData, context);</span> |
| 132 | +<span class="fc" id="L132"> }</span> |
132 | 133 | } |
133 | 134 |
|
134 | | -<span class="pc bpc" id="L134" title="1 of 2 branches missed."> if (!tenants.isEmpty()) {</span> |
135 | | -<span class="fc" id="L135"> roleMap.put("tenants", tenants);</span> |
| 135 | +<span class="pc bpc" id="L135" title="1 of 2 branches missed."> if (!tenants.isEmpty()) {</span> |
| 136 | +<span class="fc" id="L136"> roleMap.put("tenants", tenants);</span> |
136 | 137 | } |
137 | | -<span class="fc" id="L137"> }</span> |
| 138 | +<span class="fc" id="L138"> }</span> |
138 | 139 |
|
139 | 140 | private void addTenant(List<Map<String, Object>> tenants, ExportNodeRoleTenantData tenantData, NodeModelExportContext context) { |
140 | | -<span class="fc" id="L140"> Map<String, Object> tenantMap = new LinkedHashMap<>();</span> |
| 141 | +<span class="fc" id="L141"> Map<String, Object> tenantMap = new LinkedHashMap<>();</span> |
141 | 142 |
|
142 | | -<span class="fc" id="L142"> tenantMap.put("tenant", context.getVariableStringResolver().resolve(tenantData.getTenant(), tenantData.getConfig()));</span> |
143 | | -<span class="fc bfc" id="L143" title="All 2 branches covered."> if (!tenantData.getRoles().isEmpty()) {</span> |
| 143 | +<span class="fc" id="L143"> tenantMap.put("tenant", context.getVariableStringResolver().resolve(tenantData.getTenant(), tenantData.getConfig()));</span> |
| 144 | +<span class="fc bfc" id="L144" title="All 2 branches covered."> if (!tenantData.getRoles().isEmpty()) {</span> |
144 | 145 | // copy role list for each tenant to avoid alias in YAML export |
145 | | -<span class="fc" id="L145"> tenantMap.put("roles", new ArrayList<>(tenantData.getRoles()));</span> |
| 146 | +<span class="fc" id="L146"> tenantMap.put("roles", new ArrayList<>(tenantData.getRoles()));</span> |
146 | 147 | } |
147 | | -<span class="fc" id="L147"> tenantMap.put("config", context.getModelExportConfigProcessor().apply(tenantData.getConfig()));</span> |
| 148 | +<span class="fc" id="L148"> tenantMap.put("config", context.getModelExportConfigProcessor().apply(tenantData.getConfig()));</span> |
148 | 149 |
|
149 | | -<span class="fc" id="L149"> tenants.add(tenantMap);</span> |
150 | | -<span class="fc" id="L150"> }</span> |
| 150 | +<span class="fc" id="L150"> tenants.add(tenantMap);</span> |
| 151 | +<span class="fc" id="L151"> }</span> |
151 | 152 |
|
152 | 153 | private void save(Map<String, Object> modelMap, NodeModelExportContext context) { |
153 | | -<span class="fc" id="L153"> File file = new File(context.getNodeDir(), MODEL_FILE);</span> |
154 | | -<span class="fc" id="L154"> try (FileOutputStream os = new FileOutputStream(file);</span> |
155 | | -<span class="fc" id="L155"> OutputStreamWriter writer = new OutputStreamWriter(os, StandardCharsets.UTF_8)) {</span> |
156 | | -<span class="fc" id="L156"> Yaml yaml = new Yaml(context.getYamlRepresenter(), new DumperOptions());</span> |
157 | | -<span class="fc" id="L157"> yaml.dump(modelMap, writer);</span> |
| 154 | +<span class="fc" id="L154"> File file = new File(context.getNodeDir(), MODEL_FILE);</span> |
| 155 | +<span class="fc" id="L155"> try (FileOutputStream os = new FileOutputStream(file);</span> |
| 156 | +<span class="fc" id="L156"> OutputStreamWriter writer = new OutputStreamWriter(os, StandardCharsets.UTF_8)) {</span> |
| 157 | +<span class="fc" id="L157"> Yaml yaml = new Yaml(context.getYamlRepresenter(), new DumperOptions());</span> |
| 158 | +<span class="fc" id="L158"> yaml.dump(modelMap, writer);</span> |
158 | 159 | } |
159 | | -<span class="nc" id="L159"> /*CHECKSTYLE:OFF*/ catch (Exception ex) { /*CHECKSTYLE:ON*/</span> |
160 | | -<span class="nc" id="L160"> throw new GeneratorException("Unable to write model file: " + FileUtil.getCanonicalPath(file), ex);</span> |
161 | | -<span class="fc" id="L161"> }</span> |
162 | | -<span class="fc" id="L162"> }</span> |
| 160 | +<span class="nc" id="L160"> /*CHECKSTYLE:OFF*/ catch (Exception ex) { /*CHECKSTYLE:ON*/</span> |
| 161 | +<span class="nc" id="L161"> throw new GeneratorException("Unable to write model file: " + FileUtil.getCanonicalPath(file), ex);</span> |
| 162 | +<span class="fc" id="L162"> }</span> |
| 163 | +<span class="fc" id="L163"> }</span> |
163 | 164 |
|
164 | 165 | private String cleanupFileName(String fileName, String basePath) { |
165 | | -<span class="fc" id="L165"> String relativePath = StringUtils.substring(fileName, basePath.length() + 1);</span> |
166 | | -<span class="fc" id="L166"> return StringUtils.replace(relativePath, File.separator, "/");</span> |
| 166 | +<span class="fc" id="L166"> String relativePath = StringUtils.substring(fileName, basePath.length() + 1);</span> |
| 167 | +<span class="fc" id="L167"> return Strings.CS.replace(relativePath, File.separator, "/");</span> |
167 | 168 | } |
168 | 169 |
|
169 | 170 | } |
|
0 commit comments