async getInstanceMetadata()

in src/sqladmin-fetcher.ts [183:255]


  async getInstanceMetadata({
    projectId,
    regionId,
    instanceId,
  }: InstanceConnectionInfo): Promise<InstanceMetadata> {
    setupGaxiosConfig();

    const res = await this.client.connect.get({
      project: projectId,
      instance: instanceId,
    });

    if (!res.data) {
      throw new CloudSQLConnectorError({
        message:
          `Failed to find metadata on project id: ${projectId} ` +
          `and instance id: ${instanceId}. Ensure network connectivity and ` +
          'validate the provided `instanceConnectionName` config value',
        code: 'ENOSQLADMIN',
      });
    }

    const ipAddresses = this.parseIpAddresses(
      res.data.ipAddresses,
      res.data.dnsName,
      res.data.dnsNames,
      res.data.pscEnabled
    );

    const {serverCaCert} = res.data;
    if (!serverCaCert || !serverCaCert.cert || !serverCaCert.expirationTime) {
      throw new CloudSQLConnectorError({
        message: 'Cannot connect to instance, no valid CA certificate found',
        code: 'ENOSQLADMINCERT',
      });
    }

    const {region} = res.data;
    if (!region) {
      throw new CloudSQLConnectorError({
        message: 'Cannot connect to instance, no valid region found',
        code: 'ENOSQLADMINREGION',
      });
    }
    if (region !== regionId) {
      throw new CloudSQLConnectorError({
        message: `Provided region was mismatched. Got ${region}, want ${regionId}`,
        code: 'EBADSQLADMINREGION',
      });
    }

    cleanGaxiosConfig();
    // Find a DNS name to use to validate the certificate from the dns_names field. Any
    // name in the list may be used to validate the server TLS certificate.
    // Fall back to legacy dns_name field if necessary.
    let serverName = null;
    if (res.data.dnsNames && res.data.dnsNames.length > 0) {
      serverName = res.data.dnsNames[0].name;
    }
    if (serverName === null) {
      serverName = res.data.dnsName;
    }

    return {
      ipAddresses,
      serverCaCert: {
        cert: serverCaCert.cert,
        expirationTime: serverCaCert.expirationTime,
      },
      serverCaMode: res.data.serverCaMode || '',
      dnsName: serverName || '',
    };
  }