Skip to content

Commit

Permalink
chore(identity,sql): support bicep (#2018)
Browse files Browse the repository at this point in the history
* chore(sql): add bicep
  • Loading branch information
xzf0587 authored Aug 24, 2021
1 parent daf6fda commit f324d40
Show file tree
Hide file tree
Showing 31 changed files with 730 additions and 86 deletions.
2 changes: 2 additions & 0 deletions packages/fx-core/src/common/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ const SecretDataMatchers = [
"fx-resource-bot.bots",
"fx-resource-bot.composeExtensions",
"fx-resource-apim.apimClientAADClientSecret",
"fx-resource-azure-sql.adminPassword",
];

export const CryptoDataMatchers = new Set([
Expand All @@ -147,6 +148,7 @@ export const CryptoDataMatchers = new Set([
"fx-resource-bot.botPassword",
"fx-resource-bot.localBotPassword",
"fx-resource-apim.apimClientAADClientSecret",
"fx-resource-azure-sql.adminPassword",
]);

/**
Expand Down
74 changes: 37 additions & 37 deletions packages/fx-core/src/plugins/resource/function/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -906,47 +906,47 @@ export class FunctionPluginImpl {

FunctionProvision.updateFunctionSettingsForFrontend(site, frontendEndpoint);
}
}

const sqlConfig: ReadonlyPluginConfig | undefined = ctx.configOfOtherPlugins.get(
DependentPluginInfo.sqlPluginName
);
const identityConfig: ReadonlyPluginConfig | undefined = ctx.configOfOtherPlugins.get(
DependentPluginInfo.identityPluginName
);
if (
this.isPluginEnabled(ctx, DependentPluginInfo.sqlPluginName) &&
this.isPluginEnabled(ctx, DependentPluginInfo.identityPluginName) &&
sqlConfig &&
identityConfig
) {
Logger.info(InfoMessages.dependPluginDetected(DependentPluginInfo.sqlPluginName));
Logger.info(InfoMessages.dependPluginDetected(DependentPluginInfo.identityPluginName));

const identityId: string = this.checkAndGet(
identityConfig.get(DependentPluginInfo.identityId) as string,
"identity Id"
);
const databaseName: string = this.checkAndGet(
sqlConfig.get(DependentPluginInfo.databaseName) as string,
"database name"
);
const sqlEndpoint: string = this.checkAndGet(
sqlConfig.get(DependentPluginInfo.sqlEndpoint) as string,
"sql endpoint"
const sqlConfig: ReadonlyPluginConfig | undefined = ctx.configOfOtherPlugins.get(
DependentPluginInfo.sqlPluginName
);
const identityName: string = this.checkAndGet(
identityConfig.get(DependentPluginInfo.identityName) as string,
"identity name"
const identityConfig: ReadonlyPluginConfig | undefined = ctx.configOfOtherPlugins.get(
DependentPluginInfo.identityPluginName
);
if (
this.isPluginEnabled(ctx, DependentPluginInfo.sqlPluginName) &&
this.isPluginEnabled(ctx, DependentPluginInfo.identityPluginName) &&
sqlConfig &&
identityConfig
) {
Logger.info(InfoMessages.dependPluginDetected(DependentPluginInfo.sqlPluginName));
Logger.info(InfoMessages.dependPluginDetected(DependentPluginInfo.identityPluginName));

const identityId: string = this.checkAndGet(
identityConfig.get(DependentPluginInfo.identityId) as string,
"identity Id"
);
const databaseName: string = this.checkAndGet(
sqlConfig.get(DependentPluginInfo.databaseName) as string,
"database name"
);
const sqlEndpoint: string = this.checkAndGet(
sqlConfig.get(DependentPluginInfo.sqlEndpoint) as string,
"sql endpoint"
);
const identityName: string = this.checkAndGet(
identityConfig.get(DependentPluginInfo.identityName) as string,
"identity name"
);

FunctionProvision.updateFunctionSettingsForSQL(
site,
identityId,
databaseName,
sqlEndpoint,
identityName
);
FunctionProvision.updateFunctionSettingsForSQL(
site,
identityId,
databaseName,
sqlEndpoint,
identityName
);
}
}

const apimConfig: ReadonlyPluginConfig | undefined = ctx.configOfOtherPlugins.get(
Expand Down
15 changes: 15 additions & 0 deletions packages/fx-core/src/plugins/resource/identity/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,18 @@ export class Telemetry {
appid: "appid",
};
}
export class IdentityBicep {
static readonly identityName: string = "userAssignedIdentityProvision.outputs.identityName";
static readonly identityId: string = "userAssignedIdentityProvision.outputs.identityId";
static readonly identity: string = "userAssignedIdentityProvision.outputs.identity";
}

export class IdentityArmOutput {
static readonly identityName: string = "identity_identityName";
static readonly identityId: string = "identity_identityId";
static readonly identity: string = "identity_identity";
}

export class IdentityBicepFile {
static readonly moduleTemplateFileName: string = "userAssignedIdentity.template.bicep";
}
98 changes: 96 additions & 2 deletions packages/fx-core/src/plugins/resource/identity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,27 @@ import {
} from "@microsoft/teamsfx-api";

import { IdentityConfig } from "./config";
import { Constants, Telemetry } from "./constants";
import {
Constants,
IdentityArmOutput,
IdentityBicep,
IdentityBicepFile,
Telemetry,
} from "./constants";
import { ContextUtils } from "./utils/contextUtils";
import { ResultFactory, Result } from "./results";
import { Message } from "./utils/messages";
import { TelemetryUtils } from "./utils/telemetryUtil";
import { formatEndpoint } from "./utils/commonUtils";
import { getTemplatesFolder } from "../../..";
import { generateBicepFiles, getTemplatesFolder } from "../../..";
import { AzureResourceSQL } from "../../solution/fx-solution/question";
import { Service } from "typedi";
import { ResourcePlugins } from "../../solution/fx-solution/ResourcePluginContainer";
import { Providers, ResourceManagementClientContext } from "@azure/arm-resources";
import { Bicep, ConstantString } from "../../../common/constants";
import { ScaffoldArmTemplateResult } from "../../../common/armInterface";
import { isArmSupportEnabled } from "../../../common";
import { getArmOutput } from "../utils4v2";

@Service(ResourcePlugins.IdentityPlugin)
export class IdentityPlugin implements Plugin {
Expand All @@ -48,6 +58,21 @@ export class IdentityPlugin implements Plugin {
config: IdentityConfig = new IdentityConfig();

async provision(ctx: PluginContext): Promise<Result> {
if (!isArmSupportEnabled()) {
return this.provisionImplement(ctx);
} else {
return ok(undefined);
}
}

async postProvision(ctx: PluginContext): Promise<Result> {
if (isArmSupportEnabled()) {
this.syncArmOutput(ctx);
}
return ok(undefined);
}

async provisionImplement(ctx: PluginContext): Promise<Result> {
ctx.logProvider?.info(Message.startProvision);
TelemetryUtils.init(ctx);
TelemetryUtils.sendEvent(Telemetry.stage.provision + Telemetry.startSuffix);
Expand Down Expand Up @@ -108,6 +133,69 @@ export class IdentityPlugin implements Plugin {
return ok(undefined);
}

public async generateArmTemplates(ctx: PluginContext): Promise<Result> {
const selectedPlugins = (ctx.projectSettings?.solutionSettings as AzureSolutionSettings)
.activeResourcePlugins;
const context = {
Plugins: selectedPlugins,
};

const bicepTemplateDirectory = path.join(
getTemplatesFolder(),
"plugins",
"resource",
"identity",
"bicep"
);

const moduleTemplateFilePath = path.join(
bicepTemplateDirectory,
IdentityBicepFile.moduleTemplateFileName
);
const moduleContentResult = await generateBicepFiles(moduleTemplateFilePath, context);
if (moduleContentResult.isErr()) {
throw moduleContentResult.error;
}

const parameterTemplateFilePath = path.join(
bicepTemplateDirectory,
Bicep.ParameterOrchestrationFileName
);
const moduleOrchestrationFilePath = path.join(
bicepTemplateDirectory,
Bicep.ModuleOrchestrationFileName
);
const outputTemplateFilePath = path.join(
bicepTemplateDirectory,
Bicep.OutputOrchestrationFileName
);

const result: ScaffoldArmTemplateResult = {
Modules: {
userAssignedIdentityProvision: {
Content: moduleContentResult.value,
},
},
Orchestration: {
ParameterTemplate: {
Content: await fs.readFile(parameterTemplateFilePath, ConstantString.UTF8Encoding),
},
ModuleTemplate: {
Content: await fs.readFile(moduleOrchestrationFilePath, ConstantString.UTF8Encoding),
Outputs: {
identityName: IdentityBicep.identityName,
identityId: IdentityBicep.identityId,
identity: IdentityBicep.identity,
},
},
OutputTemplate: {
Content: await fs.readFile(outputTemplateFilePath, ConstantString.UTF8Encoding),
},
},
};
return ok(result);
}

async loadArmTemplate(ctx: PluginContext) {
try {
const templatesFolder = path.resolve(getTemplatesFolder(), "plugins", "resource", "identity");
Expand Down Expand Up @@ -163,6 +251,12 @@ export class IdentityPlugin implements Plugin {
throw error;
}
}

private syncArmOutput(ctx: PluginContext) {
ctx.config.set(Constants.identityName, getArmOutput(ctx, IdentityArmOutput.identityName));
ctx.config.set(Constants.identityId, getArmOutput(ctx, IdentityArmOutput.identityId));
ctx.config.set(Constants.identity, getArmOutput(ctx, IdentityArmOutput.identity));
}
}

export default new IdentityPlugin();
19 changes: 19 additions & 0 deletions packages/fx-core/src/plugins/resource/sql/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export class Constants {
public static readonly sqlEndpoint: string = "sqlEndpoint";
public static readonly databaseName: string = "databaseName";
public static readonly skipAddingUser: string = "skipAddingUser";
public static readonly admin: string = "admin";
public static readonly adminPassword: string = "adminPassword";
public static readonly aadAdmin: string = "aadAdmin";
public static readonly aadAdminObjectId: string = "aadAdminObjectId";

public static readonly solution: string = "solution";
public static readonly solutionPluginFullName = "fx-solution-azure";
Expand Down Expand Up @@ -75,6 +79,7 @@ export class Telemetry {
provision: "provision",
postProvision: "post-provision",
getQuestion: "get-question",
generateArmTemplates: "generate-arm-templates",
};

static readonly properties = {
Expand All @@ -91,3 +96,17 @@ export class Telemetry {
export class HelpLinks {
static readonly default = "https://aka.ms/teamsfx-sql-help";
}

export class AzureSqlBicep {
static readonly sqlEndpoint: string = "azureSqlProvision.outputs.sqlEndpoint";
static readonly databaseName: string = "azureSqlProvision.outputs.databaseName";
}

export class AzureSqlArmOutput {
static readonly sqlEndpoint: string = "azureSql_sqlEndpoint";
static readonly databaseName: string = "azureSql_databaseName";
}

export class AzureSqlBicepFile {
static readonly moduleTemplateFileName: string = "sql.template.bicep";
}
21 changes: 19 additions & 2 deletions packages/fx-core/src/plugins/resource/sql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Licensed under the MIT license.
import {
AzureSolutionSettings,
ok,
err,
Func,
FxError,
Plugin,
PluginContext,
Expand All @@ -14,6 +14,7 @@ import {
UserError,
} from "@microsoft/teamsfx-api";
import { Service } from "typedi";
import { isArmSupportEnabled } from "../../..";
import {
AzureResourceSQL,
HostTypeOptionAzure,
Expand Down Expand Up @@ -50,7 +51,15 @@ export class SqlPlugin implements Plugin {
}

public async provision(ctx: PluginContext): Promise<SqlResult> {
return this.runWithSqlError(Telemetry.stage.provision, () => this.sqlImpl.provision(ctx), ctx);
if (!isArmSupportEnabled()) {
return this.runWithSqlError(
Telemetry.stage.provision,
() => this.sqlImpl.provision(ctx),
ctx
);
} else {
return ok(undefined);
}
}

public async postProvision(ctx: PluginContext): Promise<SqlResult> {
Expand All @@ -61,6 +70,14 @@ export class SqlPlugin implements Plugin {
);
}

public async generateArmTemplates(ctx: PluginContext): Promise<SqlResult> {
return this.runWithSqlError(
Telemetry.stage.generateArmTemplates,
() => this.sqlImpl.generateArmTemplates(ctx),
ctx
);
}

public async getQuestions(
stage: Stage,
ctx: PluginContext
Expand Down
Loading

0 comments on commit f324d40

Please sign in to comment.