public async createChildImpl()

in src/tree/CosmosDBTreeItem.ts [97:163]


    public async createChildImpl(context: ICreateChildImplContext): Promise<AzExtTreeItem> {
        const cosmosDBApi = await this.getCosmosDBApi();
        const databaseToAdd = await cosmosDBApi.pickTreeItem({
            resourceType: 'Database'
        });
        if (!databaseToAdd) {
            throw new UserCancelledError('cosmosDBpickTreeItem');
        }

        const client = await this.parent.site.createClient(context);
        const appSettingsDict = await client.listApplicationSettings();
        appSettingsDict.properties = appSettingsDict.properties || {};

        let newAppSettings: Map<string, string>;
        if (databaseToAdd.docDBData) {
            const docdbAppSettings = new Map([
                [this._endpointSuffix, nonNullProp(databaseToAdd, 'docDBData').documentEndpoint],
                [this._keySuffix, nonNullProp(databaseToAdd, 'docDBData').masterKey],
                [this._databaseSuffix, databaseToAdd.databaseName]
            ]);
            const docdbSuffixes = [this._endpointSuffix, this._keySuffix, this._databaseSuffix];
            newAppSettings = await this.promptForAppSettings(context, appSettingsDict, docdbAppSettings, docdbSuffixes, 'AZURE_COSMOS');
        } else if (databaseToAdd.postgresData) {
            const postgresAppSettings: Map<string | undefined, string | undefined> = new Map([
                [this._pgHostSuffix, databaseToAdd.hostName],
                [this._pgDbNameSuffix, databaseToAdd.databaseName],
                [this._pgUserSuffix, databaseToAdd.postgresData?.username],
                [this._pgPassSuffix, databaseToAdd.postgresData?.password],
                [this._pgPortSuffix, databaseToAdd.port]
            ]);
            const postgresSuffixes = [this._pgHostSuffix, this._pgDbNameSuffix, this._pgUserSuffix, this._pgPassSuffix, this._pgPortSuffix];
            newAppSettings = await this.promptForAppSettings(context, appSettingsDict, postgresAppSettings, postgresSuffixes, 'POSTGRES');
        } else {
            const mongoAppSettings: Map<string | undefined, string | undefined> = new Map([[undefined, databaseToAdd.connectionString]]);
            newAppSettings = await this.promptForAppSettings(context, appSettingsDict, mongoAppSettings, undefined, 'MONGO_URL');
        }

        for (const [k, v] of newAppSettings) {
            appSettingsDict.properties[k] = v;
        }

        await client.updateApplicationSettings(appSettingsDict);
        await this.parent.appSettingsNode.refresh(context);

        const createdDatabase = new CosmosDBConnection(this, databaseToAdd, Array.from(newAppSettings.keys()));
        context.showCreatingTreeItem(createdDatabase.label);

        const revealDatabase: vscode.MessageItem = { title: localize('reveal', 'Reveal Database') };
        const manageFirewallRules: vscode.MessageItem = { title: localize('manageFirewallRulesMsgItem', 'Manage Firewall Rules') };
        const message: string = localize(
            'connectedDatabase', 'Database "{0}" connected to web app "{1}". Created the following application settings: {2}',
            createdDatabase.label, this.parent.site.fullName, Array.from(newAppSettings.keys()).join(', '));
        // Don't wait
        const buttons: vscode.MessageItem[] = [revealDatabase];
        if (createdDatabase.cosmosExtensionItem.azureData && createdDatabase.cosmosExtensionItem.postgresData) {
            buttons.push(manageFirewallRules);
        }
        void vscode.window.showInformationMessage(message, ...buttons).then(async (result: vscode.MessageItem | undefined) => {
            if (result === revealDatabase) {
                await createdDatabase.cosmosExtensionItem.reveal();
            } else if (result === manageFirewallRules) {
                const accountId: string | undefined = createdDatabase.cosmosExtensionItem.azureData?.accountId;
                await openInPortal(this, `${accountId}/connectionSecurity`);
            }
        });
        return createdDatabase;
    }