Skip to content

Commit

Permalink
chore: refactor into private method
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwotherspoon committed Sep 25, 2024
1 parent df3e8c6 commit 698bc5b
Showing 1 changed file with 39 additions and 25 deletions.
64 changes: 39 additions & 25 deletions src/sqladmin-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,40 @@ export class SQLAdminFetcher {
}
}

private parseIpAddresses(
ipResponse: sqladmin_v1beta4.Schema$IpMapping[] | undefined,
dnsName: string | null | undefined,
pscEnabled: boolean | 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;
}
}
}

// Resolve dnsName into IP address for PSC enabled instances.
// Note that we have to check for PSC enablement because CAS instances
// also set the dnsName field.
if (dnsName && pscEnabled) {
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;
}

async getInstanceMetadata({
projectId,
regionId,
Expand All @@ -146,31 +180,11 @@ export class SQLAdminFetcher {
});
}

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 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 ipAddresses = this.parseIpAddresses(
res.data.ipAddresses,
res.data.dnsName,
res.data.pscEnabled
);

const {serverCaCert} = res.data;
if (!serverCaCert || !serverCaCert.cert || !serverCaCert.expirationTime) {
Expand Down

0 comments on commit 698bc5b

Please sign in to comment.