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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource "tfe_workspace" "test_workspace" {
morremeyer marked this conversation as resolved.
Show resolved Hide resolved
name = "test-workspace"
organization = "renovate-fixtures"

terraform_version = "1.1.6"
}
14 changes: 14 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,20 @@ Object {
}
`;

exports[`modules/manager/terraform/extract extractPackageFile() extracts terraform_version for tfe_workspace 1`] = `
Object {
"deps": Array [
Object {
"currentValue": "1.1.6",
"datasource": "github-tags",
"depName": "hashicorp/terraform",
"depType": "required_version",
"extractVersion": "v(?<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('tfeWorkspaceTerraformVersion.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', async () => {
const res = await extractPackageFile(
tfeWorkspaceBlock,
'tfeWorkspaceTerraformVersion.tf',
{}
);
expect(res).toMatchSnapshot();
expect(res.deps).toHaveLength(1);
expect(res.deps.filter((dep) => dep.skipReason)).toHaveLength(0);
});
});
});
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',
morremeyer marked this conversation as resolved.
Show resolved Hide resolved
];

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,8 @@ 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) resource (tfe provider)
- `terraform_version` argument
morremeyer marked this conversation as resolved.
Show resolved Hide resolved

Terraform range constraints are supported:

Expand All @@ -24,15 +26,15 @@ 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 both `required_version` in terraform blocks as well as `terraform_version` in `tfe_workspace` resources |
morremeyer marked this conversation as resolved.
Show resolved Hide resolved
| 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.
11 changes: 11 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,10 @@ export function analyseTerraformResource(
dep.datasource = HelmDatasource.id;
break;

case TerraformResourceTypes.tfe_workspace:
analyseTerraformVersion(dep);
morremeyer marked this conversation as resolved.
Show resolved Hide resolved
break;

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