Skip to content

Commit

Permalink
Sign in to Cloud (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrmarti committed Nov 5, 2018
1 parent ae6f03e commit 00fd3af
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"activationEvents": [
"onCommand:azure-account.login",
"onCommand:azure-account.logout",
"onCommand:azure-account.loginToCloud",
"onCommand:azure-account.selectSubscriptions",
"onCommand:azure-account.createAccount",
"onCommand:azure-account.openCloudConsoleLinux",
Expand All @@ -46,6 +47,11 @@
"title": "%azure-account.commands.logout%",
"category": "%azure-account.commands.azure%"
},
{
"command": "azure-account.loginToCloud",
"title": "%azure-account.commands.loginToCloud%",
"category": "%azure-account.commands.azure%"
},
{
"command": "azure-account.selectSubscriptions",
"title": "%azure-account.commands.selectSubscriptions%",
Expand Down Expand Up @@ -106,6 +112,11 @@
"type": "string",
"default": "",
"description": "A specific tenant to sign in to. The default is to sign in to the common tenant and use all known tenants."
},
"azure.cloud": {
"type": "string",
"default": "Azure",
"description": "The current Azure Cloud to connect to."
}
}
}
Expand Down Expand Up @@ -147,4 +158,4 @@
"vscode-nls": "2.0.2",
"ws": "3.1.0"
}
}
}
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"azure-account.commands.azure": "Azure",
"azure-account.commands.login": "Sign In",
"azure-account.commands.logout": "Sign Out",
"azure-account.commands.loginToCloud": "Sign In to Azure Cloud",
"azure-account.commands.selectSubscriptions": "Select Subscriptions",
"azure-account.commands.createAccount": "Create an Account",
"azure-account.commands.openCloudConsoleLinux": "Open Bash in Cloud Shell",
Expand Down
52 changes: 38 additions & 14 deletions src/azure-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,22 @@ export class AzureLoginHelper {
private tokenCache = new MemoryCache();
private delayedCache = new ProxyTokenCache(this.tokenCache);
private oldResourceFilter = '';
private doLogin = false;

constructor(private context: ExtensionContext, private reporter: TelemetryReporter) {
const subscriptions = context.subscriptions;
subscriptions.push(commands.registerCommand('azure-account.login', () => this.login().catch(console.error)));
subscriptions.push(commands.registerCommand('azure-account.logout', () => this.logout().catch(console.error)));
subscriptions.push(commands.registerCommand('azure-account.loginToCloud', () => this.loginToCloud().catch(console.error)));
subscriptions.push(commands.registerCommand('azure-account.askForLogin', () => this.askForLogin().catch(console.error)));
subscriptions.push(commands.registerCommand('azure-account.selectSubscriptions', () => this.selectSubscriptions().catch(console.error)));
subscriptions.push(this.api.onSessionsChanged(() => this.updateSubscriptions().catch(console.error)));
subscriptions.push(this.api.onSubscriptionsChanged(() => this.updateFilters()));
subscriptions.push(workspace.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('azure.environment') || e.affectsConfiguration('azure.tenant')) {
this.initialize()
if (e.affectsConfiguration('azure.cloud') || e.affectsConfiguration('azure.tenant')) {
const doLogin = this.doLogin;
this.doLogin = false;
this.initialize(doLogin)
.catch(console.error);
} else if (e.affectsConfiguration('azure.resourceFilter')) {
this.updateFilters(true);
Expand Down Expand Up @@ -269,7 +273,20 @@ export class AzureLoginHelper {
this.updateStatus();
}

private async initialize() {
async loginToCloud(): Promise<any>{
const selected = await window.showQuickPick(getEnvironmentList(), {
placeHolder: localize('azure-account.chooseCloudToLogin', "Choose cloud to sign in to")
});
if (selected) {
const config = workspace.getConfiguration('azure');
if (config.get('cloud') !== selected) {
this.doLogin = true;
config.update('cloud', selected, getCurrentTarget(config.inspect('cloud')));
}
}
}

private async initialize(doLogin?: boolean) {
try {
const timing = false;
const start = Date.now();
Expand Down Expand Up @@ -298,6 +315,9 @@ export class AzureLoginHelper {
if (!(err instanceof AzureLoginError)) {
throw err;
}
if (doLogin) {
this.login();
}
} finally {
this.updateStatus();
}
Expand Down Expand Up @@ -506,16 +526,7 @@ export class AzureLoginHelper {

private async updateConfiguration(azureConfig: WorkspaceConfiguration, resourceFilter: string[]) {
const resourceFilterConfig = azureConfig.inspect<string[]>('resourceFilter');
let target = ConfigurationTarget.Global;
if (resourceFilterConfig) {
if (resourceFilterConfig.workspaceFolderValue) {
target = ConfigurationTarget.WorkspaceFolder;
} else if (resourceFilterConfig.workspaceValue) {
target = ConfigurationTarget.Workspace;
} else if (resourceFilterConfig.globalValue) {
target = ConfigurationTarget.Global;
}
}
const target = getCurrentTarget(resourceFilterConfig);
await azureConfig.update('resourceFilter', resourceFilter[0] !== 'all' ? resourceFilter : undefined, target);
}

Expand Down Expand Up @@ -594,7 +605,7 @@ function getCredentialsService(environment: AzureEnvironment) {

function getSelectedEnvironment(): AzureEnvironment {
const envConfig = workspace.getConfiguration('azure');
const envSetting = envConfig.get<string>('environment') || 'Azure';
const envSetting = envConfig.get<string>('cloud') || 'Azure';
return getEnvironment(envSetting);
}

Expand Down Expand Up @@ -716,6 +727,19 @@ export async function listAll<T>(client: { listNext(nextPageLink: string): Promi
return all;
}

function getCurrentTarget(config: { key: string; defaultValue?: unknown; globalValue?: unknown; workspaceValue?: unknown, workspaceFolderValue?: unknown } | undefined) {
if (config) {
if (config.workspaceFolderValue) {
return ConfigurationTarget.WorkspaceFolder;
} else if (config.workspaceValue) {
return ConfigurationTarget.Workspace;
} else if (config.globalValue) {
return ConfigurationTarget.Global;
}
}
return ConfigurationTarget.Global;
}

async function exitCode(command: string, ...args: string[]) {
return new Promise<number | undefined>(resolve => {
cp.spawn(command, args)
Expand Down

0 comments on commit 00fd3af

Please sign in to comment.