renovate/lib/modules/datasource/terraform-module/base.ts
Rhys Arkins dca3418bbd refactor: lib/modules (#14488)
Moves datasource, manager, platform and versioning code from lib/ into new lib/modules/

BREAKING CHANGE: External tools must update paths to datasource, manager, platform and versioning
2022-03-04 09:04:02 +01:00

43 lines
1.5 KiB
TypeScript

import { ExternalHostError } from '../../../types/errors/external-host-error';
import { cache } from '../../../util/cache/package/decorator';
import type { HttpError } from '../../../util/http';
import { ensureTrailingSlash } from '../../../util/url';
import { Datasource } from '../datasource';
import type { ServiceDiscoveryResult } from './types';
// TODO: extract to a separate directory structure (#10532)
export abstract class TerraformDatasource extends Datasource {
static id = 'terraform';
@cache({
namespace: `datasource-${TerraformDatasource.id}`,
key: (registryUrl: string) =>
TerraformDatasource.getDiscoveryUrl(registryUrl),
ttlMinutes: 1440,
})
async getTerraformServiceDiscoveryResult(
registryUrl: string
): Promise<ServiceDiscoveryResult> {
const discoveryURL = TerraformDatasource.getDiscoveryUrl(registryUrl);
const serviceDiscovery = (
await this.http.getJson<ServiceDiscoveryResult>(discoveryURL)
).body;
return serviceDiscovery;
}
private static getDiscoveryUrl(registryUrl: string): string {
return `${ensureTrailingSlash(registryUrl)}.well-known/terraform.json`;
}
override handleSpecificErrors(err: HttpError): void {
const failureCodes = ['EAI_AGAIN'];
// istanbul ignore if
if (failureCodes.includes(err.code)) {
throw new ExternalHostError(err);
}
// istanbul ignore if
if (err.response?.statusCode === 503) {
throw new ExternalHostError(err);
}
}
}