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

fix(sql): remove sql skip-adding-user question #1443

Merged
merged 4 commits into from
Jun 28, 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 packages/fx-core/src/plugins/resource/sql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ export class SqlPlugin implements Plugin {
);
}
const errorCode = res.error.source + "." + res.error.name;
const errorType = res.error instanceof SystemError ? Telemetry.systemError : Telemetry.userError;
const errorType =
res.error instanceof SystemError ? Telemetry.systemError : Telemetry.userError;
TelemetryUtils.init(ctx);
let errorMessage = res.error.message;
if (res.error.innerError) {
Expand Down
47 changes: 20 additions & 27 deletions packages/fx-core/src/plugins/resource/sql/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import {
ok,
Stage,
QTreeNode,
Func,
Platform,
Inputs,
Func,
} from "@microsoft/teamsfx-api";
import { ManagementClient } from "./managementClient";
import { ErrorMessage } from "./errors";
Expand All @@ -22,12 +21,7 @@ import { formatEndpoint, parseToken, UserType } from "./utils/commonUtils";
import { Constants, HelpLinks, Telemetry } from "./constants";
import { Message } from "./utils/message";
import { TelemetryUtils } from "./utils/telemetryUtils";
import {
adminNameQuestion,
adminPasswordQuestion,
confirmPasswordQuestion,
skipAddingUserQuestion,
} from "./questions";
import { adminNameQuestion, adminPasswordQuestion, confirmPasswordQuestion } from "./questions";
import {
sqlConfirmPasswordValidatorGenerator,
sqlPasswordValidatorGenerator,
Expand Down Expand Up @@ -82,7 +76,6 @@ export class SqlPluginImpl {
sqlNode.addChild(new QTreeNode(adminNameQuestion));
sqlNode.addChild(new QTreeNode(adminPasswordQuestion));
sqlNode.addChild(new QTreeNode(confirmPasswordQuestion));
sqlNode.addChild(skipAddingUserQuestion);
return ok(sqlNode);
}
this.init(ctx);
Expand All @@ -98,9 +91,6 @@ export class SqlPluginImpl {
sqlNode.addChild(new QTreeNode(confirmPasswordQuestion));
}

if (ctx.answers?.platform === Platform.CLI) {
sqlNode.addChild(skipAddingUserQuestion);
}
return ok(sqlNode);
}
return ok(undefined);
Expand Down Expand Up @@ -134,14 +124,15 @@ export class SqlPluginImpl {
TelemetryUtils.init(ctx);
TelemetryUtils.sendEvent(Telemetry.stage.preProvision + Telemetry.startSuffix);

this.config.skipAddingUser = ctx.config.get(Constants.skipAddingUser) as boolean;
if (ctx.answers?.platform === Platform.CLI) {
const skipAddingUser = ctx.answers![Constants.questionKey.skipAddingUser] as string;
if (skipAddingUser) {
this.config.skipAddingUser = skipAddingUser === "true" ? true : false;
ctx.config.set(Constants.skipAddingUser, this.config.skipAddingUser);
}
const skipAddingUser = ctx.config.get(Constants.skipAddingUser);
if (skipAddingUser === undefined) {
this.config.skipAddingUser = (await ctx.azureAccountProvider?.getIdentityCredentialAsync())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be simplified to :

this.config.skipAddingUser = !(await ctx.azureAccountProvider?.getIdentityCredentialAsync())

? false
: true;
} else {
this.config.skipAddingUser = skipAddingUser as boolean;
}

// sql server name
ctx.logProvider?.debug(Message.endpoint(this.config.sqlEndpoint));

Expand Down Expand Up @@ -227,10 +218,11 @@ export class SqlPluginImpl {
ctx.logProvider?.info(Message.startPostProvision);
DialogUtils.init(ctx, ProgressTitle.PostProvision, ProgressTitle.PostProvisionSteps);
TelemetryUtils.init(ctx);
TelemetryUtils.sendEvent(Telemetry.stage.postProvision + Telemetry.startSuffix,
undefined,
{ [Telemetry.properties.skipAddingUser]: this.config.skipAddingUser ? Telemetry.valueYes : Telemetry.valueNo }
);
TelemetryUtils.sendEvent(Telemetry.stage.postProvision + Telemetry.startSuffix, undefined, {
[Telemetry.properties.skipAddingUser]: this.config.skipAddingUser
? Telemetry.valueYes
: Telemetry.valueNo,
});

const sqlClient = new SqlClient(ctx, this.config);
const managementClient: ManagementClient = new ManagementClient(ctx, this.config);
Expand Down Expand Up @@ -297,10 +289,11 @@ export class SqlPluginImpl {

await managementClient.deleteLocalFirewallRule();

TelemetryUtils.sendEvent(Telemetry.stage.postProvision,
true,
{ [Telemetry.properties.skipAddingUser]: this.config.skipAddingUser ? Telemetry.valueYes : Telemetry.valueNo }
);
TelemetryUtils.sendEvent(Telemetry.stage.postProvision, true, {
[Telemetry.properties.skipAddingUser]: this.config.skipAddingUser
? Telemetry.valueYes
: Telemetry.valueNo,
});
ctx.logProvider?.info(Message.endPostProvision);
await DialogUtils.progressBar?.end();
return ok(undefined);
Expand Down
28 changes: 12 additions & 16 deletions packages/fx-core/src/plugins/resource/sql/questions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { Inputs, QTreeNode, TextInputQuestion } from "@microsoft/teamsfx-api";
import { Inputs, TextInputQuestion } from "@microsoft/teamsfx-api";
import { Constants } from "./constants";
import { sqlConfirmPasswordValidatorGenerator, sqlPasswordValidatorGenerator, sqlUserNameValidator } from "./utils/checkInput";
import {
sqlConfirmPasswordValidatorGenerator,
sqlPasswordValidatorGenerator,
sqlUserNameValidator,
} from "./utils/checkInput";

export const adminNameQuestion: TextInputQuestion = {
name: Constants.questionKey.adminName,
Expand All @@ -12,8 +16,8 @@ export const adminNameQuestion: TextInputQuestion = {
validFunc: async (input: string, previousInputs?: Inputs): Promise<string | undefined> => {
const res = sqlUserNameValidator(input as string);
return res;
}
}
},
},
};

export const adminPasswordQuestion: TextInputQuestion = {
Expand All @@ -27,8 +31,8 @@ export const adminPasswordQuestion: TextInputQuestion = {
const name = previousInputs![Constants.questionKey.adminName] as string;
const res = sqlPasswordValidatorGenerator(name)(password);
return res;
}
}
},
},
};

export const confirmPasswordQuestion: TextInputQuestion = {
Expand All @@ -42,14 +46,6 @@ export const confirmPasswordQuestion: TextInputQuestion = {
const password = previousInputs![Constants.questionKey.adminPassword] as string;
const res = sqlConfirmPasswordValidatorGenerator(password)(confirm);
return res;
}
}
},
},
};

export const skipAddingUserQuestion = new QTreeNode({
name: Constants.questionKey.skipAddingUser,
title: Constants.userQuestion.confirmPassword,
type: "singleSelect",
staticOptions: ["true", "false"],
default: "false",
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import "mocha";
import * as chai from "chai";
import chaiAsPromised from "chai-as-promised";
import { TestHelper } from "../helper";
import { SqlPlugin } from "../../../../../src/plugins/resource/sql";
import * as dotenv from "dotenv";
import { Platform, PluginContext } from "@microsoft/teamsfx-api";
import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";
import * as faker from "faker";
import * as sinon from "sinon";
import { ApplicationTokenCredentials } from "@azure/ms-rest-nodeauth";
import { TokenResponse } from "adal-node/lib/adal";
import { Constants } from "../../../../../src/plugins/resource/sql/constants";
import * as commonUtils from "../../../../../src/plugins/resource/sql/utils/commonUtils";

chai.use(chaiAsPromised);

dotenv.config();
const testWithAzure: boolean = process.env.UT_TEST_ON_AZURE ? true : false;

describe("skipAddingUser", () => {
let sqlPlugin: SqlPlugin;
let pluginContext: PluginContext;
let credentials: msRestNodeAuth.TokenCredentialsBase;

before(async () => {
if (testWithAzure) {
credentials = await msRestNodeAuth.interactiveLogin();
} else {
credentials = new msRestNodeAuth.ApplicationTokenCredentials(
faker.random.uuid(),
faker.internet.url(),
faker.internet.password()
);
}
});

beforeEach(async () => {
sqlPlugin = new SqlPlugin();
pluginContext = await TestHelper.pluginContext(credentials);
});

afterEach(() => {
sinon.restore();
});

it("preProvision", async function () {
// Arrange
sinon
.stub(ApplicationTokenCredentials.prototype, "getToken")
.resolves({ accessToken: faker.random.word() } as TokenResponse);
const mockInfo: commonUtils.TokenInfo = {
name: faker.random.word(),
objectId: faker.random.word(),
userType: commonUtils.UserType.User,
};
sinon.stub(commonUtils, "parseToken").returns(mockInfo);
pluginContext.answers = { platform: Platform.VSCode };
pluginContext.answers[Constants.questionKey.adminName] = "test-admin";
pluginContext.answers[Constants.questionKey.adminPassword] = "test-password";

// Act
let preProvisionResult = await sqlPlugin.preProvision(pluginContext);

// Assert
chai.assert.isTrue(preProvisionResult.isOk());
chai.assert.isFalse(sqlPlugin.sqlImpl.config.skipAddingUser);

// set no identity credential
pluginContext.azureAccountProvider!.getIdentityCredentialAsync = async () => {
return undefined;
};
// Act
preProvisionResult = await sqlPlugin.preProvision(pluginContext);

// Assert
chai.assert.isTrue(preProvisionResult.isOk());
chai.assert.isTrue(sqlPlugin.sqlImpl.config.skipAddingUser);

// set config true
pluginContext.config.set(Constants.skipAddingUser, true);
// Act
preProvisionResult = await sqlPlugin.preProvision(pluginContext);

// Assert
chai.assert.isTrue(preProvisionResult.isOk());
chai.assert.isTrue(sqlPlugin.sqlImpl.config.skipAddingUser);

// set config false
pluginContext.config.set(Constants.skipAddingUser, false);
// Act
preProvisionResult = await sqlPlugin.preProvision(pluginContext);

// Assert
chai.assert.isTrue(preProvisionResult.isOk());
chai.assert.isFalse(sqlPlugin.sqlImpl.config.skipAddingUser);
});
});