UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

191 lines (189 loc) 8.48 kB
import { concat, Observable } from 'rxjs'; import { EnvironmentModule, EnvironmentModuleEntryPointType } from '../manifest/environment-modules'; import { ToolConditionValidator } from './tool-condition-validator'; /** * Tool Connection query class. * @dynamic */ export class ToolConnectionQuery { /** * Create an observable to determine the collection of tools to be applicable to the connection. * * @param appContext the application context. * @param caches the share inventory query caches. * @param connection the connection object. * @param solution the entry point object of the solution. */ static queryToolsList(appContext, caches, connection, solution) { return ToolConnectionQuery.queryEntryPointList(appContext, caches, connection, solution, [EnvironmentModuleEntryPointType.Tool]); } /** * Create an observable to determine the collection of settings to be applicable to the connection. * * @param appContext the application context. * @param caches the share inventory query caches. * @param connection the connection object. * @param solution the entry point object of the solution. */ static querySettingsFormsList(appContext, caches, connection, solution) { return ToolConnectionQuery.queryEntryPointList(appContext, caches, connection, solution, [EnvironmentModuleEntryPointType.SettingsForm]); } /** * Create an observable to determine the collection of entry points to be applicable to the connection. * * @param appContext the application context. * @param caches the share inventory query caches. * @param connection the connection object. * @param solution the entry point object of the solution. * @param entryPoints the list of entry points. */ static validateEntryPoints(appContext, caches, connection, solution, entryPoints) { return ToolConnectionQuery.validateEntryPointList(appContext, caches, connection, solution, entryPoints); } /** * gets the list of tools available to the solution and connection * @param connection the connection * @param solution the solution */ static getToolsList(connection, solution) { const tools = EnvironmentModule.getEntryPointsByType([EnvironmentModuleEntryPointType.Tool]) .filter(tool => ToolConnectionQuery.checkToolRequirements(connection, solution, tool)); // TODO, explore if we should cache this list return tools; } /** * get the list of settings forms available to the solution and connection * @param connection the connection * @param solution the solution */ static getSettingsFormList(connection, solution) { const settings = EnvironmentModule.getEntryPointsByType([EnvironmentModuleEntryPointType.SettingsForm]) .filter(setting => ToolConnectionQuery.checkToolRequirements(connection, solution, setting)); // TODO, explore if we should cache this list return settings; } /** * query for the list of a particular type of entry point * @param appContext the app context * @param caches the inventory query caches * @param connection the connection * @param solution the solution entry point. * @param entryPointTypes the type list of entry point we want to query. */ static queryEntryPointList(appContext, caches, connection, solution, entryPointTypes) { const observablesCollection = []; const tools = []; const endpoint = !connection ? null : appContext.authorizationManager.getJeaEndpoint(connection.name); EnvironmentModule.getEntryPointsByType(entryPointTypes) .filter(entryPoint => !endpoint || entryPoint.parentModule.jea) .map(tool => { const item = ToolConditionValidator.current(appContext, caches) .scanToolCondition(connection, solution, tool); if (item.weight === 0 /* ToolConditionWeight.NonObservable */) { tools.push(item.result); } else if (observablesCollection[item.weight]) { observablesCollection[item.weight].push(item.result); } else { observablesCollection[item.weight] = [item.result]; } }); const observables = []; for (let weight = 1 /* ToolConditionWeight.Property */; weight <= 4 /* ToolConditionWeight.Script */; weight++) { if (observablesCollection[weight]) { observablesCollection[weight].forEach(item => observables.push(item)); } } const observable = new Observable((observer) => { // respond tools that have no conditional observable. observer.next(tools); // process observable sequentially by using concat. const subscription = concat(...observables).subscribe({ next: data => { data.connectionId = connection && connection.id; tools.push(data); observer.next(tools); }, error: error => observer.error(error), complete: () => { observer.complete(); } }); return () => subscription.unsubscribe(); }); return observable; } /** * query for the list of a particular type of entry point * @param appContext the app context * @param caches the inventory query caches * @param connection the connection * @param solution the solution entry point. * @param entryPointTypes the type list of entry point we want to query. */ static validateEntryPointList(appContext, caches, connection, solution, entryPoints) { const observablesCollection = []; const tools = []; entryPoints .map(tool => { const item = ToolConditionValidator.current(appContext, caches).scanToolCondition(connection, solution, tool); if (item.weight === 0 /* ToolConditionWeight.NonObservable */) { tools.push(item.result); } else if (observablesCollection[item.weight]) { observablesCollection[item.weight].push(item.result); } else { observablesCollection[item.weight] = [item.result]; } }); const observables = []; for (let weight = 1 /* ToolConditionWeight.Property */; weight <= 4 /* ToolConditionWeight.Script */; weight++) { if (observablesCollection[weight]) { observablesCollection[weight].forEach(item => observables.push(item)); } } const observable = new Observable((observer) => { if (observables.length === 0) { observer.next(tools); observer.complete(); return; } // process observable sequentially by using concat, and aggregate to the single tools[] array. const subscription = concat(...observables).subscribe({ next: data => { data.connectionId = connection.id; tools.push(data); }, error: error => observer.error(error), complete: () => { observer.next(tools); observer.complete(); } }); return () => subscription.unsubscribe(); }); return observable; } static checkToolRequirements(connection, solution, tool) { if (!tool.requirements) { // tool is missing requirements, never show. return false; } const solutionId = EnvironmentModule.createFormattedEntrypoint(solution); // process each tool requirement. for (let i = 0; i < tool.requirements.length; i++) { const req = tool.requirements[i]; if (req.solutionIds && ((!connection && !req.connectionTypes) || (connection && req.connectionTypes && req.solutionIds.some(s => s === solutionId) && req.connectionTypes.some(c => c === connection.type)))) { return true; } } return false; } } //# sourceMappingURL=tool-connection-query.js.map