Skip to content

Added env var to magically import data connect service from console #8237

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

Merged
merged 4 commits into from
Feb 20, 2025
Merged
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
48 changes: 33 additions & 15 deletions src/init/features/dataconnect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import { parseCloudSQLInstanceName, parseServiceName } from "../../../dataconnect/names";
import { logger } from "../../../logger";
import { readTemplateSync } from "../../../templates";
import { logBullet } from "../../../utils";
import { logBullet, envOverride } from "../../../utils";
import { checkBillingEnabled } from "../../../gcp/cloudbilling";
import * as sdk from "./sdk";
import { getPlatformFromFolder } from "../../../dataconnect/fileUtils";
Expand All @@ -30,6 +30,11 @@
const QUERIES_TEMPLATE = readTemplateSync("init/dataconnect/queries.gql");
const MUTATIONS_TEMPLATE = readTemplateSync("init/dataconnect/mutations.gql");

// serviceEnvVar is used by Firebase Console to specify which service to import.
// It should be in the form <location>/<serviceId>
// It must be an existing service - if set to anything else, we'll ignore it.
const serviceEnvVar = () => envOverride("FDC_SERVICE", "");

Check warning on line 36 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function

export interface RequiredInfo {
serviceId: string;
locationId: string;
Expand Down Expand Up @@ -70,7 +75,7 @@
const defaultSchema = { path: "schema.gql", content: SCHEMA_TEMPLATE };

// doSetup is split into 2 phases - ask questions and then actuate files and API calls based on those answers.
export async function doSetup(setup: Setup, config: Config): Promise<void> {

Check warning on line 78 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
const isBillingEnabled = setup.projectId ? await checkBillingEnabled(setup.projectId) : false;
if (setup.projectId) {
isBillingEnabled ? await ensureApis(setup.projectId) : await ensureSparkApis(setup.projectId);
Expand All @@ -78,8 +83,8 @@
const info = await askQuestions(setup, isBillingEnabled);
// Most users will want to perist data between emulator runs, so set this to a reasonable default.

const dir: string = config.get("dataconnect.source", "dataconnect");

Check warning on line 86 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
const dataDir = config.get("emulators.dataconnect.dataDir", `${dir}/.dataconnect/pgliteData`);

Check warning on line 87 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
config.set("emulators.dataconnect.dataDir", dataDir);
await actuate(setup, config, info);

Expand Down Expand Up @@ -154,7 +159,7 @@

// actuate writes product specific files and makes product specifc API calls.
// It does not handle writing firebase.json and .firebaserc
export async function actuate(setup: Setup, config: Config, info: RequiredInfo) {

Check warning on line 162 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function

Check warning on line 162 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
await writeFiles(config, info);

if (setup.projectId && info.shouldProvisionCSQL) {
Expand All @@ -163,15 +168,15 @@
locationId: info.locationId,
instanceId: info.cloudSqlInstanceId,
databaseId: info.cloudSqlDatabase,
configYamlPath: join(config.get("dataconnect.source"), "dataconnect.yaml"),

Check warning on line 171 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe argument of type `any` assigned to a parameter of type `string`
enableGoogleMlIntegration: false,
waitForCreation: false,
});
}
}

async function writeFiles(config: Config, info: RequiredInfo) {

Check warning on line 178 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function
const dir: string = config.get("dataconnect.source") || "dataconnect";

Check warning on line 179 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
const subbedDataconnectYaml = subDataconnectYamlValues({
...info,
connectorDirs: info.connectors.map((c) => c.path),
Expand Down Expand Up @@ -210,7 +215,7 @@
}
}

async function writeConnectorFiles(

Check warning on line 218 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function
config: Config,
connectorInfo: {
id: string;
Expand Down Expand Up @@ -282,21 +287,34 @@
}),
);
if (existingServicesAndSchemas.length) {
const choices: { name: string; value: { service: Service; schema?: Schema } | undefined }[] =
existingServicesAndSchemas.map((s) => {
const serviceName = parseServiceName(s.service.name);
return {
name: `${serviceName.location}/${serviceName.serviceId}`,
value: s,
};
});
choices.push({ name: "Create a new service", value: undefined });
const choice: { service: Service; schema?: Schema } | undefined = await promptOnce({
message:
"Your project already has existing services. Which would you like to set up local files for?",
type: "list",
choices,
let choice: { service: Service; schema?: Schema } | undefined;
const [serviceLocationFromEnvVar, serviceIdFromEnvVar] = serviceEnvVar().split("/");
const serviceFromEnvVar = existingServicesAndSchemas.find((s) => {
const serviceName = parseServiceName(s.service.name);
return (
serviceName.serviceId === serviceIdFromEnvVar &&
serviceName.location === serviceLocationFromEnvVar
);
});
if (serviceFromEnvVar) {
choice = serviceFromEnvVar;
} else {
const choices: { name: string; value: { service: Service; schema?: Schema } | undefined }[] =
existingServicesAndSchemas.map((s) => {
const serviceName = parseServiceName(s.service.name);
return {
name: `${serviceName.location}/${serviceName.serviceId}`,
value: s,
};
});
choices.push({ name: "Create a new service", value: undefined });
choice = await promptOnce({
message:
"Your project already has existing services. Which would you like to set up local files for?",
type: "list",
choices,
});
}
if (choice) {
const serviceName = parseServiceName(choice.service.name);
info.serviceId = serviceName.serviceId;
Expand Down
Loading