Skip to content

Commit

Permalink
[Feature] DatabricksConfig: Add clone() support
Browse files Browse the repository at this point in the history
  • Loading branch information
satviksr-db committed Nov 2, 2024
1 parent a1460c5 commit e14a2ed
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,17 @@ public DatabricksConfig setAzureUseMsi(boolean azureUseMsi) {
return this;
}

/** @deprecated Use {@link #getAzureUseMsi()} instead. */
/**
* @deprecated Use {@link #getAzureUseMsi()} instead.
*/
@Deprecated()
public boolean getAzureUseMSI() {
return azureUseMsi;
}

/** @deprecated Use {@link #setAzureUseMsi(boolean)} instead. */
/**
* @deprecated Use {@link #setAzureUseMsi(boolean)} instead.
*/
@Deprecated
public DatabricksConfig setAzureUseMSI(boolean azureUseMsi) {
this.azureUseMsi = azureUseMsi;
Expand Down Expand Up @@ -631,6 +635,25 @@ public DatabricksEnvironment getDatabricksEnvironment() {
return DatabricksEnvironment.getEnvironmentFromHostname(this.host);
}

private DatabricksConfig clone(Set<String> fieldsToSkip) {
DatabricksConfig newConfig = new DatabricksConfig();
for (Field f : DatabricksConfig.class.getDeclaredFields()) {
if (fieldsToSkip.contains(f.getName())) {
continue;
}
try {
f.set(newConfig, f.get(this));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
return newConfig;
}

public DatabricksConfig clone() {
return clone(new HashSet<>());
}

public DatabricksConfig newWithWorkspaceHost(String host) {
Set<String> fieldsToSkip =
new HashSet<>(
Expand All @@ -645,18 +668,6 @@ public DatabricksConfig newWithWorkspaceHost(String host) {
// don't cache the
// header factory.
"headerFactory"));
DatabricksConfig newConfig = new DatabricksConfig();
for (Field f : DatabricksConfig.class.getDeclaredFields()) {
if (fieldsToSkip.contains(f.getName())) {
continue;
}
try {
f.set(newConfig, f.get(this));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
newConfig.setHost(host);
return newConfig;
return clone(fieldsToSkip).setHost(host);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,22 @@ public void testNewWithWorkspaceHost() {
assert newWorkspaceConfig.getClientId().equals("my-client-id");
assert newWorkspaceConfig.getClientSecret().equals("my-client-secret");
}

@Test
public void testClone() {
DatabricksConfig config =
new DatabricksConfig()
.setAuthType("oauth-m2m")
.setClientId("my-client-id")
.setClientSecret("my-client-secret")
.setAccountId("account-id")
.setHost("https://account.cloud.databricks.com");

DatabricksConfig newWorkspaceConfig = config.clone();

assert newWorkspaceConfig.getHost().equals("https://account.cloud.databricks.com");
assert newWorkspaceConfig.getAuthType().equals("oauth-m2m");
assert newWorkspaceConfig.getClientId().equals("my-client-id");
assert newWorkspaceConfig.getClientSecret().equals("my-client-secret");
}
}

0 comments on commit e14a2ed

Please sign in to comment.