Skip to content

Commit f04f6cb

Browse files
authored
[Feature] DatabricksConfig: Add clone() support (#376)
## Changes Adds support for cloning DatabricksConfig(), this is needed because we need a way to set configurations per API call such as 1. timeout 2. httpClient configuration 3. debugHeaders However, we still want to use the cached oauth token etc and we would like the header factory to be a common object across workspace clients and databricks configs Ideally this should be supported by the SDK itself natively, however since its not supported and it will take significant migration to achieve that, this is a work around to achieve the same. ## Tests Added UT
1 parent a1460c5 commit f04f6cb

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksConfig.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,25 @@ public DatabricksEnvironment getDatabricksEnvironment() {
631631
return DatabricksEnvironment.getEnvironmentFromHostname(this.host);
632632
}
633633

634+
private DatabricksConfig clone(Set<String> fieldsToSkip) {
635+
DatabricksConfig newConfig = new DatabricksConfig();
636+
for (Field f : DatabricksConfig.class.getDeclaredFields()) {
637+
if (fieldsToSkip.contains(f.getName())) {
638+
continue;
639+
}
640+
try {
641+
f.set(newConfig, f.get(this));
642+
} catch (IllegalAccessException e) {
643+
throw new RuntimeException(e);
644+
}
645+
}
646+
return newConfig;
647+
}
648+
649+
public DatabricksConfig clone() {
650+
return clone(new HashSet<>());
651+
}
652+
634653
public DatabricksConfig newWithWorkspaceHost(String host) {
635654
Set<String> fieldsToSkip =
636655
new HashSet<>(
@@ -645,18 +664,6 @@ public DatabricksConfig newWithWorkspaceHost(String host) {
645664
// don't cache the
646665
// header factory.
647666
"headerFactory"));
648-
DatabricksConfig newConfig = new DatabricksConfig();
649-
for (Field f : DatabricksConfig.class.getDeclaredFields()) {
650-
if (fieldsToSkip.contains(f.getName())) {
651-
continue;
652-
}
653-
try {
654-
f.set(newConfig, f.get(this));
655-
} catch (IllegalAccessException e) {
656-
throw new RuntimeException(e);
657-
}
658-
}
659-
newConfig.setHost(host);
660-
return newConfig;
667+
return clone(fieldsToSkip).setHost(host);
661668
}
662669
}

databricks-sdk-java/src/test/java/com/databricks/sdk/core/DatabricksConfigTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,22 @@ public void testNewWithWorkspaceHost() {
177177
assert newWorkspaceConfig.getClientId().equals("my-client-id");
178178
assert newWorkspaceConfig.getClientSecret().equals("my-client-secret");
179179
}
180+
181+
@Test
182+
public void testClone() {
183+
DatabricksConfig config =
184+
new DatabricksConfig()
185+
.setAuthType("oauth-m2m")
186+
.setClientId("my-client-id")
187+
.setClientSecret("my-client-secret")
188+
.setAccountId("account-id")
189+
.setHost("https://account.cloud.databricks.com");
190+
191+
DatabricksConfig newWorkspaceConfig = config.clone();
192+
193+
assert newWorkspaceConfig.getHost().equals("https://account.cloud.databricks.com");
194+
assert newWorkspaceConfig.getAuthType().equals("oauth-m2m");
195+
assert newWorkspaceConfig.getClientId().equals("my-client-id");
196+
assert newWorkspaceConfig.getClientSecret().equals("my-client-secret");
197+
}
180198
}

0 commit comments

Comments
 (0)