Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(terraform): add support for terraform_version in tfe_workspace blocks #14522

Merged
merged 10 commits into from
Mar 14, 2022
11 changes: 11 additions & 0 deletions lib/modules/manager/terraform/__fixtures__/tfeWorkspace.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "tfe_workspace" "test_workspace" {
name = "test-workspace"
organization = "renovate-fixtures"

terraform_version = "1.1.6"
}

resource "tfe_workspace" "test_workspace" {
name = "test-workspace"
organization = "renovate-fixtures"
}
17 changes: 17 additions & 0 deletions lib/modules/manager/terraform/__snapshots__/extract.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,23 @@ Object {
}
`;

exports[`modules/manager/terraform/extract extractPackageFile() extracts terraform_version for tfe_workspace and ignores missing terraform_version keys 1`] = `
Object {
"deps": Array [
Object {
"currentValue": "1.1.6",
"datasource": "github-tags",
"depName": "hashicorp/terraform",
"depType": "tfe_workspace",
"extractVersion": "v(?<version>.*)$",
},
Object {
"skipReason": "no-version",
},
],
}
`;

exports[`modules/manager/terraform/extract extractPackageFile() test terraform block with only requirement_terraform_version 1`] = `
Object {
"deps": Array [
Expand Down
4 changes: 4 additions & 0 deletions lib/modules/manager/terraform/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ export enum TerraformResourceTypes {
* https://www.terraform.io/docs/providers/helm/r/release.html
*/
helm_release = 'helm_release',
/**
* https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/resources/workspace
*/
tfe_workspace = 'tfe_workspace',
}
12 changes: 12 additions & 0 deletions lib/modules/manager/terraform/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const helm = loadFixture('helm.tf');
const lockedVersion = loadFixture('lockedVersion.tf');
const lockedVersionLockfile = loadFixture('rangeStrategy.hcl');
const terraformBlock = loadFixture('terraformBlock.tf');
const tfeWorkspaceBlock = loadFixture('tfeWorkspace.tf');

const adminConfig: RepoGlobalConfig = {
// `join` fixes Windows CI
Expand Down Expand Up @@ -99,5 +100,16 @@ describe('modules/manager/terraform/extract', () => {
expect(res.deps.filter((dep) => dep.skipReason)).toHaveLength(0);
expect(res).toMatchSnapshot();
});

it('extracts terraform_version for tfe_workspace and ignores missing terraform_version keys', async () => {
const res = await extractPackageFile(
tfeWorkspaceBlock,
'tfeWorkspace.tf',
{}
);
expect(res).toMatchSnapshot();
expect(res.deps).toHaveLength(2);
expect(res.deps.filter((dep) => dep.skipReason)).toHaveLength(1);
});
});
});
1 change: 1 addition & 0 deletions lib/modules/manager/terraform/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const contentCheckList = [
' "helm_release" ',
' "docker_image" ',
'required_version',
'terraform_version', // part of tfe_workspace
];

export async function extractPackageFile(
Expand Down
22 changes: 12 additions & 10 deletions lib/modules/manager/terraform/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Currently, Terraform supports renovating the following dependencies, where sub p
- chart repository ( Public and Private )
- docker\_\*
- Docker registry ( Public and Private )
- [tfe_workspace](https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/resources/workspace) ( `terraform_version` argument )

Terraform range constraints are supported:

Expand All @@ -24,15 +25,16 @@ Terraform range constraints are supported:

For fine-grained control, e.g. to turn off only parts of this manager, you can use the following `depTypes`:

| resource | depType |
| --------------------------- | :-----------------: |
| Terraform provider | `provider` |
| required Terraform provider | `required_provider` |
| required Terraform version | `required_version` |
| Terraform module | `module` |
| Helm release | `helm_release` |
| Docker container | `docker_container` |
| Docker image | `docker_image` |
| Docker service | `docker_service` |
| resource | depType | Notes |
| --------------------------- | :-----------------: | :------------------------------------------------------------------------: |
| Terraform provider | `provider` | |
| required Terraform provider | `required_provider` | |
| required Terraform version | `required_version` | This handles the `required_version` in terraform blocks |
| TFE workspace | `tfe_workspace` | This handles the `terraform_version` argument in `tfe_workspace` resources |
| Terraform module | `module` | |
| Helm release | `helm_release` | |
| Docker container | `docker_container` | |
| Docker image | `docker_image` | |
| Docker service | `docker_service` | |

If you need to change the versioning format, read the [versioning](https://docs.renovatebot.com/modules/versioning/) documentation to learn more.
16 changes: 16 additions & 0 deletions lib/modules/manager/terraform/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HelmDatasource } from '../../datasource/helm';
import { getDep } from '../dockerfile/extract';
import type { PackageDependency } from '../types';
import { TerraformDependencyTypes, TerraformResourceTypes } from './common';
import { analyseTerraformVersion } from './required-version';
import type { ExtractionResult, ResourceManagerData } from './types';
import {
checkIfStringIsPath,
Expand Down Expand Up @@ -32,10 +33,15 @@ export function extractTerraformResource(

const typeMatch = resourceTypeExtractionRegex.exec(line);

// Sets the resourceType, e.g. "helm_release" 'resource "helm_release" "test_release"'
dep.managerData.resourceType =
TerraformResourceTypes[typeMatch?.groups?.type] ??
TerraformResourceTypes.unknown;

/**
* Iterates over all lines of the resource to extract the relevant key value pairs,
* e.g. the chart name for helm charts or the terraform_version for tfe_workspace
*/
do {
lineNumber += 1;
line = lines[lineNumber];
Expand All @@ -49,6 +55,7 @@ export function extractTerraformResource(
dep.managerData[kvMatch.groups.key] = kvMatch.groups.value;
break;
case 'version':
case 'terraform_version':
dep.currentValue = kvMatch.groups.value;
break;
default:
Expand Down Expand Up @@ -104,6 +111,15 @@ export function analyseTerraformResource(
dep.datasource = HelmDatasource.id;
break;

case TerraformResourceTypes.tfe_workspace:
if (dep.currentValue) {
analyseTerraformVersion(dep);
dep.depType = 'tfe_workspace';
} else {
dep.skipReason = 'no-version';
}
break;

default:
dep.skipReason = 'invalid-value';
break;
Expand Down