|
15 | 15 | */ |
16 | 16 | package com.google.cloud.teleport.v2.spanner.migrations.utils; |
17 | 17 |
|
| 18 | +import com.google.common.base.Preconditions; |
| 19 | +import org.apache.commons.lang3.StringUtils; |
| 20 | + |
18 | 21 | public class DataflowWorkerMachineTypeValidator { |
19 | 22 |
|
20 | 23 | 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); |
27 | 28 |
|
28 | 29 | // Handle custom machine types first, format is custom-{vCPU}-{RAM} |
29 | 30 | if (workerMachineType.startsWith("custom-")) { |
30 | 31 | 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; |
37 | 37 | 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]); |
45 | 39 | } 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); |
49 | 42 | } |
| 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); |
50 | 49 | } else { |
51 | 50 | // Handle standard machine types. |
52 | 51 | java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(".*-(\\d+)$"); |
53 | 52 | java.util.regex.Matcher matcher = pattern.matcher(workerMachineType); |
54 | 53 |
|
55 | 54 | if (matcher.find()) { |
| 55 | + Integer vCpus = null; |
56 | 56 | 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)); |
64 | 58 | } 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); |
68 | 61 | } |
| 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); |
69 | 68 | } 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); |
74 | 74 | } |
75 | 75 | } |
76 | 76 | } |
|
0 commit comments