fix option sources (#12387)

This commit is contained in:
Charles Gagnon
2020-09-16 23:00:50 -07:00
committed by GitHub
parent be1e0b3c8d
commit fca8b85a72
4 changed files with 18 additions and 27 deletions

View File

@@ -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<OptionsSourceType, new () => 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<string>;
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<string, string>();
constructor() {
super();
}
async getOptions(): Promise<string[] | CategoryValue[]> {
const controllers = await apiService.arcApi.getRegisteredDataControllers();
@@ -97,4 +82,3 @@ export class ArcControllersOptionsSource extends OptionsSource {
}
}
}
OptionsSources.set(<OptionsSourceType>ArcControllersOptionsSource.name, ArcControllersOptionsSource);

View File

@@ -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
}

View File

@@ -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);

View File

@@ -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<void> {
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) {