Skip to content

Commit

Permalink
Improve login wizard (#1045)
Browse files Browse the repository at this point in the history
Ensure we can let users select host manually when they want to setup new
auth for bundle init
  • Loading branch information
ilia-db authored Feb 7, 2024
1 parent 0f85446 commit 25f964f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
18 changes: 8 additions & 10 deletions packages/databricks-vscode/src/bundle/BundleInitWizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {Loggers} from "../logger";
import {AuthProvider} from "../configuration/auth/AuthProvider";
import {LoginWizard} from "../configuration/LoginWizard";
import {CliWrapper} from "../cli/CliWrapper";
import {ConfigModel} from "../configuration/models/ConfigModel";
import {getSubProjects} from "./BundleFileSet";
import {tmpdir} from "os";

Expand Down Expand Up @@ -47,13 +46,10 @@ export class BundleInitWizard {

public async initNewProject(
workspaceUri?: Uri,
existingAuthProvider?: AuthProvider,
configModel?: ConfigModel
existingAuthProvider?: AuthProvider
) {
const authProvider = await this.configureAuthForBundleInit(
existingAuthProvider,
configModel
);
const authProvider =
await this.configureAuthForBundleInit(existingAuthProvider);
if (!authProvider) {
this.logger.debug(
"No valid auth providers, can't proceed with bundle init wizard"
Expand Down Expand Up @@ -91,8 +87,7 @@ export class BundleInitWizard {
}

private async configureAuthForBundleInit(
authProvider?: AuthProvider,
configModel?: ConfigModel
authProvider?: AuthProvider
): Promise<AuthProvider | undefined> {
if (authProvider) {
const response = await this.promptToUseExistingAuth(authProvider);
Expand All @@ -103,7 +98,7 @@ export class BundleInitWizard {
}
}
if (!authProvider) {
authProvider = await LoginWizard.run(this.cli, configModel);
authProvider = await LoginWizard.run(this.cli);
}
if (authProvider && (await authProvider.check())) {
return authProvider;
Expand Down Expand Up @@ -147,6 +142,9 @@ export class BundleInitWizard {
isTransient: true,
location: TerminalLocation.Editor,
env: this.cli.getBundleInitEnvVars(authProvider),
// Without strict env we will inherit our environmentVariableCollection
// which will override auth env vars we provide in this call.
strictEnv: true,
// Setting CWD avoids a possibility of the CLI picking up unrelated bundle configuration
// in the current workspace root or while traversing up the folder structure.
cwd: tmpdir(),
Expand Down
5 changes: 2 additions & 3 deletions packages/databricks-vscode/src/bundle/BundleProjectManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export class BundleProjectManager {
},
})
public async startManualMigration() {
const authProvider = await LoginWizard.run(this.cli, this.configModel);
const authProvider = await LoginWizard.run(this.cli);
if (
authProvider instanceof ProfileAuthProvider &&
(await authProvider.check())
Expand Down Expand Up @@ -275,8 +275,7 @@ export class BundleProjectManager {
this.connectionManager.databricksWorkspace?.authProvider;
const parentFolder = await bundleInitWizard.initNewProject(
this.workspaceUri,
authProvider,
this.configModel
authProvider
);
if (parentFolder) {
await this.isBundleProjectCache.refresh();
Expand Down
4 changes: 2 additions & 2 deletions packages/databricks-vscode/src/cli/CliWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export class CliWrapper {
}

getBundleInitEnvVars(authProvider: AuthProvider) {
return {
return removeUndefinedKeys({
...EnvVarGenerators.getEnvVarsForCli(
workspaceConfigs.databrickscfgLocation
),
Expand All @@ -312,7 +312,7 @@ export class CliWrapper {
...authProvider.toEnv(),
// eslint-disable-next-line @typescript-eslint/naming-convention
DATABRICKS_OUTPUT_FORMAT: "text",
};
});
}

async bundleInit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,11 @@ export class ConnectionManager implements Disposable {
popup: {prefix: "Can't configure workspace. "},
})
async configureLogin() {
const authProvider = await LoginWizard.run(this.cli, this.configModel);
const authProvider = await LoginWizard.run(
this.cli,
this.configModel.target,
await this.configModel.get("host")
);
if (!authProvider) {
return;
}
Expand Down
16 changes: 8 additions & 8 deletions packages/databricks-vscode/src/configuration/LoginWizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
loadConfigFile,
AuthType as SdkAuthType,
} from "@databricks/databricks-sdk";
import {ConfigModel} from "./models/ConfigModel";
import {randomUUID} from "crypto";
import ini from "ini";
import {copyFile, writeFile} from "fs/promises";
Expand Down Expand Up @@ -55,7 +54,7 @@ export class LoginWizard {
}
constructor(
private readonly cliWrapper: CliWrapper,
private readonly configModel?: ConfigModel
private readonly target?: string
) {}

private async inputHost(input: MultiStepInput) {
Expand Down Expand Up @@ -253,13 +252,13 @@ export class LoginWizard {
input: MultiStepInput,
pick: AuthTypeQuickPickItem
) {
let initialValue = this.configModel?.target ?? "";
let initialValue = this.target ?? "";

// If the initialValue profile already exists, then create a unique name.
const profiles = await this.getProfiles();
if (profiles.find((profile) => profile.name === initialValue)) {
const suffix = randomUUID().slice(0, 8);
initialValue = `${this.configModel?.target ?? "dev"}-${suffix}`;
initialValue = `${this.target ?? "dev"}-${suffix}`;
}

const profileName = await input.showInputBox({
Expand Down Expand Up @@ -342,11 +341,12 @@ export class LoginWizard {

static async run(
cliWrapper: CliWrapper,
configModel?: ConfigModel
target?: string,
host?: URL
): Promise<AuthProvider | undefined> {
const wizard = new LoginWizard(cliWrapper, configModel);
if (configModel) {
wizard.state.host = await configModel.get("host");
const wizard = new LoginWizard(cliWrapper, target);
if (host) {
wizard.state.host = host;
}
await MultiStepInput.run(wizard.inputHost.bind(wizard));
if (!wizard.state.host || !wizard.state.authProvider) {
Expand Down

0 comments on commit 25f964f

Please sign in to comment.