Skip to content

Commit

Permalink
Temporary fix for firebase init dataconnect with Spark projects. (#7704)
Browse files Browse the repository at this point in the history
Co-authored-by: joehan <[email protected]>
  • Loading branch information
rosalyntan and joehan authored Sep 23, 2024
1 parent e0089d6 commit 79152ce
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 58 deletions.
1 change: 0 additions & 1 deletion src/dataconnect/ensureApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ export async function ensureApis(projectId: string): Promise<void> {
export async function ensureSparkApis(projectId: string): Promise<void> {
const prefix = "dataconnect";
// These are the APIs that can be enabled without a billing account.
await ensure(projectId, api.dataconnectOrigin(), prefix);
await ensure(projectId, api.cloudSQLAdminOrigin(), prefix);
}
114 changes: 57 additions & 57 deletions src/init/features/dataconnect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,66 +228,66 @@ async function promptForService(
if (isBillingEnabled) {
// Enabling compute.googleapis.com requires a Blaze plan.
await ensureApis(setup.projectId);
} else {
await ensureSparkApis(setup.projectId);
}
// TODO (b/344021748): Support initing with services that have existing sources/files
const existingServices = await listAllServices(setup.projectId);
const existingServicesAndSchemas = await Promise.all(
existingServices.map(async (s) => {
return {
service: s,
schema: await getSchema(s.name),
};
}),
);
if (existingServicesAndSchemas.length) {
const choices: { name: string; value: any }[] = 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 } = 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;
info.locationId = serviceName.location;
if (choice.schema) {
const primaryDatasource = choice.schema.datasources.find((d) => d.postgresql);
if (primaryDatasource?.postgresql?.cloudSql.instance) {
const instanceName = parseCloudSQLInstanceName(
primaryDatasource.postgresql.cloudSql.instance,
);
info.cloudSqlInstanceId = instanceName.instanceId;
}
if (choice.schema.source.files) {
info.schemaGql = choice.schema.source.files;
}
info.cloudSqlDatabase = primaryDatasource?.postgresql?.database ?? "";
const connectors = await listConnectors(choice.service.name, [
"connectors.name",
"connectors.source.files",
]);
if (connectors.length) {
info.connectors = connectors.map((c) => {
const id = c.name.split("/").pop()!;
return {
id,
path: connectors.length === 1 ? "./connector" : `./${id}`,
files: c.source.files || [],
};
});
// TODO (b/344021748): Support initing with services that have existing sources/files
const existingServices = await listAllServices(setup.projectId);
const existingServicesAndSchemas = await Promise.all(
existingServices.map(async (s) => {
return {
service: s,
schema: await getSchema(s.name),
};
}),
);
if (existingServicesAndSchemas.length) {
const choices: { name: string; value: any }[] = 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 } = 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;
info.locationId = serviceName.location;
if (choice.schema) {
const primaryDatasource = choice.schema.datasources.find((d) => d.postgresql);
if (primaryDatasource?.postgresql?.cloudSql.instance) {
const instanceName = parseCloudSQLInstanceName(
primaryDatasource.postgresql.cloudSql.instance,
);
info.cloudSqlInstanceId = instanceName.instanceId;
}
if (choice.schema.source.files) {
info.schemaGql = choice.schema.source.files;
}
info.cloudSqlDatabase = primaryDatasource?.postgresql?.database ?? "";
const connectors = await listConnectors(choice.service.name, [
"connectors.name",
"connectors.source.files",
]);
if (connectors.length) {
info.connectors = connectors.map((c) => {
const id = c.name.split("/").pop()!;
return {
id,
path: connectors.length === 1 ? "./connector" : `./${id}`,
files: c.source.files || [],
};
});
}
}
}
}
} else {
await ensureSparkApis(setup.projectId);
}
}

Expand Down

0 comments on commit 79152ce

Please sign in to comment.