Skip to content

Commit

Permalink
Merge branch 'master' into jh-outdated
Browse files Browse the repository at this point in the history
  • Loading branch information
joehan committed Jun 7, 2024
2 parents 704a494 + fe3bfb7 commit 1921141
Show file tree
Hide file tree
Showing 19 changed files with 130 additions and 65 deletions.
12 changes: 12 additions & 0 deletions firebase-vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
## NEXT

## 0.2.6

- Updated internal firebase-tools dependency to 13.11.1

- Fix behaviour on failed postgres connection

## 0.2.5

- Icon fix

## 0.2.4

- Emulator bump v1.2.0
- Connect to postgres flow reworked
- Telemetry enabled
Expand Down
4 changes: 2 additions & 2 deletions firebase-vscode/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion firebase-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"publisher": "firebase",
"icon": "./resources/firebase_logo.png",
"description": "VSCode Extension for Firebase",
"version": "0.2.3",
"version": "0.2.6",
"engines": {
"vscode": "^1.69.0"
},
Expand Down
File renamed without changes.
13 changes: 10 additions & 3 deletions firebase-vscode/src/data-connect/emulator-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fetch from "node-fetch";
import { Observable, of } from "rxjs";
import { backOff } from "exponential-backoff";
import { ResolvedDataConnectConfigs } from "./config";
import { Signal } from "@preact/signals-core";

enum Kind {
KIND_UNSPECIFIED = "KIND_UNSPECIFIED",
Expand All @@ -28,19 +29,20 @@ export const emulatorOutputChannel =
vscode.window.createOutputChannel("Firebase Emulators");

/**
*
* TODO: convert to class
* @param fdcEndpoint FDC Emulator endpoint
*/
export async function runEmulatorIssuesStream(
configs: ResolvedDataConnectConfigs,
fdcEndpoint: string,
isPostgresEnabled: Signal<boolean>,
) {
const obsErrors = await getEmulatorIssuesStream(configs, fdcEndpoint);
const obsConverter = {
next(nextResponse: EmulatorIssueResponse) {
if (nextResponse.result?.issues?.length) {
for (const issue of nextResponse.result.issues) {
displayIssue(issue);
displayAndHandleIssue(issue, isPostgresEnabled);
}
}
},
Expand All @@ -57,14 +59,19 @@ export async function runEmulatorIssuesStream(
/**
* Based on the severity of the issue, either log, display notification, or display interactive popup to the user
*/
export function displayIssue(issue: EmulatorIssue) {
export function displayAndHandleIssue(issue: EmulatorIssue, isPostgresEnabled: Signal<boolean>) {
const issueMessage = `Data Connect Emulator: ${issue.kind.toString()} - ${issue.message}`;
if (issue.severity === Severity.ALERT) {
vscode.window.showErrorMessage(issueMessage);
} else if (issue.severity === Severity.NOTICE) {
vscode.window.showWarningMessage(issueMessage);
}
emulatorOutputChannel.appendLine(issueMessage);

// special handlings
if (issue.kind === Kind.SQL_CONNECTION) {
isPostgresEnabled.value = false;
}
}

/**
Expand Down
10 changes: 7 additions & 3 deletions firebase-vscode/src/data-connect/emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export class DataConnectEmulatorController implements vscode.Disposable {

// Notify webviews when the emulator status changes
effect(() => {
if (this.isPostgresEnabled) {
this.emulatorsController.emulatorStatusItem.show();
} else {
this.emulatorsController.emulatorStatusItem.hide();
}
notifyIsConnectedToPostgres(this.isPostgresEnabled.value);
}),

Expand Down Expand Up @@ -84,10 +89,9 @@ export class DataConnectEmulatorController implements vscode.Disposable {

// configure the emulator to use the local psql string
const emulatorClient = new DataConnectEmulatorClient();
emulatorClient.configureEmulator({ connectionString: newConnectionString });

this.isPostgresEnabled.value = true;
this.emulatorsController.emulatorStatusItem.show();

emulatorClient.configureEmulator({ connectionString: newConnectionString });
}

dispose() {
Expand Down
6 changes: 5 additions & 1 deletion firebase-vscode/src/data-connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ export function registerFdc(
vscode.commands.executeCommand(
"firebase.dataConnect.executeIntrospection",
);
runEmulatorIssuesStream(configs, emulatorController.getLocalEndpoint().value);
runEmulatorIssuesStream(
configs,
emulatorController.getLocalEndpoint().value,
fdcEmulatorsController.isPostgresEnabled,
);
runDataConnectCompiler(configs, emulatorController.getLocalEndpoint().value);
}
}),
Expand Down
4 changes: 2 additions & 2 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "firebase-tools",
"version": "13.11.0",
"version": "13.11.1",
"description": "Command-Line Interface for Firebase",
"main": "./lib/index.js",
"bin": {
Expand Down
4 changes: 3 additions & 1 deletion scripts/build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
FROM node:20

# Install dependencies
RUN curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
RUN echo "deb https://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
RUN apt-get update && \
apt-get install -y curl git jq
apt-get install -y curl git jq google-cloud-cli

# Install hub
RUN curl -fsSL --output hub.tgz https://github.com/github/hub/releases/download/v2.14.2/hub-linux-amd64-2.14.2.tgz
Expand Down
7 changes: 4 additions & 3 deletions scripts/publish-vsce.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ elif [[ ! ($VERSION == "patch" || $VERSION == "minor" || $VERSION == "major") ]]
fi

cd firebase-vscode
echo "Running npm install for VSCode..."
npm install
echo "Ran npm install for VSCode."

echo "Making a $VERSION version of VSCode..."
npm version $VERSION
Expand All @@ -35,6 +32,10 @@ NEW_HEADER="## NEXT \n\n## $NEW_VSCODE_VERSION\n\n- Updated internal firebase-to
sed -i -e "s/$NEXT_HEADER/$NEW_HEADER/g" CHANGELOG.md
echo "Made a $VERSION version of VSCode."

echo "Running npm install for VSCode..."
npm install
echo "Ran npm install for VSCode."

echo "Building firebase-vscode .VSIX file"
npm run pkg
echo "Built firebase-vscode .VSIX file."
Expand Down
6 changes: 5 additions & 1 deletion scripts/publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ trap - ERR
trap "echo 'Missing jq.'; exit 1" ERR
which jq &> /dev/null
trap - ERR

trap "echo 'Missing gsutil.'; exit 1" ERR
which gsutil &> /dev/null
trap - ERR
echo "Checked for commands."

echo "Checking for logged-in npm user..."
Expand Down Expand Up @@ -109,7 +113,7 @@ echo "Published to npm."
echo "Cleaning up release notes..."
rm CHANGELOG.md
touch CHANGELOG.md
git commit -m "[firebase-release] Removed change log and reset repo after ${NEW_VERSION} release" CHANGELOG.md
git commit -m "[firebase-release] Removed change log and reset repo after ${NEW_VERSION} release" CHANGELOG.md firebase-vscode/CHANGELOG.md firebase-vscode/package.json firebase-vscode/package-lock.json
echo "Cleaned up release notes."

echo "Pushing to GitHub..."
Expand Down
23 changes: 1 addition & 22 deletions src/dataconnect/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { dataconnectOrigin } from "../api";
import { Client } from "../apiv2";
import * as operationPoller from "../operation-poller";
import * as types from "./types";
import { logger } from "../logger";

const DATACONNECT_API_VERSION = "v1alpha";
const dataconnectClient = () =>
Expand All @@ -25,28 +24,8 @@ export async function listLocations(projectId: string): Promise<string[]> {

/** Service methods */
export async function listAllServices(projectId: string): Promise<types.Service[]> {
const locations = await listLocations(projectId);
let services: types.Service[] = [];
await Promise.all(
locations.map(async (l) => {
try {
const locationServices = await listServices(projectId, l);
services = services.concat(locationServices);
} catch (err) {
logger.debug(`Unable to listServices in ${l}: ${err}`);
}
}),
);

return services;
}

export async function listServices(
projectId: string,
locationId: string,
): Promise<types.Service[]> {
const res = await dataconnectClient().get<{ services: types.Service[] }>(
`/projects/${projectId}/locations/${locationId}/services`,
`/projects/${projectId}/locations/-/services`,
);
return res.body.services ?? [];
}
Expand Down
27 changes: 21 additions & 6 deletions src/dataconnect/provisionCloudSql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,19 @@ export async function provisionCloudSql(args: {
instanceId: string;
databaseId: string;
enableGoogleMlIntegration: boolean;
waitForCreation: boolean;
silent?: boolean;
}): Promise<string> {
let connectionName: string; // Not used yet, will be used for schema migration
const { projectId, locationId, instanceId, databaseId, enableGoogleMlIntegration, silent } = args;
let connectionName = ""; // Not used yet, will be used for schema migration
const {
projectId,
locationId,
instanceId,
databaseId,
enableGoogleMlIntegration,
waitForCreation,
silent,
} = args;
try {
const existingInstance = await cloudSqlAdminClient.getInstance(projectId, instanceId);
silent || utils.logLabeledBullet("dataconnect", `Found existing instance ${instanceId}.`);
Expand Down Expand Up @@ -59,21 +68,27 @@ export async function provisionCloudSql(args: {
silent ||
utils.logLabeledBullet(
"dataconnect",
`CloudSQL instance '${instanceId}' not found, creating it. This instance is provided under the terms of the Data Connect free trial ${freeTrialTermsLink()}`,
`CloudSQL instance '${instanceId}' not found, creating it.` +
`\nThis instance is provided under the terms of the Data Connect free trial ${freeTrialTermsLink()}` +
`\nMonitor the progress at ${cloudSqlAdminClient.instanceConsoleLink(projectId, instanceId)}`,
);
silent || utils.logLabeledBullet("dataconnect", `This may take while...`);
const newInstance = await promiseWithSpinner(
() =>
cloudSqlAdminClient.createInstance(
projectId,
locationId,
instanceId,
enableGoogleMlIntegration,
waitForCreation,
),
"Creating your instance...",
);
silent || utils.logLabeledBullet("dataconnect", "Instance created");
connectionName = newInstance?.connectionName || "";
if (newInstance) {
silent || utils.logLabeledBullet("dataconnect", "Instance created");
connectionName = newInstance?.connectionName || "";
} else {
silent || utils.logLabeledBullet("dataconnect", "Instance creation process started");
}
}
try {
await cloudSqlAdminClient.getDatabase(projectId, instanceId, databaseId);
Expand Down
1 change: 1 addition & 0 deletions src/deploy/dataconnect/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export default async function (
instanceId,
databaseId,
enableGoogleMlIntegration,
waitForCreation: true,
});
}),
);
Expand Down
2 changes: 1 addition & 1 deletion src/emulator/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ export async function startAll(
});
await startEmulator(dataConnectEmulator);
if (!utils.isVSCodeExtension()) {
dataConnectEmulator.connectToPostgres();
await dataConnectEmulator.connectToPostgres();
}
}

Expand Down
22 changes: 16 additions & 6 deletions src/emulator/dataconnectEmulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,23 @@ type ConfigureEmulatorRequest = {
};

export class DataConnectEmulatorClient {
private readonly client: Client;
constructor() {
this.client = EmulatorRegistry.client(Emulators.DATACONNECT);
}
private client: Client | undefined = undefined;

public async configureEmulator(body: ConfigureEmulatorRequest): Promise<ClientResponse<void>> {
const res = await this.client.post<ConfigureEmulatorRequest, void>("emulator/configure", body);
return res;
if (!this.client) {
this.client = EmulatorRegistry.client(Emulators.DATACONNECT);
}
try {
const res = await this.client.post<ConfigureEmulatorRequest, void>(
"emulator/configure",
body,
);
return res;
} catch (err: any) {
if (err.status === 500) {
throw new FirebaseError(`Data Connect emulator: ${err.context.body.message}`);
}
throw err;
}
}
}
17 changes: 16 additions & 1 deletion src/gcp/cloudsql/cloudsqladmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Client } from "../../apiv2";
import { cloudSQLAdminOrigin } from "../../api";
import * as operationPoller from "../../operation-poller";
import { Instance, Database, User, UserType, DatabaseFlag } from "./types";
import { FirebaseError } from "../../error";
const API_VERSION = "v1";

const client = new Client({
Expand All @@ -22,15 +23,26 @@ export async function listInstances(projectId: string): Promise<Instance[]> {

export async function getInstance(projectId: string, instanceId: string): Promise<Instance> {
const res = await client.get<Instance>(`projects/${projectId}/instances/${instanceId}`);
if (res.body.state === "FAILED") {
throw new FirebaseError(
`Cloud SQL instance ${instanceId} is in a failed state.\nGo to ${instanceConsoleLink(projectId, instanceId)} to repair or delete it.`,
);
}
return res.body;
}

/** Returns a link to Cloud SQL's page in Cloud Console. */
export function instanceConsoleLink(projectId: string, instanceId: string) {
return `https://console.cloud.google.com/sql/instances/${instanceId}/overview?project=${projectId}`;
}

export async function createInstance(
projectId: string,
location: string,
instanceId: string,
enableGoogleMlIntegration: boolean,
): Promise<Instance> {
waitForCreation: boolean,
): Promise<Instance | undefined> {
const databaseFlags = [{ name: "cloudsql.iam_authentication", value: "on" }];
if (enableGoogleMlIntegration) {
databaseFlags.push({ name: "cloudsql.enable_google_ml_integration", value: "on" });
Expand All @@ -56,6 +68,9 @@ export async function createInstance(
},
},
});
if (!waitForCreation) {
return;
}
const opName = `projects/${projectId}/operations/${op.body.name}`;
const pollRes = await operationPoller.pollOperation<Instance>({
apiOrigin: cloudSQLAdminOrigin(),
Expand Down
Loading

0 comments on commit 1921141

Please sign in to comment.