2021-06-14 06:13:10 +00:00
|
|
|
import { HelmDatasource } from '../../datasource/helm';
|
2020-07-02 17:20:30 +00:00
|
|
|
import { SkipReason } from '../../types';
|
2020-07-28 10:27:17 +00:00
|
|
|
import { getDep } from '../dockerfile/extract';
|
2021-03-02 20:44:55 +00:00
|
|
|
import type { PackageDependency } from '../types';
|
2021-05-11 17:08:02 +00:00
|
|
|
import { TerraformDependencyTypes, TerraformResourceTypes } from './common';
|
|
|
|
import type { ExtractionResult, ResourceManagerData } from './types';
|
2020-07-02 17:20:30 +00:00
|
|
|
import {
|
|
|
|
checkIfStringIsPath,
|
|
|
|
keyValueExtractionRegex,
|
2020-07-28 10:27:17 +00:00
|
|
|
resourceTypeExtractionRegex,
|
2020-07-02 17:20:30 +00:00
|
|
|
} from './util';
|
|
|
|
|
2020-07-28 10:27:17 +00:00
|
|
|
function applyDockerDependency(
|
|
|
|
dep: PackageDependency<ResourceManagerData>,
|
|
|
|
value: string
|
|
|
|
): void {
|
|
|
|
const dockerDep = getDep(value);
|
|
|
|
Object.assign(dep, dockerDep);
|
|
|
|
}
|
|
|
|
|
2020-07-02 17:20:30 +00:00
|
|
|
export function extractTerraformResource(
|
|
|
|
startingLine: number,
|
|
|
|
lines: string[]
|
|
|
|
): ExtractionResult {
|
|
|
|
let lineNumber = startingLine;
|
2020-07-28 10:27:17 +00:00
|
|
|
let line = lines[lineNumber];
|
2020-07-02 17:20:30 +00:00
|
|
|
const deps: PackageDependency[] = [];
|
2020-07-28 10:27:17 +00:00
|
|
|
const dep: PackageDependency<ResourceManagerData> = {
|
2020-07-02 17:20:30 +00:00
|
|
|
managerData: {
|
|
|
|
terraformDependencyType: TerraformDependencyTypes.resource,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-07-28 10:27:17 +00:00
|
|
|
const typeMatch = resourceTypeExtractionRegex.exec(line);
|
|
|
|
|
|
|
|
dep.managerData.resourceType =
|
|
|
|
TerraformResourceTypes[typeMatch?.groups?.type] ??
|
|
|
|
TerraformResourceTypes.unknown;
|
|
|
|
|
2020-07-02 17:20:30 +00:00
|
|
|
do {
|
|
|
|
lineNumber += 1;
|
|
|
|
line = lines[lineNumber];
|
|
|
|
const kvMatch = keyValueExtractionRegex.exec(line);
|
|
|
|
if (kvMatch) {
|
2020-07-28 10:27:17 +00:00
|
|
|
switch (kvMatch.groups.key) {
|
|
|
|
case 'chart':
|
|
|
|
case 'image':
|
|
|
|
case 'name':
|
|
|
|
case 'repository':
|
|
|
|
dep.managerData[kvMatch.groups.key] = kvMatch.groups.value;
|
|
|
|
break;
|
|
|
|
case 'version':
|
|
|
|
dep.currentValue = kvMatch.groups.value;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* istanbul ignore next */
|
|
|
|
break;
|
2020-07-02 17:20:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (line.trim() !== '}');
|
|
|
|
deps.push(dep);
|
|
|
|
return { lineNumber, dependencies: deps };
|
|
|
|
}
|
|
|
|
|
2020-07-28 10:27:17 +00:00
|
|
|
export function analyseTerraformResource(
|
|
|
|
dep: PackageDependency<ResourceManagerData>
|
|
|
|
): void {
|
|
|
|
switch (dep.managerData.resourceType) {
|
|
|
|
case TerraformResourceTypes.docker_container:
|
2021-03-04 05:21:55 +00:00
|
|
|
if (dep.managerData.image) {
|
2020-07-28 10:27:17 +00:00
|
|
|
applyDockerDependency(dep, dep.managerData.image);
|
2021-04-04 12:48:52 +00:00
|
|
|
dep.depType = 'docker_container';
|
2021-03-04 05:21:55 +00:00
|
|
|
} else {
|
|
|
|
dep.skipReason = SkipReason.InvalidDependencySpecification;
|
2020-07-28 10:27:17 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TerraformResourceTypes.docker_image:
|
2021-03-04 05:21:55 +00:00
|
|
|
if (dep.managerData.name) {
|
2020-07-28 10:27:17 +00:00
|
|
|
applyDockerDependency(dep, dep.managerData.name);
|
2021-04-04 12:48:52 +00:00
|
|
|
dep.depType = 'docker_image';
|
2021-03-04 05:21:55 +00:00
|
|
|
} else {
|
|
|
|
dep.skipReason = SkipReason.InvalidDependencySpecification;
|
2020-07-28 10:27:17 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TerraformResourceTypes.docker_service:
|
2021-03-04 05:21:55 +00:00
|
|
|
if (dep.managerData.image) {
|
2020-07-28 10:27:17 +00:00
|
|
|
applyDockerDependency(dep, dep.managerData.image);
|
2021-04-04 12:48:52 +00:00
|
|
|
dep.depType = 'docker_service';
|
2021-03-04 05:21:55 +00:00
|
|
|
} else {
|
|
|
|
dep.skipReason = SkipReason.InvalidDependencySpecification;
|
2020-07-28 10:27:17 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TerraformResourceTypes.helm_release:
|
2021-11-09 07:02:59 +00:00
|
|
|
if (!dep.managerData.chart) {
|
2020-07-28 10:27:17 +00:00
|
|
|
dep.skipReason = SkipReason.InvalidName;
|
|
|
|
} else if (checkIfStringIsPath(dep.managerData.chart)) {
|
|
|
|
dep.skipReason = SkipReason.LocalChart;
|
|
|
|
}
|
2021-04-04 12:48:52 +00:00
|
|
|
dep.depType = 'helm_release';
|
2020-07-28 10:27:17 +00:00
|
|
|
dep.registryUrls = [dep.managerData.repository];
|
|
|
|
dep.depName = dep.managerData.chart;
|
2021-06-14 06:13:10 +00:00
|
|
|
dep.datasource = HelmDatasource.id;
|
2020-07-28 10:27:17 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-02-18 07:24:08 +00:00
|
|
|
dep.skipReason = SkipReason.InvalidValue;
|
2020-07-28 10:27:17 +00:00
|
|
|
break;
|
2020-07-02 17:20:30 +00:00
|
|
|
}
|
|
|
|
}
|