Skip to content

Commit

Permalink
Handle Compute extensions file generation
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-c-martin authored Oct 10, 2021
1 parent d26a5db commit 6bb024f
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 55 deletions.
4 changes: 2 additions & 2 deletions generator/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface SchemaConfiguration {
relativePath: string;
}

interface SchemaReference {
export interface SchemaReference {
scope: ScopeType;
type: string;
reference: string;
Expand Down Expand Up @@ -178,7 +178,7 @@ async function generateSchemaConfig(outputFile: string, namespace: string, apiVe

let output = await readJsonFile(outputFile);
if (autoGenConfig?.postProcessor) {
autoGenConfig?.postProcessor(namespace, apiVersion, output);
await autoGenConfig?.postProcessor(namespace, apiVersion, output);

await writeJsonFile(outputFile, output);
}
Expand Down
2 changes: 1 addition & 1 deletion generator/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface AutoGenResourceConfig {
}

export interface SchemaPostProcessor {
(namespace: string, apiVersion: string, schema: any): void,
(namespace: string, apiVersion: string, schema: any): Promise<void>,
}

export interface Package {
Expand Down
2 changes: 1 addition & 1 deletion generator/processors/Microsoft.Authorization.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SchemaPostProcessor } from '../models';

export const postProcessor: SchemaPostProcessor = (_namespace: string, _apiVersion: string, schema: any) => {
export const postProcessor: SchemaPostProcessor = async (_namespace: string, _apiVersion: string, schema: any) => {
const allowedValues = schema.definitions?.ParameterDefinitionsValue?.properties?.allowedValues;
if (allowedValues && allowedValues.oneOf) {
const allowedValuesItems = allowedValues.oneOf[0]?.items
Expand Down
69 changes: 45 additions & 24 deletions generator/processors/Microsoft.Compute.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,70 @@
import path from 'path';
import fs from 'fs'
import { SchemaPostProcessor } from '../models';
import * as constants from '../constants';
import { SchemaReference } from '../generate';
import { SchemaPostProcessor, ScopeType } from '../models';
import { saveAutoGeneratedSchemaRefs } from '../generate';
import { safeMkdir, writeJsonFile } from '../utils';

export const postProcessor: SchemaPostProcessor = (namespace: string, apiVersion: string, schema: any) => {
export const postProcessor: SchemaPostProcessor = async (namespace: string, apiVersion: string, schema: any) => {
const extensionsDefinitions = extensionsDefinition(apiVersion);

const references: SchemaReference[] = [];

// Set schema.resourceDefinitions.virtualMachines_extensions.properties.properties = $extensionsProperties
if (schema.resourceDefinitions?.virtualMachines_extensions?.properties?.properties) {
schema.resourceDefinitions.virtualMachines_extensions.properties.properties = extensionsProperties(apiVersion);

// set extensionsDefinitions.resourceDefinitions.virtualMachines_extensions = schema.resourceDefinitions.virtualMachines_extensions
(extensionsDefinitions.resourceDefinitions as any).virtualMachines_extensions = schema.resourceDefinitions.virtualMachines_extensions;

// remove schema.resourceDefinitions.virtualMachines_extensions
delete schema.resourceDefinitions.virtualMachines_extensions;

references.push({
scope: ScopeType.ResourceGroup,
type: 'Microsoft.Compute/virtualMachines/extensions',
reference: 'resourceDefinitions/virtualMachines_extensions',
});
}

// Set schema.resourceDefinitions.virtualMachineScaleSets_extensions.properties.properties = $extensionsProperties
if (schema.resourceDefinitions?.virtualMachineScaleSets_extensions?.properties?.properties) {
schema.resourceDefinitions.virtualMachineScaleSets_extensions.properties.properties = extensionsProperties(apiVersion);

// set extensionsDefinitions.resourceDefinitions.virtualMachineScaleSets_extensions = schema.resourceDefinitions.virtualMachineScaleSets_extensions
(extensionsDefinitions.resourceDefinitions as any).virtualMachineScaleSets_extensions = schema.resourceDefinitions.virtualMachineScaleSets_extensions;

// remove schema.resourceDefinitions.virtualMachineScaleSets_extensions
delete schema.resourceDefinitions.virtualMachineScaleSets_extensions

references.push({
scope: ScopeType.ResourceGroup,
type: 'Microsoft.Compute/virtualMachineScaleSets/extensions',
reference: 'resourceDefinitions/virtualMachineScaleSets_extensions',
});
}

// Set schema.definitions.virtualMachines_extensions_childResource.properties.properties = $extensionsProperties
if (schema.definitions.virtualMachines_extensions_childResource?.properties?.properties) {
schema.definitions.virtualMachines_extensions_childResource.properties.properties = extensionsProperties(apiVersion);
}

// Set schema.definitions.VirtualMachineScaleSetExtension.properties.properties = $extensionsProperties
if (schema.definitions.VirtualMachineScaleSetExtension?.properties?.properties) {
schema.definitions.VirtualMachineScaleSetExtension.properties.properties = extensionsProperties(apiVersion);
}

// set extensionsDefinitions.resourceDefinitions.virtualMachines_extensions = schema.resourceDefinitions.virtualMachines_extensions
extensionsDefinitions.resourceDefinitions.virtualMachines_extensions = schema.resourceDefinitions.virtualMachines_extensions
// set extensionsDefinitions.resourceDefinitions.virtualMachineScaleSets_extensions = schema.resourceDefinitions.virtualMachineScaleSets_extensions
extensionsDefinitions.resourceDefinitions.virtualMachineScaleSets_extensions = schema.resourceDefinitions.virtualMachineScaleSets_extensions
// save extensionsDefinitions as Microsoft.Compute.Extensions.json
const relativePath = `${apiVersion}/Microsoft.Compute.Extensions.json`;
const extensionFile = path.join(constants.schemasBasePath, relativePath);

// remove schema.resourceDefinitions.virtualMachines_extensions
delete schema.resourceDefinitions.virtualMachines_extensions
// remove schema.resourceDefinitions.virtualMachineScaleSets_extensions
delete schema.resourceDefinitions.virtualMachineScaleSets_extensions

// save extensionsDefinitions as Microsoft.ComputeExtensions.json
const extensionFile = path.normalize(path.join(__dirname, `../../schemas/${apiVersion}/Microsoft.Compute.Extensions.json`));
const extensionDir = path.dirname(extensionFile);
if (!fs.existsSync(extensionDir)) {
fs.mkdirSync(extensionDir, { recursive: true });
}
await safeMkdir(path.dirname(extensionFile));
await writeJsonFile(extensionFile, extensionsDefinitions);

const data = JSON.stringify(extensionsDefinitions, null, 2);
fs.writeFileSync(extensionFile, data);
await saveAutoGeneratedSchemaRefs([{
relativePath,
references,
}]);
}

const extensionsDefinition = (apiVersion: string) => (
Expand All @@ -49,10 +73,7 @@ const extensionsDefinition = (apiVersion: string) => (
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Microsoft.Compute.Extensions",
"description": "Microsoft Compute Extensions Resource Types",
"resourceDefinitions": {
"virtualMachines_extensions": {},
"virtualMachineScaleSets_extensions": {}
},
"resourceDefinitions": {},
"definitions": {
"genericExtension": {
"type": "object",
Expand Down
2 changes: 1 addition & 1 deletion generator/processors/Microsoft.Insights.Application.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SchemaPostProcessor } from '../models';

export const postProcessor: SchemaPostProcessor = (namespace: string, apiVersion: string, schema: any) => {
export const postProcessor: SchemaPostProcessor = async (namespace: string, apiVersion: string, schema: any) => {
// this shouldn't be a resource definition, and it causes Export failures as it contains duplicate properties "Type" and "type"
const resources = Object.values<any>(schema.resourceDefinitions || {});

Expand Down
2 changes: 1 addition & 1 deletion generator/processors/Microsoft.MachineLearning.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SchemaPostProcessor } from '../models';
import { replaceCyclicRef } from './helpers';

export const postProcessor: SchemaPostProcessor = (namespace: string, apiVersion: string, schema: any) => {
export const postProcessor: SchemaPostProcessor = async (namespace: string, apiVersion: string, schema: any) => {
replaceCyclicRef(schema.definitions?.ModeValueInfo?.properties?.parameters?.oneOf[0]?.items);
}
2 changes: 1 addition & 1 deletion generator/processors/Microsoft.MachineLearningServices.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SchemaPostProcessor } from '../models';
import { replaceCyclicRef } from './helpers';

export const postProcessor: SchemaPostProcessor = (namespace: string, apiVersion: string, schema: any) => {
export const postProcessor: SchemaPostProcessor = async (namespace: string, apiVersion: string, schema: any) => {
replaceCyclicRef(schema.definitions?.LabelClass?.properties?.subclasses?.oneOf[0]?.additionalProperties);
}
2 changes: 1 addition & 1 deletion generator/processors/Microsoft.Resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function appendProps(definition: any, props: any[]) {
}
}

export const postProcessor: SchemaPostProcessor = (namespace: string, apiVersion: string, schema: any) => {
export const postProcessor: SchemaPostProcessor = async (namespace: string, apiVersion: string, schema: any) => {
appendProps(schema.tenant_resourceDefinitions?.deployments, [subscriptionProps]);
appendProps(schema.managementGroup_resourceDefinitions?.deployments, [subscriptionProps]);
appendProps(schema.subscription_resourceDefinitions?.deployments, [resourceGroupProps]);
Expand Down
2 changes: 1 addition & 1 deletion generator/processors/Microsoft.SecurityInsights.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SchemaPostProcessor } from '../models';
import { replaceCyclicRef } from './helpers';

export const postProcessor: SchemaPostProcessor = (namespace: string, apiVersion: string, schema: any) => {
export const postProcessor: SchemaPostProcessor = async (namespace: string, apiVersion: string, schema: any) => {
replaceCyclicRef(schema.definitions?.MetadataDependencies?.properties?.criteria?.oneOf[0]?.items);
}
2 changes: 1 addition & 1 deletion generator/processors/Microsoft.Storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SchemaPostProcessor, ScopeType } from '../models';

export const postProcessor: SchemaPostProcessor = (namespace: string, apiVersion: string, schema: any) => {
export const postProcessor: SchemaPostProcessor = async (namespace: string, apiVersion: string, schema: any) => {
const scopes = [
'tenant_resourceDefinitions',
'managementGroup_resourceDefinitions',
Expand Down
22 changes: 1 addition & 21 deletions schemas/2019-04-01/deploymentTemplate.json
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,6 @@
{ "$ref": "https://schema.management.azure.com/schemas/2017-11-01/Microsoft.Network.json#/resourceDefinitions/virtualNetworkGateways" },
{ "$ref": "https://schema.management.azure.com/schemas/2017-11-01/Microsoft.Network.json#/resourceDefinitions/virtualNetworks_subnets" },
{ "$ref": "https://schema.management.azure.com/schemas/2017-11-01/Microsoft.Network.json#/resourceDefinitions/virtualNetworks_virtualNetworkPeerings" },
{ "$ref": "https://schema.management.azure.com/schemas/2017-12-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachines_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2017-12-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachineScaleSets_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2015-05-01-preview/Microsoft.Network.json#/resourceDefinitions/applicationGateways" },
{ "$ref": "https://schema.management.azure.com/schemas/2015-05-01-preview/Microsoft.Network.json#/resourceDefinitions/connections" },
{ "$ref": "https://schema.management.azure.com/schemas/2015-05-01-preview/Microsoft.Network.json#/resourceDefinitions/expressRouteCircuits_authorizations" },
Expand Down Expand Up @@ -1889,12 +1887,6 @@
{ "$ref": "https://schema.management.azure.com/schemas/2016-12-01/Microsoft.RecoveryServices.json#/resourceDefinitions/vaults_backupFabrics_protectionContainers_protectedItems" },
{ "$ref": "https://schema.management.azure.com/schemas/2016-12-01/Microsoft.RecoveryServices.json#/resourceDefinitions/vaults_backupPolicies" },
{ "$ref": "https://schema.management.azure.com/schemas/2017-07-01/Microsoft.RecoveryServices.json#/resourceDefinitions/vaults_backupFabrics_backupProtectionIntent" },
{ "$ref": "https://schema.management.azure.com/schemas/2018-06-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachines_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2018-06-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachineScaleSets_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2018-10-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachines_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2018-10-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachineScaleSets_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2019-03-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachines_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2019-03-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachineScaleSets_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2015-11-01-preview/Microsoft.OperationsManagement.json#/resourceDefinitions/ManagementConfigurations" },
{ "$ref": "https://schema.management.azure.com/schemas/2015-11-01-preview/Microsoft.OperationsManagement.json#/resourceDefinitions/solutions" },
{ "$ref": "https://schema.management.azure.com/schemas/2015-08-31-preview/Microsoft.ManagedIdentity.json#/resourceDefinitions/userAssignedIdentities" },
Expand All @@ -1908,8 +1900,6 @@
{ "$ref": "https://schema.management.azure.com/schemas/2019-05-01-preview/Microsoft.AppPlatform.json#/resourceDefinitions/Spring_apps" },
{ "$ref": "https://schema.management.azure.com/schemas/2019-05-01-preview/Microsoft.AppPlatform.json#/resourceDefinitions/Spring_apps_bindings" },
{ "$ref": "https://schema.management.azure.com/schemas/2019-05-01-preview/Microsoft.AppPlatform.json#/resourceDefinitions/Spring_apps_deployments" },
{ "$ref": "https://schema.management.azure.com/schemas/2019-07-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachines_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2019-07-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachineScaleSets_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2016-07-12-preview/Microsoft.Advisor.json#/resourceDefinitions/recommendations_suppressions" },
{ "$ref": "https://schema.management.azure.com/schemas/2017-03-31/Microsoft.Advisor.json#/resourceDefinitions/recommendations_suppressions" },
{ "$ref": "https://schema.management.azure.com/schemas/2017-04-19/Microsoft.Advisor.json#/resourceDefinitions/recommendations_suppressions" },
Expand All @@ -1918,17 +1908,7 @@
{ "$ref": "https://schema.management.azure.com/schemas/2020-01-01/Microsoft.Advisor.json#/resourceDefinitions/configurations" },
{ "$ref": "https://schema.management.azure.com/schemas/2014-04-01/Microsoft.Insights.ManuallyAuthored.json#/resourceDefinitions/components" },
{ "$ref": "https://schema.management.azure.com/schemas/2014-04-01/Microsoft.Insights.ManuallyAuthored.json#/resourceDefinitions/webtests" },
{ "$ref": "https://schema.management.azure.com/schemas/2014-04-01/Microsoft.Insights.ManuallyAuthored.json#/resourceDefinitions/autoscalesettings" },
{ "$ref": "https://schema.management.azure.com/schemas/2020-06-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachines_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2020-06-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachineScaleSets_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2020-12-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachines_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2020-12-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachineScaleSets_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2021-03-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachines_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2021-03-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachineScaleSets_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2021-04-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachines_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2021-04-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachineScaleSets_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2021-07-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachines_extensions" },
{ "$ref": "https://schema.management.azure.com/schemas/2021-07-01/Microsoft.Compute.Extensions.json#/resourceDefinitions/virtualMachineScaleSets_extensions" }
{ "$ref": "https://schema.management.azure.com/schemas/2014-04-01/Microsoft.Insights.ManuallyAuthored.json#/resourceDefinitions/autoscalesettings" }
]
}
]
Expand Down
Loading

0 comments on commit 6bb024f

Please sign in to comment.