private parseIpAddresses()

in src/sqladmin-fetcher.ts [132:181]


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

    // Search the dns_names field for the PSC DNS Name.
    if (dnsNames) {
      for (const dnm of dnsNames) {
        if (
          dnm.name &&
          dnm.connectionType === 'PRIVATE_SERVICE_CONNECT' &&
          dnm.dnsScope === 'INSTANCE'
        ) {
          ipAddresses.psc = dnm.name;
          break;
        }
      }
    }

    // If the psc dns name was not found, use the legacy dns_name field
    if (!ipAddresses.psc && 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;
  }