Skip to content

Commit

Permalink
implement terraform service-discovery caching
Browse files Browse the repository at this point in the history
  • Loading branch information
secustor committed Jul 11, 2020
1 parent 482412e commit 614f891
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
36 changes: 30 additions & 6 deletions lib/datasource/terraform-module/index.ts
Expand Up @@ -50,6 +50,33 @@ export interface ServiceDiscoveryResult {
'providers.v1'?: string;
}

export async function getTerraformServiceDiscoveryResult(
registryUrl: string
): Promise<ServiceDiscoveryResult> {
const discoveryURL = `${registryUrl}/.well-known/terraform.json`;
const cacheNamespace = 'terraform-service-discovery';
const cachedResult = await packageCache.get<ServiceDiscoveryResult>(
cacheNamespace,
registryUrl
);
// istanbul ignore if
if (cachedResult) {
return cachedResult;
}
const serviceDiscovery = (
await http.getJson<ServiceDiscoveryResult>(discoveryURL)
).body;

const cacheMinutes = 1440; // 24h
await packageCache.set(
cacheNamespace,
registryUrl,
serviceDiscovery,
cacheMinutes
);

return serviceDiscovery;
}
/**
* terraform.getReleases
*
Expand Down Expand Up @@ -80,12 +107,9 @@ export async function getReleases({
return cachedResult;
}
try {
const serviceDiscovery = (
await http.getJson<ServiceDiscoveryResult>(
`${registryUrl}/.well-known/terraform.json`
)
).body;

const serviceDiscovery = await getTerraformServiceDiscoveryResult(
registryUrl
);
const pkgUrl = `${registry}${serviceDiscovery['modules.v1']}${repository}`;
const res = (await http.getJson<TerraformRelease>(pkgUrl)).body;
const returnedName = res.namespace + '/' + res.name + '/' + res.provider;
Expand Down
10 changes: 4 additions & 6 deletions lib/datasource/terraform-provider/index.ts
Expand Up @@ -3,7 +3,7 @@ import { logger } from '../../logger';
import * as packageCache from '../../util/cache/package';
import { Http } from '../../util/http';
import { GetReleasesConfig, ReleaseResult } from '../common';
import { ServiceDiscoveryResult } from '../terraform-module';
import { getTerraformServiceDiscoveryResult } from '../terraform-module';

export const id = 'terraform-provider';
export const defaultRegistryUrls = [
Expand Down Expand Up @@ -38,11 +38,9 @@ async function queryRegistry(
registryURL: string,
repository: string
): Promise<ReleaseResult> {
const serviceDiscovery = (
await http.getJson<ServiceDiscoveryResult>(
`${registryURL}/.well-known/terraform.json`
)
).body;
const serviceDiscovery = await getTerraformServiceDiscoveryResult(
registryURL
);
const backendURL = `${registryURL}${serviceDiscovery['providers.v1']}${repository}`;
const res = (await http.getJson<TerraformProvider>(backendURL)).body;
const dep: ReleaseResult = {
Expand Down

0 comments on commit 614f891

Please sign in to comment.