Skip to content

Commit 29e854d

Browse files
feat: introduce validateFields method to SourceConnectionConfig and implement validation for all source configurations
1 parent 9992792 commit 29e854d

7 files changed

Lines changed: 87 additions & 6 deletions

File tree

v2/spanner-common/src/main/java/com/google/cloud/teleport/v2/spanner/migrations/shard/CassandraShard.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public CassandraShard(OptionsMap optionsMap) {
3030
extractAndSetHostAndPort();
3131
}
3232

33-
private void validateFields() {
33+
@Override
34+
public void validateFields() {
3435
if (getContactPoints() == null || getContactPoints().isEmpty()) {
3536
throw new IllegalArgumentException("CONTACT_POINTS cannot be null or empty.");
3637
}

v2/spanner-common/src/main/java/com/google/cloud/teleport/v2/spanner/migrations/shard/Shard.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,37 @@ public int hashCode() {
197197
secretManagerUri,
198198
dbNameToLogicalShardIdMap);
199199
}
200+
201+
public void validateFields() {
202+
if (logicalShardId == null) {
203+
throw new IllegalArgumentException("logicalShardId cannot be null");
204+
}
205+
if (host == null) {
206+
throw new IllegalArgumentException("host cannot be null");
207+
}
208+
if (port == null) {
209+
throw new IllegalArgumentException("port cannot be null");
210+
}
211+
if (user == null) {
212+
throw new IllegalArgumentException("user cannot be null");
213+
}
214+
if (password == null) {
215+
throw new IllegalArgumentException("password cannot be null");
216+
}
217+
if (dbName == null) {
218+
throw new IllegalArgumentException("dbName cannot be null");
219+
}
220+
if (namespace == null) {
221+
throw new IllegalArgumentException("namespace cannot be null");
222+
}
223+
if (secretManagerUri == null) {
224+
throw new IllegalArgumentException("secretManagerUri cannot be null");
225+
}
226+
if (connectionProperties == null) {
227+
throw new IllegalArgumentException("connectionProperties cannot be null");
228+
}
229+
if (dbNameToLogicalShardIdMap == null) {
230+
throw new IllegalArgumentException("dbNameToLogicalShardIdMap cannot be null");
231+
}
232+
}
200233
}

v2/spanner-common/src/main/java/com/google/cloud/teleport/v2/spanner/migrations/source/config/AstraConnectionConfig.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,20 @@ public String getAstraDbRegion() {
5454
public void setAstraDbRegion(String astraDbRegion) {
5555
this.astraDbRegion = astraDbRegion;
5656
}
57+
58+
@Override
59+
public void validateFields() {
60+
if (databaseId == null) {
61+
throw new IllegalArgumentException("databaseId cannot be null");
62+
}
63+
if (astraToken == null) {
64+
throw new IllegalArgumentException("astraToken cannot be null");
65+
}
66+
if (keySpace == null) {
67+
throw new IllegalArgumentException("keySpace cannot be null");
68+
}
69+
if (astraDbRegion == null) {
70+
throw new IllegalArgumentException("astraDbRegion cannot be null");
71+
}
72+
}
5773
}

v2/spanner-common/src/main/java/com/google/cloud/teleport/v2/spanner/migrations/source/config/CassandraConnectionConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@ public CassandraConnectionConfig(OptionsMap optionsMap) {
2929
public OptionsMap getOptionsMap() {
3030
return optionsMap;
3131
}
32+
33+
@Override
34+
public void validateFields() {
35+
if (optionsMap == null) {
36+
throw new IllegalArgumentException("optionsMap cannot be null");
37+
}
38+
}
3239
}

v2/spanner-common/src/main/java/com/google/cloud/teleport/v2/spanner/migrations/source/config/JdbcShardConfig.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,14 @@ public List<Shard> getShardConfigs() {
3333
public void setShardConfigs(List<Shard> shardConfigs) {
3434
this.shardConfigs = shardConfigs;
3535
}
36+
37+
@Override
38+
public void validateFields() {
39+
if (shardConfigs == null || shardConfigs.isEmpty()) {
40+
throw new IllegalArgumentException("shardConfigs cannot be null or empty");
41+
}
42+
for (Shard shard : shardConfigs) {
43+
shard.validateFields();
44+
}
45+
}
3646
}

v2/spanner-common/src/main/java/com/google/cloud/teleport/v2/spanner/migrations/source/config/SourceConfigParser.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,34 @@ public SourceConnectionConfig parseConfiguration(
6969
String sourceTypeStr, String sourceConfigFilePath) throws Exception {
7070

7171
SourceType sourceType = SourceType.parseSourceType(sourceTypeStr);
72-
switch (SourceType.parseSourceType(sourceTypeStr)) {
72+
SourceConnectionConfig sourceConfig;
73+
switch (sourceType) {
7374
case CASSANDRA:
7475
// Maps directly to the DataStax OptionsMap
75-
return new CassandraConnectionConfig(
76-
CassandraDriverConfigLoader.getOptionsMapFromFile(sourceConfigFilePath));
76+
sourceConfig =
77+
new CassandraConnectionConfig(
78+
CassandraDriverConfigLoader.getOptionsMapFromFile(sourceConfigFilePath));
79+
break;
7780
case ASTRA_DB:
7881
String astraFileContent = FileLoader.readConfigFilePath(sourceConfigFilePath);
7982
Map<String, Object> astraConfigMap = parseConfigToConfigMap(astraFileContent);
80-
return mapper.convertValue(astraConfigMap, AstraConnectionConfig.class);
83+
sourceConfig = mapper.convertValue(astraConfigMap, AstraConnectionConfig.class);
84+
break;
8185
case MYSQL:
8286
case PG:
8387
String jdbcFileContent = FileLoader.readConfigFilePath(sourceConfigFilePath);
8488
Map<String, Object> jdbcConfigMap = parseConfigToConfigMap(jdbcFileContent);
8589
JdbcShardConfig jdbcShardConfig = mapper.convertValue(jdbcConfigMap, JdbcShardConfig.class);
90+
jdbcShardConfig.validateFields();
8691
// Returns ordered list of shards
8792
jdbcShardConfig.getShardConfigs().sort(Comparator.comparing(Shard::getLogicalShardId));
8893
resolveShardSecret(jdbcShardConfig, sourceConfigFilePath);
8994
return jdbcShardConfig;
9095
default:
9196
throw new IllegalArgumentException("Unsupported source type: " + sourceType);
9297
}
98+
sourceConfig.validateFields();
99+
return sourceConfig;
93100
}
94101

95102
/**

v2/spanner-common/src/main/java/com/google/cloud/teleport/v2/spanner/migrations/source/config/SourceConnectionConfig.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@
2323
*
2424
* <p>This interface will be expanded in Phase 2 during the platformization phase.
2525
*/
26-
public interface SourceConnectionConfig {}
26+
public interface SourceConnectionConfig {
27+
28+
/**
29+
* Validates that the source configuration is valid. Throws IllegalArgumentException if the
30+
* configuration is invalid.
31+
*/
32+
void validateFields();
33+
}

0 commit comments

Comments
 (0)