mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-17 02:51:36 -05:00
Azure provider cleanup and add rg property (#13030)
* Move Azure DataGrid Provider into own class * Fix compile
This commit is contained in:
78
extensions/azurecore/src/azureDataGridProvider.ts
Normal file
78
extensions/azurecore/src/azureDataGridProvider.ts
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
/*---------------------------------------------------------------------------------------------
|
||||||
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||||
|
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||||
|
*--------------------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
import * as azdata from 'azdata';
|
||||||
|
import { AppContext } from './appContext';
|
||||||
|
import { AzureResourceServiceNames } from './azureResource/constants';
|
||||||
|
import { IAzureResourceSubscriptionService } from './azureResource/interfaces';
|
||||||
|
import { TokenCredentials } from '@azure/ms-rest-js';
|
||||||
|
import { azureResource } from 'azureResource';
|
||||||
|
import * as azureResourceUtils from './azureResource/utils';
|
||||||
|
import * as constants from './constants';
|
||||||
|
import * as loc from './localizedConstants';
|
||||||
|
import * as utils from './utils';
|
||||||
|
|
||||||
|
const typesClause = [
|
||||||
|
azureResource.AzureResourceType.sqlDatabase,
|
||||||
|
azureResource.AzureResourceType.sqlServer,
|
||||||
|
azureResource.AzureResourceType.sqlManagedInstance,
|
||||||
|
azureResource.AzureResourceType.postgresServer,
|
||||||
|
azureResource.AzureResourceType.azureArcService,
|
||||||
|
azureResource.AzureResourceType.azureArcSqlManagedInstance,
|
||||||
|
azureResource.AzureResourceType.azureArcPostgresServer
|
||||||
|
].map(type => `type == "${type}"`).join(' or ');
|
||||||
|
|
||||||
|
export class AzureDataGridProvider implements azdata.DataGridProvider {
|
||||||
|
constructor(private _appContext: AppContext) { }
|
||||||
|
|
||||||
|
public providerId = constants.dataGridProviderId;
|
||||||
|
public async getDataGridItems() {
|
||||||
|
const accounts = await azdata.accounts.getAllAccounts();
|
||||||
|
const items: any[] = [];
|
||||||
|
await Promise.all(accounts.map(async (account) => {
|
||||||
|
await Promise.all(account.properties.tenants.map(async (tenant: { id: string; }) => {
|
||||||
|
try {
|
||||||
|
const tokenResponse = await azdata.accounts.getAccountSecurityToken(account, tenant.id, azdata.AzureResource.ResourceManagement);
|
||||||
|
const token = tokenResponse.token;
|
||||||
|
const tokenType = tokenResponse.tokenType;
|
||||||
|
const credential = new TokenCredentials(token, tokenType);
|
||||||
|
const subscriptionService = this._appContext.getService<IAzureResourceSubscriptionService>(AzureResourceServiceNames.subscriptionService);
|
||||||
|
const subscriptions = await subscriptionService.getSubscriptions(account, credential, tenant.id);
|
||||||
|
try {
|
||||||
|
const newItems = (await azureResourceUtils.runResourceQuery(account, subscriptions, true, `where ${typesClause}`)).resources
|
||||||
|
.map(item => {
|
||||||
|
return <azdata.DataGridItem>{
|
||||||
|
id: item.id,
|
||||||
|
// nameLink: <azdata.DataGridHyperlinkInfo>{ displayText: item.name, linkOrCommand: ''},
|
||||||
|
resourceGroup: item.resourceGroup,
|
||||||
|
subscriptionName: subscriptions.find(subscription => subscription.id === item.subscriptionId)?.name ?? item.subscriptionId,
|
||||||
|
locationDisplayName: utils.getRegionDisplayName(item.location),
|
||||||
|
typeDisplayName: utils.getResourceTypeDisplayName(item.type),
|
||||||
|
iconPath: utils.getResourceTypeIcon(this._appContext, item.type)
|
||||||
|
};
|
||||||
|
});
|
||||||
|
items.push(...newItems);
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}));
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getDataGridColumns(): Promise<azdata.DataGridColumn[]> {
|
||||||
|
return [
|
||||||
|
{ id: 'icon', type: 'image', field: 'iconPath', name: '', width: 25, sortable: false, filterable: false, resizable: false, tooltip: loc.typeIcon },
|
||||||
|
{ id: 'name', type: 'hyperlink', field: 'nameLink', name: loc.name, width: 150 },
|
||||||
|
{ id: 'type', type: 'text', field: 'typeDisplayName', name: loc.resourceType, width: 150 },
|
||||||
|
{ id: 'type', type: 'text', field: 'resourceGroup', name: loc.resourceGroup, width: 150 },
|
||||||
|
{ id: 'location', type: 'text', field: 'locationDisplayName', name: loc.location, width: 150 },
|
||||||
|
{ id: 'subscriptionId', type: 'text', field: 'subscriptionName', name: loc.subscription, width: 150 }
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,6 +39,7 @@ declare module 'azureResource' {
|
|||||||
name: string;
|
name: string;
|
||||||
id: string;
|
id: string;
|
||||||
subscriptionId: string;
|
subscriptionId: string;
|
||||||
|
resourceGroup?: string;
|
||||||
tenant?: string;
|
tenant?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,8 @@ export class AzureResourceDatabaseService implements IAzureResourceService<azure
|
|||||||
serverFullName: server.properties.fullyQualifiedDomainName,
|
serverFullName: server.properties.fullyQualifiedDomainName,
|
||||||
loginName: server.properties.administratorLogin,
|
loginName: server.properties.administratorLogin,
|
||||||
subscriptionId: db.subscriptionId,
|
subscriptionId: db.subscriptionId,
|
||||||
tenant: db.tenantId
|
tenant: db.tenantId,
|
||||||
|
resourceGroup: db.resourceGroup
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,8 @@ export class AzureResourceDatabaseServerService extends ResourceServiceBase<DbSe
|
|||||||
loginName: resource.properties.administratorLogin,
|
loginName: resource.properties.administratorLogin,
|
||||||
defaultDatabaseName: 'master',
|
defaultDatabaseName: 'master',
|
||||||
subscriptionId: resource.subscriptionId,
|
subscriptionId: resource.subscriptionId,
|
||||||
tenant: resource.tenantId
|
tenant: resource.tenantId,
|
||||||
|
resourceGroup: resource.resourceGroup
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,8 @@ export class KustoResourceService extends ResourceServiceBase<KustoGraphData, az
|
|||||||
loginName: '',
|
loginName: '',
|
||||||
defaultDatabaseName: '',
|
defaultDatabaseName: '',
|
||||||
subscriptionId: resource.subscriptionId,
|
subscriptionId: resource.subscriptionId,
|
||||||
tenant: resource.tenantId
|
tenant: resource.tenantId,
|
||||||
|
resourceGroup: resource.resourceGroup
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,8 @@ export class PostgresServerArcService extends ResourceServiceBase<PostgresArcSer
|
|||||||
loginName: resource.properties.admin,
|
loginName: resource.properties.admin,
|
||||||
defaultDatabaseName: 'postgres',
|
defaultDatabaseName: 'postgres',
|
||||||
subscriptionId: resource.subscriptionId,
|
subscriptionId: resource.subscriptionId,
|
||||||
tenant: resource.tenantId
|
tenant: resource.tenantId,
|
||||||
|
resourceGroup: resource.resourceGroup
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,8 @@ export class PostgresServerService extends ResourceServiceBase<DbServerGraphData
|
|||||||
loginName: resource.properties.administratorLogin,
|
loginName: resource.properties.administratorLogin,
|
||||||
defaultDatabaseName: 'postgres',
|
defaultDatabaseName: 'postgres',
|
||||||
subscriptionId: resource.subscriptionId,
|
subscriptionId: resource.subscriptionId,
|
||||||
tenant: resource.tenantId
|
tenant: resource.tenantId,
|
||||||
|
resourceGroup: resource.resourceGroup
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ export class SqlInstanceResourceService extends ResourceServiceBase<SqlInstanceG
|
|||||||
loginName: resource.properties.administratorLogin,
|
loginName: resource.properties.administratorLogin,
|
||||||
defaultDatabaseName: 'master',
|
defaultDatabaseName: 'master',
|
||||||
subscriptionId: resource.subscriptionId,
|
subscriptionId: resource.subscriptionId,
|
||||||
tenant: resource.tenantId
|
tenant: resource.tenantId,
|
||||||
|
resourceGroup: resource.resourceGroup
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,8 @@ export class SqlInstanceArcResourceService extends ResourceServiceBase<SqlInstan
|
|||||||
loginName: resource.properties.admin,
|
loginName: resource.properties.admin,
|
||||||
defaultDatabaseName: 'master',
|
defaultDatabaseName: 'master',
|
||||||
subscriptionId: resource.subscriptionId,
|
subscriptionId: resource.subscriptionId,
|
||||||
tenant: resource.tenantId
|
tenant: resource.tenantId,
|
||||||
|
resourceGroup: resource.resourceGroup
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,8 +43,8 @@ import * as loc from './localizedConstants';
|
|||||||
import * as constants from './constants';
|
import * as constants from './constants';
|
||||||
import { AzureResourceGroupService } from './azureResource/providers/resourceGroup/resourceGroupService';
|
import { AzureResourceGroupService } from './azureResource/providers/resourceGroup/resourceGroupService';
|
||||||
import { Logger } from './utils/Logger';
|
import { Logger } from './utils/Logger';
|
||||||
import { TokenCredentials } from '@azure/ms-rest-js';
|
|
||||||
import { FlatAzureResourceTreeProvider } from './azureResource/tree/flatTreeProvider';
|
import { FlatAzureResourceTreeProvider } from './azureResource/tree/flatTreeProvider';
|
||||||
|
import { AzureDataGridProvider } from './azureDataGridProvider';
|
||||||
|
|
||||||
let extensionContext: vscode.ExtensionContext;
|
let extensionContext: vscode.ExtensionContext;
|
||||||
|
|
||||||
@@ -90,63 +90,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<azurec
|
|||||||
pushDisposable(vscode.window.registerTreeDataProvider('azureResourceExplorer', azureResourceTree));
|
pushDisposable(vscode.window.registerTreeDataProvider('azureResourceExplorer', azureResourceTree));
|
||||||
pushDisposable(vscode.workspace.onDidChangeConfiguration(e => onDidChangeConfiguration(e), this));
|
pushDisposable(vscode.workspace.onDidChangeConfiguration(e => onDidChangeConfiguration(e), this));
|
||||||
registerAzureResourceCommands(appContext, azureResourceTree);
|
registerAzureResourceCommands(appContext, azureResourceTree);
|
||||||
|
azdata.dataprotocol.registerDataGridProvider(new AzureDataGridProvider(appContext));
|
||||||
const typesClause = [
|
|
||||||
azureResource.AzureResourceType.sqlDatabase,
|
|
||||||
azureResource.AzureResourceType.sqlServer,
|
|
||||||
azureResource.AzureResourceType.sqlManagedInstance,
|
|
||||||
azureResource.AzureResourceType.postgresServer,
|
|
||||||
azureResource.AzureResourceType.azureArcService,
|
|
||||||
azureResource.AzureResourceType.azureArcSqlManagedInstance,
|
|
||||||
azureResource.AzureResourceType.azureArcPostgresServer
|
|
||||||
].map(type => `type == "${type}"`).join(' or ');
|
|
||||||
azdata.dataprotocol.registerDataGridProvider({
|
|
||||||
providerId: constants.dataGridProviderId,
|
|
||||||
getDataGridItems: async () => {
|
|
||||||
const accounts = await azdata.accounts.getAllAccounts();
|
|
||||||
const items: any[] = [];
|
|
||||||
await Promise.all(accounts.map(async (account) => {
|
|
||||||
await Promise.all(account.properties.tenants.map(async (tenant: { id: string; }) => {
|
|
||||||
try {
|
|
||||||
const tokenResponse = await azdata.accounts.getAccountSecurityToken(account, tenant.id, azdata.AzureResource.ResourceManagement);
|
|
||||||
const token = tokenResponse.token;
|
|
||||||
const tokenType = tokenResponse.tokenType;
|
|
||||||
const credential = new TokenCredentials(token, tokenType);
|
|
||||||
const subscriptionService = appContext.getService<IAzureResourceSubscriptionService>(AzureResourceServiceNames.subscriptionService);
|
|
||||||
const subscriptions = await subscriptionService.getSubscriptions(account, credential, tenant.id);
|
|
||||||
try {
|
|
||||||
const newItems = (await azureResourceUtils.runResourceQuery(account, subscriptions, true, `where ${typesClause}`)).resources
|
|
||||||
.map(item => {
|
|
||||||
return {
|
|
||||||
...item,
|
|
||||||
subscriptionName: subscriptions.find(subscription => subscription.id === item.subscriptionId)?.name ?? item.subscriptionId,
|
|
||||||
locationDisplayName: utils.getRegionDisplayName(item.location),
|
|
||||||
typeDisplayName: utils.getResourceTypeDisplayName(item.type),
|
|
||||||
iconPath: utils.getResourceTypeIcon(appContext, item.type)
|
|
||||||
};
|
|
||||||
});
|
|
||||||
items.push(...newItems);
|
|
||||||
} catch (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
}));
|
|
||||||
return items;
|
|
||||||
},
|
|
||||||
getDataGridColumns: async () => {
|
|
||||||
return [
|
|
||||||
{ id: 'icon', type: 'image', field: 'iconPath', name: '', width: 25, sortable: false, filterable: false, resizable: false, tooltip: loc.typeIcon },
|
|
||||||
{ id: 'name', type: 'text', field: 'name', name: loc.name, width: 150 },
|
|
||||||
{ id: 'type', type: 'text', field: 'typeDisplayName', name: loc.resourceType, width: 150 },
|
|
||||||
{ id: 'type', type: 'text', field: 'resourceGroup', name: loc.resourceGroup, width: 150 },
|
|
||||||
{ id: 'location', type: 'text', field: 'locationDisplayName', name: loc.location, width: 150 },
|
|
||||||
{ id: 'subscriptionId', type: 'text', field: 'subscriptionName', name: loc.subscription, width: 150 }
|
|
||||||
];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getSubscriptions(account?: azdata.Account, ignoreErrors?: boolean, selectedOnly: boolean = false): Thenable<azurecore.GetSubscriptionsResult> {
|
getSubscriptions(account?: azdata.Account, ignoreErrors?: boolean, selectedOnly: boolean = false): Thenable<azurecore.GetSubscriptionsResult> {
|
||||||
|
|||||||
@@ -76,7 +76,8 @@ const mockDatabases: azureResource.AzureResourceDatabase[] = [
|
|||||||
serverName: 'mock database server 1',
|
serverName: 'mock database server 1',
|
||||||
serverFullName: 'mock database server full name 1',
|
serverFullName: 'mock database server full name 1',
|
||||||
loginName: 'mock login',
|
loginName: 'mock login',
|
||||||
subscriptionId: 'mock_subscription'
|
subscriptionId: 'mock_subscription',
|
||||||
|
resourceGroup: 'rg1'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'mock database 2',
|
name: 'mock database 2',
|
||||||
@@ -84,7 +85,8 @@ const mockDatabases: azureResource.AzureResourceDatabase[] = [
|
|||||||
serverName: 'mock database server 2',
|
serverName: 'mock database server 2',
|
||||||
serverFullName: 'mock database server full name 2',
|
serverFullName: 'mock database server full name 2',
|
||||||
loginName: 'mock login',
|
loginName: 'mock login',
|
||||||
subscriptionId: 'mock_subscription'
|
subscriptionId: 'mock_subscription',
|
||||||
|
resourceGroup: 'rg2'
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -75,7 +75,8 @@ const mockDatabaseServers: azureResource.AzureResourceDatabaseServer[] = [
|
|||||||
fullName: 'mock database server full name 1',
|
fullName: 'mock database server full name 1',
|
||||||
loginName: 'mock login',
|
loginName: 'mock login',
|
||||||
defaultDatabaseName: 'master',
|
defaultDatabaseName: 'master',
|
||||||
subscriptionId: 'mock_subscription'
|
subscriptionId: 'mock_subscription',
|
||||||
|
resourceGroup: 'rg1'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'mock database server 2',
|
name: 'mock database server 2',
|
||||||
@@ -83,7 +84,8 @@ const mockDatabaseServers: azureResource.AzureResourceDatabaseServer[] = [
|
|||||||
fullName: 'mock database server full name 2',
|
fullName: 'mock database server full name 2',
|
||||||
loginName: 'mock login',
|
loginName: 'mock login',
|
||||||
defaultDatabaseName: 'master',
|
defaultDatabaseName: 'master',
|
||||||
subscriptionId: 'mock_subscription'
|
subscriptionId: 'mock_subscription',
|
||||||
|
resourceGroup: 'rg2'
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user