From 50e68b844111616f93f30f3f10e6375efa0a243d Mon Sep 17 00:00:00 2001 From: jackwotherspoon Date: Mon, 23 Sep 2024 13:51:19 +0000 Subject: [PATCH] fix: only add PSC ipType if PSC is enabled --- src/ip-addresses.ts | 31 ------------------------------- src/sqladmin-fetcher.ts | 29 +++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 35 deletions(-) diff --git a/src/ip-addresses.ts b/src/ip-addresses.ts index 9b0eea4f..973e0d14 100644 --- a/src/ip-addresses.ts +++ b/src/ip-addresses.ts @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {sqladmin_v1beta4} from '@googleapis/sqladmin'; import {CloudSQLConnectorError} from './errors'; export enum IpAddressTypes { @@ -57,36 +56,6 @@ const getPSCIpAddress = (ipAddresses: IpAddresses) => { return ipAddresses.psc; }; -export function parseIpAddresses( - ipResponse: sqladmin_v1beta4.Schema$IpMapping[] | undefined, - dnsName: string | null | undefined -): IpAddresses { - const ipAddresses: IpAddresses = {}; - if (ipResponse) { - for (const ip of ipResponse) { - if (ip.type === 'PRIMARY' && ip.ipAddress) { - ipAddresses.public = ip.ipAddress; - } - if (ip.type === 'PRIVATE' && ip.ipAddress) { - ipAddresses.private = ip.ipAddress; - } - } - } - - if (dnsName) { - ipAddresses.psc = dnsName; - } - - if (!ipAddresses.public && !ipAddresses.private && !ipAddresses.psc) { - throw new CloudSQLConnectorError({ - message: 'Cannot connect to instance, it has no supported IP addresses', - code: 'ENOSQLADMINIPADDRESS', - }); - } - - return ipAddresses; -} - export function selectIpAddress( ipAddresses: IpAddresses, type: IpAddressTypes | unknown diff --git a/src/sqladmin-fetcher.ts b/src/sqladmin-fetcher.ts index 7fbc28c3..4a227451 100644 --- a/src/sqladmin-fetcher.ts +++ b/src/sqladmin-fetcher.ts @@ -146,10 +146,31 @@ export class SQLAdminFetcher { }); } - const ipAddresses = parseIpAddresses( - res.data.ipAddresses, - res.data.dnsName - ); + const ipAddresses: IpAddresses = {}; + if (res.data.ipAddresses) { + for (const ip of res.data.ipAddresses) { + if (ip.type === 'PRIMARY' && ip.ipAddress) { + ipAddresses.public = ip.ipAddress; + } + if (ip.type === 'PRIVATE' && ip.ipAddress) { + ipAddresses.private = ip.ipAddress; + } + } + } + + // Resolve dnsName into IP address for PSC enabled instances. + // Note that we have to check for PSC enablement also because CAS instances + // also set the dnsName field. + if (res.data.dnsName && res.data.pscEnabled) { + ipAddresses.psc = res.data.dnsName; + } + + if (!ipAddresses.public && !ipAddresses.private && !ipAddresses.psc) { + throw new CloudSQLConnectorError({ + message: 'Cannot connect to instance, it has no supported IP addresses', + code: 'ENOSQLADMINIPADDRESS', + }); + } const {serverCaCert} = res.data; if (!serverCaCert || !serverCaCert.cert || !serverCaCert.expirationTime) {