Skip to content

Commit

Permalink
Adding better handling for CloudSQl allowlist errors (#7770)
Browse files Browse the repository at this point in the history
  • Loading branch information
joehan authored Sep 30, 2024
1 parent 85d7c27 commit 0cd8576
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions src/gcp/cloudsql/cloudsqladmin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client } from "../../apiv2";
import { Client, ClientResponse } from "../../apiv2";
import { cloudSQLAdminOrigin } from "../../api";
import * as operationPoller from "../../operation-poller";
import { Instance, Database, User, UserType, DatabaseFlag } from "./types";
Expand Down Expand Up @@ -47,27 +47,33 @@ export async function createInstance(
if (enableGoogleMlIntegration) {
databaseFlags.push({ name: "cloudsql.enable_google_ml_integration", value: "on" });
}
const op = await client.post<Partial<Instance>, Operation>(`projects/${projectId}/instances`, {
name: instanceId,
region: location,
databaseVersion: "POSTGRES_15",
settings: {
tier: "db-f1-micro",
edition: "ENTERPRISE",
ipConfiguration: {
authorizedNetworks: [],
},
enableGoogleMlIntegration,
databaseFlags,
storageAutoResize: false,
userLabels: { "firebase-data-connect": "ft" },
insightsConfig: {
queryInsightsEnabled: true,
queryPlansPerMinute: 5, // Match the default settings
queryStringLength: 1024, // Match the default settings
let op: ClientResponse<Operation>;
try {
op = await client.post<Partial<Instance>, Operation>(`projects/${projectId}/instances`, {
name: instanceId,
region: location,
databaseVersion: "POSTGRES_15",
settings: {
tier: "db-f1-micro",
edition: "ENTERPRISE",
ipConfiguration: {
authorizedNetworks: [],
},
enableGoogleMlIntegration,
databaseFlags,
storageAutoResize: false,
userLabels: { "firebase-data-connect": "ft" },
insightsConfig: {
queryInsightsEnabled: true,
queryPlansPerMinute: 5, // Match the default settings
queryStringLength: 1024, // Match the default settings
},
},
},
});
});
} catch (err: any) {
handleAllowlistError(err, location);
throw err;
}
if (!waitForCreation) {
return;
}
Expand Down Expand Up @@ -123,6 +129,14 @@ export async function updateInstanceForDataConnect(
return pollRes;
}

function handleAllowlistError(err: any, region: string) {
if (err.message.includes("Not allowed to set system label: firebase-data-connect")) {
throw new FirebaseError(
`Cloud SQL free trial instances are not yet available in ${region}. Please check https://firebase.google.com/docs/data-connect/ for a full list of available regions.`,
);
}
}

function setDatabaseFlag(flag: DatabaseFlag, flags: DatabaseFlag[] = []): DatabaseFlag[] {
const temp = flags.filter((f) => f.name !== flag.name);
temp.push(flag);
Expand Down

0 comments on commit 0cd8576

Please sign in to comment.