Skip to content

Commit 10bd28b

Browse files
committed
Use Preconditions
1 parent a2d5223 commit 10bd28b

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

v2/spanner-common/src/main/java/com/google/cloud/teleport/v2/spanner/migrations/utils/DataflowWorkerMachineTypeValidator.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,62 +15,62 @@
1515
*/
1616
package com.google.cloud.teleport.v2.spanner.migrations.utils;
1717

18+
import com.google.common.base.Preconditions;
19+
import org.apache.commons.lang3.StringUtils;
20+
1821
public class DataflowWorkerMachineTypeValidator {
1922

2023
public static void validateMachineSpecs(String workerMachineType, Integer minCPUs) {
21-
if (workerMachineType == null || workerMachineType.trim().isEmpty()) {
22-
throw new IllegalArgumentException(
23-
String.format(
24-
"Policy Violation: You must specify a workerMachineType with at least %d vCPUs.",
25-
minCPUs));
26-
}
24+
Preconditions.checkArgument(
25+
workerMachineType != null && !StringUtils.isBlank(workerMachineType),
26+
"Policy Violation: You must specify a workerMachineType with at least %s vCPUs.",
27+
minCPUs);
2728

2829
// Handle custom machine types first, format is custom-{vCPU}-{RAM}
2930
if (workerMachineType.startsWith("custom-")) {
3031
String[] parts = workerMachineType.split("-");
31-
if (parts.length != 3) {
32-
throw new IllegalArgumentException(
33-
String.format(
34-
"Invalid custom machine type format: '%s'. Expected format: custom-{vCPU}-{RAM}.",
35-
workerMachineType));
36-
}
32+
Preconditions.checkArgument(
33+
parts.length == 3,
34+
"Invalid custom machine type format: '%s'. Expected format: custom-{vCPU}-{RAM}.",
35+
workerMachineType);
36+
Integer vCpus = null;
3737
try {
38-
int vCpus = Integer.parseInt(parts[1]);
39-
if (vCpus < minCPUs) {
40-
throw new IllegalArgumentException(
41-
String.format(
42-
"Policy Violation: Custom machine type '%s' has %d vCPUs. Minimum allowed is %d. Please use a higher machine type.",
43-
workerMachineType, vCpus, minCPUs));
44-
}
38+
vCpus = Integer.parseInt(parts[1]);
4539
} catch (NumberFormatException e) {
46-
throw new IllegalArgumentException(
47-
String.format("Invalid vCPU number in custom machine type: '%s'", workerMachineType),
48-
e);
40+
Preconditions.checkArgument(
41+
false, "Invalid vCPU number in custom machine type: '%s'", workerMachineType);
4942
}
43+
Preconditions.checkArgument(
44+
vCpus >= minCPUs,
45+
"Policy Violation: Custom machine type '%s' has %s vCPUs. Minimum allowed is %s. Please use a higher machine type.",
46+
workerMachineType,
47+
vCpus,
48+
minCPUs);
5049
} else {
5150
// Handle standard machine types.
5251
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(".*-(\\d+)$");
5352
java.util.regex.Matcher matcher = pattern.matcher(workerMachineType);
5453

5554
if (matcher.find()) {
55+
Integer vCpus = null;
5656
try {
57-
int vCpus = Integer.parseInt(matcher.group(1));
58-
if (vCpus < minCPUs) {
59-
throw new IllegalArgumentException(
60-
String.format(
61-
"Policy Violation: Machine type '%s' has %d vCPUs. Minimum allowed is %d.",
62-
workerMachineType, vCpus, minCPUs));
63-
}
57+
vCpus = Integer.parseInt(matcher.group(1));
6458
} catch (NumberFormatException e) {
65-
// This should be rare given the regex, but good practice to handle.
66-
throw new IllegalArgumentException(
67-
String.format("Invalid vCPU number in machine type: '%s'", workerMachineType), e);
59+
Preconditions.checkArgument(
60+
false, "Invalid vCPU number in machine type: '%s'", workerMachineType);
6861
}
62+
Preconditions.checkArgument(
63+
vCpus >= minCPUs,
64+
"Policy Violation: Machine type '%s' has %s vCPUs. Minimum allowed is %s.",
65+
workerMachineType,
66+
vCpus,
67+
minCPUs);
6968
} else {
70-
throw new IllegalArgumentException(
71-
String.format(
72-
"Unknown machine type format: '%s'. Please use a standard machine type (e.g., n1-standard-4) or a custom machine type (e.g., custom-4-4096) with at least %d vCPUs.",
73-
workerMachineType, minCPUs));
69+
Preconditions.checkArgument(
70+
false,
71+
"Unknown machine type format: '%s'. Please use a standard machine type (e.g., n1-standard-4) or a custom machine type (e.g., custom-4-4096) with at least %s vCPUs.",
72+
workerMachineType,
73+
minCPUs);
7474
}
7575
}
7676
}

0 commit comments

Comments
 (0)