forked from cloudfoundry/cf-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationManifestUtilsV3.java
More file actions
381 lines (336 loc) · 14.8 KB
/
ApplicationManifestUtilsV3.java
File metadata and controls
381 lines (336 loc) · 14.8 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
* Copyright 2013-2021 the original author or authors.
*
* 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.
*/
package org.cloudfoundry.operations.applications;
import static java.util.Collections.emptyMap;
import static java.util.stream.Collectors.toMap;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import org.cloudfoundry.client.v3.Metadata;
import org.cloudfoundry.client.v3.processes.HealthCheckType;
import org.cloudfoundry.client.v3.processes.ReadinessHealthCheckType;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import reactor.core.Exceptions;
/**
* Utilities for dealing with {@link ManifestV3}s. Includes the functionality to transform to and from standard CLI YAML files.
*/
public final class ApplicationManifestUtilsV3 extends ApplicationManifestUtilsCommon {
private static final Yaml YAML;
static {
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
dumperOptions.setExplicitStart(true);
YAML = new Yaml(dumperOptions);
}
private static final Pattern FIND_VARIABLE_REGEX =
Pattern.compile("\\(\\(([a-zA-Z]\\w+)\\)\\)");
private ApplicationManifestUtilsV3() {}
/**
* Reads a YAML manifest file (defined by the <a href="https://v3-apidocs.cloudfoundry.org/version/3.124.0/index.html#manifests">CC API</a>) from a {@link Path} and converts it into a {@link
* ManifestV3} object. Note that all resolution (both inheritance and common) is performed during read.
*
* @param path the path to read from
* @return the resolved manifest
*/
public static ManifestV3 read(Path path) {
return doRead(path.toAbsolutePath(), emptyMap());
}
/**
* Reads a YAML manifest file (defined by the <a href="https://v3-apidocs.cloudfoundry.org/version/3.124.0/index.html#manifests">CC API</a>) from a {@link Path} and converts it into a {@link
* ManifestV3} object. Note that all resolution (both inheritance and common) is performed during read.
*
* @param path the path to read from
* @param variablesPath use variable substitution (described in <a href="https://docs.cloudfoundry.org/devguide/deploy-apps/manifest-attributes.html#variable-substitution">Add Variables to a Manifest</a>)
* @return the resolved manifest
*/
public static ManifestV3 read(Path path, Path variablesPath) {
Map<String, String> variables =
deserialize(variablesPath.toAbsolutePath()).entrySet().stream()
.collect(toMap(Map.Entry::getKey, e -> String.valueOf(e.getValue())));
return doRead(path.toAbsolutePath(), variables);
}
/**
* Write a {@link ManifestV3} to a {@link Path}
*
* @param path the path to write to
* @param manifest the manifest to write
*/
public static void write(Path path, ManifestV3 manifest) {
try (OutputStream out =
Files.newOutputStream(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
write(out, manifest);
} catch (IOException e) {
throw Exceptions.propagate(e);
}
}
/**
* Write a {@link ManifestV3} to an {@link OutputStream}
*
* @param out the {@link OutputStream} to write to
* @param manifest the manifest to write
*/
public static void write(OutputStream out, ManifestV3 manifest) {
try (Writer writer = new OutputStreamWriter(out)) {
YAML.dump(toYaml(manifest), writer);
} catch (IOException e) {
throw Exceptions.propagate(e);
}
}
@SuppressWarnings("unchecked")
private static ManifestV3 doRead(Path path, Map<String, String> variables) {
Map<String, Object> root = deserialize(path);
ManifestV3.Builder builder = ManifestV3.builder();
asInteger(root, "version", variables, builder::version);
ManifestV3Application template = getTemplate(path, root, variables);
Optional.ofNullable(root.get("applications"))
.map(value -> ((List<Map<String, Object>>) value).stream())
.orElseGet(Stream::empty)
.map(
application -> {
String name = getName(application);
return toApplicationManifest(
application,
variables,
ManifestV3Application.builder().from(template),
path)
.name(name)
.build();
})
.forEach(builder::application);
return builder.build();
}
private static ManifestV3Application getTemplate(
Path path, Map<String, Object> root, Map<String, String> variables) {
return toApplicationManifest(root, variables, ManifestV3Application.builder(), path)
.name("template")
.build();
}
@SuppressWarnings("unchecked")
private static ManifestV3Application.Builder toApplicationManifest(
Map<String, Object> application,
Map<String, String> variables,
ManifestV3Application.Builder builder,
Path root) {
toApplicationManifestCommon(application, variables, builder, root);
asList(
application,
"processes",
variables,
raw -> getProcess((Map<String, Object>) raw, variables),
builder::processe);
asList(
application,
"services",
variables,
raw -> getService(raw, variables),
builder::service);
asList(
application,
"sidecars",
variables,
raw -> getSidecar((Map<String, Object>) raw, variables),
builder::sidecar);
as(
application,
"metadata",
variables,
raw -> getMetadata((Map<String, Object>) raw, variables),
builder::metadata);
asBoolean(application, "default-route", variables, builder::defaultRoute);
return builder;
}
private static ManifestV3Sidecar getSidecar(
Map<String, Object> raw, Map<String, String> variables) {
ManifestV3Sidecar.Builder builder = ManifestV3Sidecar.builder();
asString(raw, "name", variables, builder::name);
asString(raw, "command", variables, builder::command);
asListOfString(raw, "process_types", variables, builder::processType);
asInteger(raw, "memory", variables, builder::memory);
return builder.build();
}
private static ManifestV3Process getProcess(
Map<String, Object> raw, Map<String, String> variables) {
ManifestV3Process.Builder builder = ManifestV3Process.builder();
asString(raw, "type", variables, builder::type);
asString(raw, "command", variables, builder::command);
asString(raw, "disk_quota", variables, builder::disk);
asString(raw, "health-check-http-endpoint", variables, builder::healthCheckHttpEndpoint);
asInteger(
raw,
"health-check-invocation-timeout",
variables,
builder::healthCheckInvocationTimeout);
as(
raw,
"health-check-type",
variables,
s -> HealthCheckType.from((String) s),
builder::healthCheckType);
asString(
raw,
"readiness-health-check-http-endpoint",
variables,
builder::readinessHealthCheckHttpEndpoint);
asInteger(
raw,
"readiness-health-check-invocation-timeout",
variables,
builder::readinessHealthCheckInvocationTimeout);
asInteger(
raw,
"readiness-health-check-interval",
variables,
builder::readinessHealthCheckInterval);
as(
raw,
"readiness-health-check-type",
variables,
s -> ReadinessHealthCheckType.from((String) s),
builder::readinessHealthCheckType);
asInteger(raw, "instances", variables, builder::instances);
asString(raw, "memory", variables, builder::memory);
asInteger(raw, "timeout", variables, builder::timeout);
return builder.build();
}
private static ManifestV3Service getService(Object raw, Map<String, String> variables) {
ManifestV3Service.Builder builder = ManifestV3Service.builder();
if (raw instanceof String) {
builder.name((String) raw);
} else if (raw instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) raw;
asString(map, "name", variables, builder::name);
asString(map, "bindingName", variables, builder::bindingName);
builder.parameters((map).get("parameters"));
}
return builder.build();
}
private static Metadata getMetadata(Map<String, Object> raw, Map<String, String> variables) {
if (raw == null) return null;
Map<String, String> labels = new HashMap<>();
Map<String, String> annotations = new HashMap<>();
asMap(raw, "labels", variables, String.class::cast, labels::put);
asMap(raw, "annotations", variables, String.class::cast, annotations::put);
if (labels.isEmpty() && annotations.isEmpty()) {
return null;
}
Metadata.Builder builder = Metadata.builder();
if (!labels.isEmpty()) {
builder.labels(labels);
}
if (!annotations.isEmpty()) {
builder.annotations(annotations);
}
return builder.build();
}
private static Map<String, Object> toYaml(ManifestV3 manifest) {
Map<String, Object> yaml = new TreeMap<>();
yaml.put("version", manifest.getVersion());
yaml.put(
"applications",
convertCollection(
manifest.getApplications(), ApplicationManifestUtilsV3::toApplicationYaml));
return yaml;
}
private static Map<String, Object> toApplicationYaml(ManifestV3Application application) {
Map<String, Object> yaml = ApplicationManifestUtilsCommon.toApplicationYaml(application);
putIfPresent(
yaml,
"processes",
convertCollection(
application.getProcesses(), ApplicationManifestUtilsV3::toProcessYaml));
putIfPresent(yaml, "default-route", application.getDefaultRoute());
putIfPresent(
yaml,
"services",
convertCollection(
application.getServices(), ApplicationManifestUtilsV3::toServiceYaml));
putIfPresent(
yaml,
"sidecars",
convertCollection(
application.getSidecars(), ApplicationManifestUtilsV3::toSidecarsYaml));
putIfPresent(yaml, "metadata", toMetadataYaml(application.getMetadata()));
return yaml;
}
private static Map<String, Object> toSidecarsYaml(ManifestV3Sidecar sidecar) {
if (sidecar == null) return null;
Map<String, Object> yaml = new TreeMap<>();
putIfPresent(yaml, "name", sidecar.getName());
putIfPresent(yaml, "command", sidecar.getCommand());
putIfPresent(yaml, "process_types", sidecar.getProcessTypes());
putIfPresent(yaml, "memory", sidecar.getMemory());
return yaml;
}
private static Map<String, Object> toServiceYaml(ManifestV3Service service) {
if (service == null) return null;
Map<String, Object> yaml = new TreeMap<>();
putIfPresent(yaml, "name", service.getName());
putIfPresent(yaml, "binding_name", service.getBindingName());
putIfPresent(yaml, "parameters", service.getParameters());
return yaml;
}
private static Map<String, Object> toProcessYaml(ManifestV3Process process) {
if (process == null) return null;
Map<String, Object> yaml = new TreeMap<>();
putIfPresent(yaml, "type", process.getType());
putIfPresent(yaml, "command", process.getCommand());
putIfPresent(yaml, "disk_quota", process.getDisk());
putIfPresent(yaml, "health-check-http-endpoint", process.getHealthCheckHttpEndpoint());
putIfPresent(
yaml, "health-check-invocation-timeout", process.getHealthCheckInvocationTimeout());
putIfPresent(
yaml, "health-check-type", process.getHealthCheckType(), HealthCheckType::getValue);
putIfPresent(
yaml,
"readiness-health-check-type",
process.getReadinessHealthCheckType(),
ReadinessHealthCheckType::getValue);
putIfPresent(
yaml,
"readiness-health-check-http-endpoint",
process.getReadinessHealthCheckHttpEndpoint());
putIfPresent(
yaml,
"readiness-health-check-invocation-timeout",
process.getReadinessHealthCheckInvocationTimeout());
putIfPresent(
yaml, "readiness-health-check-interval", process.getReadinessHealthCheckInterval());
putIfPresent(yaml, "instances", process.getInstances());
putIfPresent(yaml, "memory", process.getMemory());
putIfPresent(yaml, "timeout", process.getTimeout());
return yaml;
}
private static Map<String, Object> toMetadataYaml(Metadata metadata) {
if (metadata == null) return null;
Map<String, Object> map = new HashMap<>();
putIfPresent(map, "labels", metadata.getLabels());
putIfPresent(map, "annotations", metadata.getAnnotations());
return map.isEmpty() ? null : map;
}
}