Skip to content
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

fix: enable IP type switching #338

Merged
merged 2 commits into from
Apr 26, 2024
Merged

fix: enable IP type switching #338

merged 2 commits into from
Apr 26, 2024

Conversation

jackwotherspoon
Copy link
Collaborator

@jackwotherspoon jackwotherspoon commented Apr 26, 2024

When the metadata of a Cloud SQL instance is fetched via the connectSettings API, the connector caches all IP addresses and types. Thus it has the information required to connect via either Public or Private IP.

The Node Connector currently incorrectly errors when a user switches IP types between consecutive calls... this PR removes the incorrect erroring and allows for the following usage:

import mysql from 'mysql2/promise';
import { Connector } from '@google-cloud/cloud-sql-connector';

const connector = new Connector();

// establish connection via Public IP
var clientOpts = await connector.getOptions({
    instanceConnectionName: 'my-project:us-central1:my-instance',
    ipType: 'PUBLIC',
});

const conn = await mysql.createConnection({
    ...clientOpts,
    user: 'root',
    password: 'my-password',
    database: 'my-database',
});

const [result] = await conn.query(`SELECT NOW();`);

// establish connection via Private IP
var clientOpts = await connector.getOptions({
    instanceConnectionName: 'my-project:us-central1:my-instance',
    ipType: 'PRIVATE',
});

const conn2 = await mysql.createConnection({
    ...clientOpts,
    user: 'root',
    password: 'my-password',
    database: 'my-database',
});
const [result2] = await conn.query(`SELECT NOW();`);

connector.close()

Fixes #305

@jackwotherspoon jackwotherspoon self-assigned this Apr 26, 2024
@jackwotherspoon jackwotherspoon requested a review from a team as a code owner April 26, 2024 13:54
@jackwotherspoon jackwotherspoon merged commit 450f2f5 into main Apr 26, 2024
22 checks passed
@jackwotherspoon jackwotherspoon deleted the ip-type-switch branch April 26, 2024 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Connect should not error on ipType switching
2 participants