Skip to content

Commit

Permalink
fix: throw error when conflicting connection settings for same instan…
Browse files Browse the repository at this point in the history
…ce (#84)

Co-authored-by: Ruy Adorno <ruyadorno@google.com>
  • Loading branch information
shubha-rajan and ruyadorno committed Apr 13, 2023
1 parent 3b5fe2b commit 1c7b3d1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ class CloudSQLInstanceMap extends Map {
// in case an instance to that connection name has already
// been setup there's no need to set it up again
if (this.has(instanceConnectionName)) {
const instance = this.get(instanceConnectionName);
if (instance.ipType !== ipType) {
throw new CloudSQLConnectorError({
message:
`getOptions called for instance ${instanceConnectionName} with ipType ${ipType}, ` +
`but was previously called with ipType ${instance.ipType}. ` +
'If you require both for your use case, please use a new connector object.',
code: 'EBADINSTANCEINFO',
});
}
return;
}
const connectionInstance = await CloudSQLInstance.getCloudSQLInstance({
Expand All @@ -73,13 +83,27 @@ class CloudSQLInstanceMap extends Map {
this.set(instanceConnectionName, connectionInstance);
}

getInstance(instanceConnectionName: string): CloudSQLInstance {
getInstance({
instanceConnectionName,
ipType,
}: {
instanceConnectionName: string;
ipType: IpAdressesTypes;
}): CloudSQLInstance {
const connectionInstance = this.get(instanceConnectionName);
if (!connectionInstance) {
throw new CloudSQLConnectorError({
message: `Cannot find info for instance: ${instanceConnectionName}`,
code: 'ENOINSTANCEINFO',
});
} else if (connectionInstance.ipType !== ipType) {
throw new CloudSQLConnectorError({
message:
`getOptions called for instance ${instanceConnectionName} with ipType ${ipType}, ` +
`but was previously called with ipType ${connectionInstance.ipType}. ` +
'If you require both for your use case, please use a new connector object.',
code: 'EBADINSTANCEINFO',
});
}
return connectionInstance;
}
Expand Down Expand Up @@ -134,7 +158,7 @@ export class Connector {
return {
stream() {
const {instanceInfo, ephemeralCert, host, privateKey, serverCaCert} =
instances.getInstance(instanceConnectionName);
instances.getInstance({instanceConnectionName, ipType});

if (
instanceInfo &&
Expand Down
3 changes: 3 additions & 0 deletions test/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ t.test('start only a single instance info per connection name', async t => {
throw new Error('should only initialize once');
}
hasInstance = true;
return {
ipType: 'PUBLIC',
};
},
},
},
Expand Down

0 comments on commit 1c7b3d1

Please sign in to comment.