From fca8b85a72e41afd485981ebf0303c4021c18dbc Mon Sep 17 00:00:00 2001 From: Charles Gagnon Date: Wed, 16 Sep 2020 23:00:50 -0700 Subject: [PATCH] fix option sources (#12387) --- .../src/helpers/optionSources.ts | 24 ++++--------------- .../resource-deployment/src/interfaces.ts | 4 ++-- .../src/localizedConstants.ts | 2 +- .../src/ui/modelViewUtils.ts | 15 ++++++++---- 4 files changed, 18 insertions(+), 27 deletions(-) diff --git a/extensions/resource-deployment/src/helpers/optionSources.ts b/extensions/resource-deployment/src/helpers/optionSources.ts index 379316295a..4584e8eb46 100644 --- a/extensions/resource-deployment/src/helpers/optionSources.ts +++ b/extensions/resource-deployment/src/helpers/optionSources.ts @@ -11,15 +11,12 @@ import { apiService } from '../services/apiService'; import { throwUnless } from '../utils'; import { CacheManager } from './cacheManager'; +export enum OptionsSourceType { + ArcControllersOptionsSource = 'ArcControllersOptionsSource' +} -export type OptionsSourceType = 'ArcControllersOptionsSource'; - -const OptionsSources = new Map OptionsSource>(); export abstract class OptionsSource implements IOptionsSource { - private _variableNames!: { [index: string]: string; }; - private _type!: OptionsSourceType; - get type(): OptionsSourceType { return this._type; } get variableNames(): { [index: string]: string; } { return this._variableNames; } @@ -27,24 +24,12 @@ export abstract class OptionsSource implements IOptionsSource { abstract async getVariableValue(variableName: string, input: string): Promise; abstract getIsPassword(variableName: string): boolean; - protected constructor() { - } - - static construct(optionsSourceType: OptionsSourceType, variableNames: { [index: string]: string }): OptionsSource { - const sourceConstructor = OptionsSources.get(optionsSourceType); - throwUnless(sourceConstructor !== undefined, loc.noOptionsSourceDefined(optionsSourceType)); - const obj = new sourceConstructor(); - obj._type = optionsSourceType; - obj._variableNames = variableNames; - return obj; + constructor(private _variableNames: { [index: string]: string }, private _type: OptionsSourceType) { } } export class ArcControllersOptionsSource extends OptionsSource { private _cacheManager = new CacheManager(); - constructor() { - super(); - } async getOptions(): Promise { const controllers = await apiService.arcApi.getRegisteredDataControllers(); @@ -97,4 +82,3 @@ export class ArcControllersOptionsSource extends OptionsSource { } } } -OptionsSources.set(ArcControllersOptionsSource.name, ArcControllersOptionsSource); diff --git a/extensions/resource-deployment/src/interfaces.ts b/extensions/resource-deployment/src/interfaces.ts index a25de03b9c..8b0225eacc 100644 --- a/extensions/resource-deployment/src/interfaces.ts +++ b/extensions/resource-deployment/src/interfaces.ts @@ -5,7 +5,7 @@ import * as azdata from 'azdata'; import * as vscode from 'vscode'; -import { OptionsSource, OptionsSourceType } from './helpers/optionSources'; +import { OptionsSourceType } from './helpers/optionSources'; export const NoteBookEnvironmentVariablePrefix = 'AZDATA_NB_VAR_'; @@ -196,7 +196,7 @@ export interface IOptionsSource { export interface OptionsInfo { values?: string[] | azdata.CategoryValue[], - source?: OptionsSource, + source?: IOptionsSource, defaultValue: string, optionsType?: OptionsType } diff --git a/extensions/resource-deployment/src/localizedConstants.ts b/extensions/resource-deployment/src/localizedConstants.ts index cac629652b..d739592394 100644 --- a/extensions/resource-deployment/src/localizedConstants.ts +++ b/extensions/resource-deployment/src/localizedConstants.ts @@ -27,7 +27,7 @@ export const unknownFieldTypeError = (type: FieldType) => localize('UnknownField export const variableValueFetchForUnsupportedVariable = (variableName: string) => localize('getVariableValue.unknownVariableName', "Attempt to get variable value for unknown variable:{0}", variableName); export const isPasswordFetchForUnsupportedVariable = (variableName: string) => localize('getIsPassword.unknownVariableName', "Attempt to get isPassword for unknown variable:{0}", variableName); export const noControllersConnected = localize('noControllersConnected', "No Azure ARC controllers are currently connected. Please run the command: 'Connect to Existing Azure Arc Controller' and then try again"); -export const noOptionsSourceDefined = (optionsSourceType: OptionsSourceType) => localize('noOptionsSourceDefined', "No OptionsSource defined for type: {0}", optionsSourceType); +export const noOptionsSourceDefined = (optionsSourceType: string) => localize('noOptionsSourceDefined', "No OptionsSource defined for type: {0}", optionsSourceType); export const noControllerInfoFound = (name: string) => localize('noControllerInfoFound', "controllerInfo could not be found with name: {0}", name); export const noPasswordFound = (controllerName: string) => localize('noPasswordFound', "Password could not be retrieved for controller: {0} and user did not provide a password. Please retry later.", controllerName); export const optionsNotDefined = (fieldType: FieldType) => localize('optionsNotDefined', "FieldInfo.options was not defined for field type: {0}", fieldType); diff --git a/extensions/resource-deployment/src/ui/modelViewUtils.ts b/extensions/resource-deployment/src/ui/modelViewUtils.ts index 8bd05ccaa3..885a31b26e 100644 --- a/extensions/resource-deployment/src/ui/modelViewUtils.ts +++ b/extensions/resource-deployment/src/ui/modelViewUtils.ts @@ -9,8 +9,8 @@ import { EOL, homedir as os_homedir } from 'os'; import * as path from 'path'; import * as vscode from 'vscode'; import * as nls from 'vscode-nls'; -import { OptionsSource } from '../helpers/optionSources'; -import { AzureAccountFieldInfo, AzureLocationsFieldInfo, ComponentCSSStyles, DialogInfoBase, FieldInfo, FieldType, FilePickerFieldInfo, KubeClusterContextFieldInfo, LabelPosition, NoteBookEnvironmentVariablePrefix, OptionsInfo, OptionsType, PageInfoBase, RowInfo, SectionInfo, TextCSSStyles } from '../interfaces'; +import { ArcControllersOptionsSource, OptionsSourceType } from '../helpers/optionSources'; +import { AzureAccountFieldInfo, AzureLocationsFieldInfo, ComponentCSSStyles, DialogInfoBase, FieldInfo, FieldType, FilePickerFieldInfo, IOptionsSource, KubeClusterContextFieldInfo, LabelPosition, NoteBookEnvironmentVariablePrefix, OptionsInfo, OptionsType, PageInfoBase, RowInfo, SectionInfo, TextCSSStyles } from '../interfaces'; import * as loc from '../localizedConstants'; import { apiService } from '../services/apiService'; import { getDefaultKubeConfigPath, getKubeConfigClusterContexts } from '../services/kubeService'; @@ -419,8 +419,15 @@ async function processOptionsTypeField(context: FieldContext): Promise { throwUnless('optionsType' in context.fieldInfo.options, loc.optionsTypeNotFound); if (context.fieldInfo.options.source) { try { - // if options.source still points to the IOptionsSource interface make it to point to the implementation - context.fieldInfo.options.source = OptionsSource.construct(context.fieldInfo.options.source.type, context.fieldInfo.options.source.variableNames); + let optionsSource: IOptionsSource; + switch (context.fieldInfo.options.source.type) { + case OptionsSourceType.ArcControllersOptionsSource: + optionsSource = new ArcControllersOptionsSource(context.fieldInfo.options.source.variableNames, context.fieldInfo.options.source.type); + break; + default: + throw new Error(loc.noOptionsSourceDefined(context.fieldInfo.options.source.type)); + } + context.fieldInfo.options.source = optionsSource; context.fieldInfo.options.values = await context.fieldInfo.options.source.getOptions(); } catch (e) {