Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide list of available resources types and apiVersions #1276

Merged
merged 5 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion extension.bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export * from "./src/language/IssueKind";
export * from "./src/language/LineColPos";
export * from "./src/language/ReferenceList";
export * from "./src/language/Span";
export { LanguageServerState, notifyTemplateGraphAvailable, stopArmLanguageServer } from "./src/languageclient/startArmLanguageServer";
export * from "./src/languageclient/getAvailableResourceTypesAndVersions";
export * from "./src/languageclient/startArmLanguageServer";
export * from './src/snippets/ISnippetManager';
export * from './src/snippets/SnippetManager';
export * from "./src/survey";
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.15.1-alpha",
"publisher": "msazurermtools",
"config": {
"ARM_LANGUAGE_SERVER_NUGET_VERSION": "3.0.0-preview.21158.1"
"ARM_LANGUAGE_SERVER_NUGET_VERSION": "3.0.0-preview.21206.10"
},
"categories": [
"Azure",
Expand Down
9 changes: 7 additions & 2 deletions src/AzureRMTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import { Issue, IssueSeverity } from "./language/Issue";
import * as Json from "./language/json/JSON";
import { ReferenceList } from "./language/ReferenceList";
import { Span } from "./language/Span";
import { notifyTemplateGraphAvailable, startArmLanguageServerInBackground } from "./languageclient/startArmLanguageServer";
import { getAvailableResourceTypesAndVersions } from './languageclient/getAvailableResourceTypesAndVersions';
import { notifyTemplateGraphAvailable, startArmLanguageServerInBackground, waitForLanguageServerAvailable } from "./languageclient/startArmLanguageServer";
import { showInsertionContext } from "./snippets/showInsertionContext";
import { SnippetManager } from "./snippets/SnippetManager";
import { survey } from "./survey";
Expand Down Expand Up @@ -1193,8 +1194,12 @@ export class AzureRMTools implements IProvideOpenedDocuments {
resourceCounts?: string;
} & TelemetryProperties = actionContext.telemetry.properties;

const resourceCounts: Histogram = deploymentTemplate.getResourceUsage();
await waitForLanguageServerAvailable();
const availableResourceTypesAndVersions = await getAvailableResourceTypesAndVersions(deploymentTemplate.schemaUri ?? '');
const [resourceCounts, invalidResourceCounts, invalidVersionCounts] = deploymentTemplate.getResourceUsage(availableResourceTypesAndVersions);
properties.resourceCounts = escapeNonPaths(this.histogramToTelemetryString(resourceCounts));
properties.invalidResources = escapeNonPaths(this.histogramToTelemetryString(invalidResourceCounts));
properties.invalidVersions = escapeNonPaths(this.histogramToTelemetryString(invalidVersionCounts));
});
}

Expand Down
43 changes: 41 additions & 2 deletions src/documents/templates/DeploymentTemplateDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as Json from "../../language/json/JSON";
import { ReferenceList } from "../../language/ReferenceList";
import { ContainsBehavior, Span } from "../../language/Span";
import { CachedValue } from '../../util/CachedValue';
import { CaseInsensitiveMap } from '../../util/CaseInsensitiveMap';
import { expectParameterDocumentOrUndefined } from '../../util/expectDocument';
import { Histogram } from '../../util/Histogram';
import { nonNullValue } from '../../util/nonNull';
Expand Down Expand Up @@ -409,18 +410,56 @@ export class DeploymentTemplateDoc extends DeploymentDocument {
/**
* Gets info about schema usage, useful for telemetry
*/
public getResourceUsage(): Histogram {
public getResourceUsage(
availableResourceTypesAndVersions: CaseInsensitiveMap<string, string[]>
): [Histogram, Histogram, Histogram] {
const resourceCounts = new Histogram();
const invalidResourceCounts = new Histogram();
const invalidVersionCounts = new Histogram();

// tslint:disable-next-line: strict-boolean-expressions
const apiProfileString = `(profile=${this.apiProfile || 'none'})`.toLowerCase();

// Collect all resources used
const resources: Json.ArrayValue | undefined = this.topLevelValue ? Json.asArrayValue(this.topLevelValue.getPropertyValue(templateKeys.resources)) : undefined;
if (resources) {
traverseResources(resources, undefined);

if (availableResourceTypesAndVersions.size > 0) {
for (const key of resourceCounts.keys) {
if (key) {
const count = resourceCounts.getCount(key);

let apiVersionsForType: string[] | undefined;
let apiVersion: string;
let resourceType: string;

const match = key.match(/([a-z./0-9-]+)@([a-z0-9-]+)/);
if (match) {
resourceType = match[1];
apiVersion = match[2];
apiVersionsForType = availableResourceTypesAndVersions.get(resourceType);
} else {
resourceType = key;
apiVersion = "";
}

if (apiVersionsForType) {
// The resource type is valid. Is the apiVersion valid?
if (!apiVersionsForType.includes(apiVersion)) {
// Invalid apiVersion
invalidVersionCounts.add(key, count);
}
} else {
// The resource type is not in the list
invalidResourceCounts.add(key, count);
}
}
}
}
}

return resourceCounts;
return [resourceCounts, invalidResourceCounts, invalidVersionCounts];

function traverseResources(resourcesObject: Json.ArrayValue, parentKey: string | undefined): void {
for (let resource of resourcesObject.elements) {
Expand Down
39 changes: 39 additions & 0 deletions src/languageclient/getAvailableResourceTypesAndVersions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// ---------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.md in the project root for license information.
// ---------------------------------------------------------------------------------------------

import { callWithTelemetryAndErrorHandlingSync } from "vscode-azureextensionui";
import { ext } from "../extensionVariables";
import { waitForLanguageServerAvailable } from "../languageclient/startArmLanguageServer";
import { CaseInsensitiveMap } from "../util/CaseInsensitiveMap";

/**
* Returns a case-insensitive map of available resource types and their apiVersions for a given ARM schema from the
* currently-known schema cache in the language server.
* The map is keyed by the resource type and each value is an array of valid apiVersions.
*
* @param schema The ARM schema of the template document to query for
*/
export async function getAvailableResourceTypesAndVersions(schema: string): Promise<CaseInsensitiveMap<string, string[]>> {
const map = new CaseInsensitiveMap<string, string[]>();

await callWithTelemetryAndErrorHandlingSync("getAvailableResourceTypesAndVersions", async () => {
await waitForLanguageServerAvailable();

const resourceTypes = <{ [key: string]: string[] }>
await ext.languageServerClient?.
sendRequest("arm-template/getAvailableResourceTypesAndVersions", {
Schema: schema
});

for (const entry of Object.entries(resourceTypes)) {
const key = entry[0];
const value = entry[1].map(apiVersion => apiVersion.toLowerCase());

map.set(key, value);
}
});

return map;
}
65 changes: 57 additions & 8 deletions src/languageclient/startArmLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as path from 'path';
import { Diagnostic, Event, EventEmitter, ProgressLocation, Uri, window, workspace } from 'vscode';
import { callWithTelemetryAndErrorHandling, callWithTelemetryAndErrorHandlingSync, IActionContext, ITelemetryContext, parseError } from 'vscode-azureextensionui';
import { LanguageClient, LanguageClientOptions, RevealOutputChannelOn, ServerOptions } from 'vscode-languageclient';
import { delay } from '../../test/support/delay';
import { acquireSharedDotnetInstallation } from '../acquisition/acquireSharedDotnetInstallation';
import { armTemplateLanguageId, backendValidationDiagnosticsSource, configKeys, configPrefix, downloadDotnetVersion, languageFriendlyName, languageServerFolderName, languageServerName, notifications } from '../constants';
import { convertDiagnosticUrisToLinkedTemplateSchema, INotifyTemplateGraphArgs, IRequestOpenLinkedFileArgs, onRequestOpenLinkedFile } from '../documents/templates/linkedTemplates/linkedTemplates';
Expand All @@ -28,12 +29,12 @@ let haveFirstSchemasFinishedLoading: boolean = false;
let isShowingLoadingSchemasProgress: boolean = false;

export enum LanguageServerState {
NotStarted,
Starting,
Failed,
Running,
Stopped,
LoadingSchemas,
NotStarted, // 0
Starting, // 1
Failed, // 2
Running, // 3
Stopped, // 4
LoadingSchemas, // 5
}

/**
Expand Down Expand Up @@ -77,6 +78,8 @@ export function startArmLanguageServerInBackground(): void {
assertNever(ext.languageServerState);
}

ext.languageServerState = LanguageServerState.Starting;

window.withProgress(
{
location: ProgressLocation.Notification,
Expand All @@ -86,7 +89,6 @@ export function startArmLanguageServerInBackground(): void {
await callWithTelemetryAndErrorHandling('startArmLanguageServer', async (actionContext: IActionContext) => {
actionContext.telemetry.suppressIfSuccessful = true;

ext.languageServerState = LanguageServerState.Starting;
try {
// The server is implemented in .NET Core. We run it by calling 'dotnet' with the dll as an argument
let serverDllPath: string = findLanguageServer();
Expand Down Expand Up @@ -123,7 +125,7 @@ async function getLangServerVersion(): Promise<string | undefined> {
});
}

export async function startLanguageClient(serverDllPath: string, dotnetExePath: string): Promise<void> {
async function startLanguageClient(serverDllPath: string, dotnetExePath: string): Promise<void> {
// tslint:disable-next-line: no-suspicious-comment
// tslint:disable-next-line: max-func-body-length // TODO: Refactor function
await callWithTelemetryAndErrorHandling('startArmLanguageClient', async (actionContext: IActionContext) => {
Expand Down Expand Up @@ -216,6 +218,8 @@ export async function startLanguageClient(serverDllPath: string, dotnetExePath:
});

try {
// client.trace = Trace.Messages;

let disposable = client.start();
ext.context.subscriptions.push(disposable);

Expand Down Expand Up @@ -382,3 +386,48 @@ function showLoadingSchemasProgress(): void {
});
}
}

export async function waitForLanguageServerAvailable(): Promise<void> {
const currentState = ext.languageServerState;
switch (currentState) {
case LanguageServerState.Stopped:
case LanguageServerState.NotStarted:
case LanguageServerState.Failed:
startArmLanguageServerInBackground();
break;

case LanguageServerState.Running:
return;

case LanguageServerState.LoadingSchemas:
case LanguageServerState.Starting:
break;

default:
assertNever(currentState);
}

// tslint:disable-next-line: no-constant-condition
while (true) {
switch (ext.languageServerState) {
case LanguageServerState.Failed:
throw new Error(`Language server failed on start-up: ${ext.languageServerStartupError}`);
case LanguageServerState.NotStarted:
case LanguageServerState.Starting:
case LanguageServerState.LoadingSchemas:
await delay(100);
break;
case LanguageServerState.Running:
await delay(1000); // Give vscode time to notice the new formatter available (I don't know of a way to detect this)

ext.outputChannel.appendLine("Language server is ready.");
assert(ext.languageServerClient);
return;

case LanguageServerState.Stopped:
throw new Error('Language server has stopped');
default:
assertNever(ext.languageServerState);
}
}
}
4 changes: 4 additions & 0 deletions src/util/CaseInsensitiveMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export class CaseInsensitiveMap<TKey extends string, TValue> {
return casePreservedKeys.values();
}

public entries(): [TKey, TValue][] {
return <[TKey, TValue][]>Object.entries(this.map);
}

public map<TReturn>(callbackfn: (key: TKey, value: TValue) => TReturn): TReturn[] {
const array: TReturn[] = [];
this._map.forEach((entry: [TKey, TValue]) => {
Expand Down
Loading