mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-25 09:35:37 -05:00
Dev/brih/feature/switch ads to portal context (#18963)
* Add CodeQL Analysis workflow (#10195) * Add CodeQL Analysis workflow * Fix path * dashboard refactor * update version, readme, minor ui changes * fix merge issue * Revert "Add CodeQL Analysis workflow (#10195)" This reverts commit fe98d586cd75be4758ac544649bb4983accf4acd. * fix context switching issue * fix resource id parsing error and mi api version * mv refresh btn, rm autorefresh, align cards * remove missed autorefresh code * improve error handling and messages * fix typos * remove duplicate/unnecessary _populate* calls * change clear configuration button text * remove confusing watermark text * add stale account handling Co-authored-by: Justin Hutchings <jhutchings1@users.noreply.github.com>
This commit is contained in:
@@ -2,11 +2,13 @@
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
import * as vscode from 'vscode';
|
||||
import { azureResource } from 'azureResource';
|
||||
import { logError, sendSqlMigrationActionEvent, TelemetryAction, TelemetryViews } from '../telemtery';
|
||||
import { DatabaseMigration, SqlMigrationService, SqlManagedInstance, getMigrationStatus, AzureAsyncOperationResource, getMigrationAsyncOperationDetails, SqlVMServer, getSubscriptions } from '../api/azure';
|
||||
import * as azdata from 'azdata';
|
||||
import * as vscode from 'vscode';
|
||||
import * as azurecore from 'azurecore';
|
||||
import { azureResource } from 'azureResource';
|
||||
import { DatabaseMigration, SqlMigrationService, getSubscriptions, getServiceMigrations } from '../api/azure';
|
||||
import { deepClone } from '../api/utils';
|
||||
import * as loc from '../constants/strings';
|
||||
|
||||
export class MigrationLocalStorage {
|
||||
private static context: vscode.ExtensionContext;
|
||||
@@ -16,161 +18,75 @@ export class MigrationLocalStorage {
|
||||
MigrationLocalStorage.context = context;
|
||||
}
|
||||
|
||||
public static async getMigrationsBySourceConnections(connectionProfile: azdata.connection.ConnectionProfile, refreshStatus?: boolean): Promise<MigrationContext[]> {
|
||||
const undefinedSessionId = '{undefined}';
|
||||
const result: MigrationContext[] = [];
|
||||
const validMigrations: MigrationContext[] = [];
|
||||
const startTime = new Date().toString();
|
||||
// fetch saved migrations
|
||||
const migrationMementos: MigrationContext[] = this.context.globalState.get(this.mementoToken) || [];
|
||||
for (let i = 0; i < migrationMementos.length; i++) {
|
||||
const migration = migrationMementos[i];
|
||||
migration.migrationContext = this.removeMigrationSecrets(migration.migrationContext);
|
||||
migration.sessionId = migration.sessionId ?? undefinedSessionId;
|
||||
if (migration.sourceConnectionProfile.serverName === connectionProfile.serverName) {
|
||||
// refresh migration status
|
||||
if (refreshStatus) {
|
||||
try {
|
||||
await this.refreshMigrationAzureAccount(migration);
|
||||
|
||||
if (migration.asyncUrl) {
|
||||
migration.asyncOperationResult = await getMigrationAsyncOperationDetails(
|
||||
migration.azureAccount,
|
||||
migration.subscription,
|
||||
migration.asyncUrl,
|
||||
migration.sessionId!);
|
||||
}
|
||||
|
||||
migration.migrationContext = await getMigrationStatus(
|
||||
migration.azureAccount,
|
||||
migration.subscription,
|
||||
migration.migrationContext,
|
||||
migration.sessionId!);
|
||||
}
|
||||
catch (e) {
|
||||
// Keeping only valid migrations in cache. Clearing all the migrations which return ResourceDoesNotExit error.
|
||||
switch (e.message) {
|
||||
case 'ResourceDoesNotExist':
|
||||
case 'NullMigrationId':
|
||||
continue;
|
||||
default:
|
||||
logError(TelemetryViews.MigrationLocalStorage, 'MigrationBySourceConnectionError', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
result.push(migration);
|
||||
}
|
||||
validMigrations.push(migration);
|
||||
public static async getMigrationServiceContext(): Promise<MigrationServiceContext> {
|
||||
const connectionProfile = await azdata.connection.getCurrentConnection();
|
||||
if (connectionProfile) {
|
||||
const serverContextKey = `${this.mementoToken}.${connectionProfile.serverName}.serviceContext`;
|
||||
return deepClone(await this.context.globalState.get(serverContextKey)) || {};
|
||||
}
|
||||
|
||||
await this.context.globalState.update(this.mementoToken, validMigrations);
|
||||
|
||||
sendSqlMigrationActionEvent(
|
||||
TelemetryViews.MigrationLocalStorage,
|
||||
TelemetryAction.Done,
|
||||
{
|
||||
'startTime': startTime,
|
||||
'endTime': new Date().toString()
|
||||
},
|
||||
{
|
||||
'migrationCount': migrationMementos.length
|
||||
}
|
||||
);
|
||||
|
||||
// only save updated migration context
|
||||
if (refreshStatus) {
|
||||
const migrations: MigrationContext[] = this.context.globalState.get(this.mementoToken) || [];
|
||||
validMigrations.forEach(migration => {
|
||||
const idx = migrations.findIndex(m => m.migrationContext.id === migration.migrationContext.id);
|
||||
if (idx > -1) {
|
||||
migrations[idx] = migration;
|
||||
}
|
||||
});
|
||||
|
||||
// check global state for migrations count mismatch, avoid saving
|
||||
// state if the count has changed when a migration may have been added
|
||||
const current: MigrationContext[] = this.context.globalState.get(this.mementoToken) || [];
|
||||
if (current.length === migrations.length) {
|
||||
await this.context.globalState.update(this.mementoToken, migrations);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return {};
|
||||
}
|
||||
|
||||
public static async refreshMigrationAzureAccount(migration: MigrationContext): Promise<void> {
|
||||
if (migration.azureAccount.isStale) {
|
||||
public static async saveMigrationServiceContext(serviceContext: MigrationServiceContext): Promise<void> {
|
||||
const connectionProfile = await azdata.connection.getCurrentConnection();
|
||||
if (connectionProfile) {
|
||||
const serverContextKey = `${this.mementoToken}.${connectionProfile.serverName}.serviceContext`;
|
||||
return await this.context.globalState.update(serverContextKey, deepClone(serviceContext));
|
||||
}
|
||||
}
|
||||
|
||||
public static async refreshMigrationAzureAccount(serviceContext: MigrationServiceContext, migration: DatabaseMigration): Promise<void> {
|
||||
if (serviceContext.azureAccount?.isStale) {
|
||||
const accounts = await azdata.accounts.getAllAccounts();
|
||||
const account = accounts.find(a => !a.isStale && a.key.accountId === migration.azureAccount.key.accountId);
|
||||
const account = accounts.find(a => !a.isStale && a.key.accountId === serviceContext.azureAccount?.key.accountId);
|
||||
if (account) {
|
||||
const subscriptions = await getSubscriptions(account);
|
||||
const subscription = subscriptions.find(s => s.id === migration.subscription.id);
|
||||
const subscription = subscriptions.find(s => s.id === serviceContext.subscription?.id);
|
||||
if (subscription) {
|
||||
migration.azureAccount = account;
|
||||
serviceContext.azureAccount = account;
|
||||
await this.saveMigrationServiceContext(serviceContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async saveMigration(
|
||||
connectionProfile: azdata.connection.ConnectionProfile,
|
||||
migrationContext: DatabaseMigration,
|
||||
targetMI: SqlManagedInstance | SqlVMServer,
|
||||
azureAccount: azdata.Account,
|
||||
subscription: azureResource.AzureResourceSubscription,
|
||||
controller: SqlMigrationService,
|
||||
asyncURL: string,
|
||||
sessionId: string): Promise<void> {
|
||||
try {
|
||||
let migrationMementos: MigrationContext[] = this.context.globalState.get(this.mementoToken) || [];
|
||||
migrationMementos = migrationMementos.filter(m => m.migrationContext.id !== migrationContext.id);
|
||||
migrationMementos.push({
|
||||
sourceConnectionProfile: connectionProfile,
|
||||
migrationContext: this.removeMigrationSecrets(migrationContext),
|
||||
targetManagedInstance: targetMI,
|
||||
subscription: subscription,
|
||||
azureAccount: azureAccount,
|
||||
controller: controller,
|
||||
asyncUrl: asyncURL,
|
||||
sessionId: sessionId
|
||||
});
|
||||
await this.context.globalState.update(this.mementoToken, migrationMementos);
|
||||
} catch (e) {
|
||||
logError(TelemetryViews.MigrationLocalStorage, 'CantSaveMigration', e);
|
||||
}
|
||||
}
|
||||
|
||||
public static async clearMigrations(): Promise<void> {
|
||||
await this.context.globalState.update(this.mementoToken, ([] as MigrationContext[]));
|
||||
}
|
||||
|
||||
public static removeMigrationSecrets(migration: DatabaseMigration): DatabaseMigration {
|
||||
// remove secrets from migration context
|
||||
if (migration.properties.sourceSqlConnection?.password) {
|
||||
migration.properties.sourceSqlConnection.password = '';
|
||||
}
|
||||
if (migration.properties.backupConfiguration?.sourceLocation?.fileShare?.password) {
|
||||
migration.properties.backupConfiguration.sourceLocation.fileShare.password = '';
|
||||
}
|
||||
if (migration.properties.backupConfiguration?.sourceLocation?.azureBlob?.accountKey) {
|
||||
migration.properties.backupConfiguration.sourceLocation.azureBlob.accountKey = '';
|
||||
}
|
||||
if (migration.properties.backupConfiguration?.targetLocation?.accountKey) {
|
||||
migration.properties.backupConfiguration.targetLocation.accountKey = '';
|
||||
}
|
||||
return migration;
|
||||
}
|
||||
}
|
||||
|
||||
export interface MigrationContext {
|
||||
sourceConnectionProfile: azdata.connection.ConnectionProfile,
|
||||
migrationContext: DatabaseMigration,
|
||||
targetManagedInstance: SqlManagedInstance | SqlVMServer,
|
||||
azureAccount: azdata.Account,
|
||||
subscription: azureResource.AzureResourceSubscription,
|
||||
controller: SqlMigrationService,
|
||||
asyncUrl: string,
|
||||
asyncOperationResult?: AzureAsyncOperationResource,
|
||||
sessionId?: string
|
||||
export function isServiceContextValid(serviceContext: MigrationServiceContext): boolean {
|
||||
return (
|
||||
serviceContext.azureAccount?.isStale === false &&
|
||||
serviceContext.location?.id !== undefined &&
|
||||
serviceContext.migrationService?.id !== undefined &&
|
||||
serviceContext.resourceGroup?.id !== undefined &&
|
||||
serviceContext.subscription?.id !== undefined &&
|
||||
serviceContext.tenant?.id !== undefined
|
||||
);
|
||||
}
|
||||
|
||||
export async function getSelectedServiceStatus(): Promise<string> {
|
||||
const serviceContext = await MigrationLocalStorage.getMigrationServiceContext();
|
||||
const serviceName = serviceContext?.migrationService?.name;
|
||||
return serviceName && isServiceContextValid(serviceContext)
|
||||
? loc.MIGRATION_SERVICE_SERVICE_PROMPT(serviceName)
|
||||
: loc.MIGRATION_SERVICE_SELECT_SERVICE_PROMPT;
|
||||
}
|
||||
|
||||
export async function getCurrentMigrations(): Promise<DatabaseMigration[]> {
|
||||
const serviceContext = await MigrationLocalStorage.getMigrationServiceContext();
|
||||
return isServiceContextValid(serviceContext)
|
||||
? await getServiceMigrations(
|
||||
serviceContext.azureAccount!,
|
||||
serviceContext.subscription!,
|
||||
serviceContext.migrationService?.id!)
|
||||
: [];
|
||||
}
|
||||
|
||||
export interface MigrationServiceContext {
|
||||
azureAccount?: azdata.Account,
|
||||
tenant?: azurecore.Tenant,
|
||||
subscription?: azureResource.AzureResourceSubscription,
|
||||
location?: azureResource.AzureLocation,
|
||||
resourceGroup?: azureResource.AzureResourceResourceGroup,
|
||||
migrationService?: SqlMigrationService,
|
||||
}
|
||||
|
||||
export enum MigrationStatus {
|
||||
|
||||
Reference in New Issue
Block a user