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

Allow required_version constraint to have a 'v' prefix #338

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion .github/workflows/test-version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@ jobs:
exit 1
fi

required_version_v_prefix:
runs-on: ubuntu-latest
name: required_version with a v prefix
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Test terraform-version
uses: ./terraform-version
id: terraform-version
with:
path: tests/workflows/test-version/v-prefix

- name: Print the version
run: echo "The terraform version was ${{ steps.terraform-version.outputs.terraform }}"

- name: Check the version
run: |
if [[ "${{ steps.terraform-version.outputs.terraform }}" != "0.12.26" ]]; then
echo "::error:: Terraform version not set from required_version range"
exit 1
fi

tfswitch:
runs-on: ubuntu-latest
name: tfswitch
Expand Down Expand Up @@ -503,7 +526,7 @@ jobs:

- name: Check the version
run: |
if [[ "${{ steps.terraform-version.outputs.terraform }}" != *"1.7"* ]]; then
if [[ "${{ steps.terraform-version.outputs.terraform }}" != *"1.8"* ]]; then
echo "::error:: Latest version was not used"
exit 1
fi
Expand Down
2 changes: 1 addition & 1 deletion image/src/terraform/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def get_version_constraints(module: TerraformModule) -> Optional[list[Constraint
try:
return [Constraint(c) for c in str(block['required_version']).split(',')]
except Exception:
debug('required_version constraint is malformed')
debug(f'required_version constraint is malformed: {str(block["required_version"])}')

return None

Expand Down
2 changes: 1 addition & 1 deletion image/src/terraform/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __init__(self, constraint: str):
else:
raise ValueError(f'Invalid version constraint {constraint}')

if match := re.match(r'(?P<major>\d+)(?:\.(?P<minor>\d+))?(?:\.(?P<patch>\d+))?(?:-(?P<pre_release>.*))?', constraint):
if match := re.match(r'v?(?P<major>\d+)(?:\.(?P<minor>\d+))?(?:\.(?P<patch>\d+))?(?:-(?P<pre_release>.*))?', constraint):
self.major = int(match.group('major'))
self.minor = int(match.group('minor')) if match.group('minor') else None
self.patch = int(match.group('patch')) if match.group('patch') else None
Expand Down
111 changes: 107 additions & 4 deletions image/src/terraform_version/backend_constraints.json
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,12 @@
],
"accelerate": [
">=1.3.0"
],
"endpoint": [
">=1.5.0"
],
"domain": [
">=1.5.0"
]
},
"environment_variables": {
Expand Down Expand Up @@ -1004,6 +1010,23 @@
"skip_table_creation": [
">=0.14.0"
]
},
"environment_variables": {
"PG_CONN_STR": [
">=1.5.0"
],
"PG_SCHEMA_NAME": [
">=1.5.0"
],
"PG_SKIP_SCHEMA_CREATION": [
">=1.5.0"
],
"PG_SKIP_TABLE_CREATION": [
">=1.5.0"
],
"PG_SKIP_INDEX_CREATION": [
">=1.5.0"
]
}
},
"s3": {
Expand Down Expand Up @@ -1100,10 +1123,6 @@
"skip_region_validation": [
">=0.11.2"
],
"skip_requesting_account_id": [
">=0.10.8",
"<=0.12.31"
],
"sse_customer_key": [
">=0.12.8"
],
Expand All @@ -1115,6 +1134,63 @@
],
"workspace_key_prefix": [
">=0.10.0"
],
"assume_role": [
">=1.6.0"
],
"endpoints": [
">=1.6.0"
],
"shared_credentials_files": [
">=1.6.0"
],
"use_path_style": [
">=1.6.0"
],
"assume_role_with_web_identity": [
">=1.6.0"
],
"shared_config_files": [
">=1.6.0"
],
"custom_ca_bundle": [
">=1.6.0"
],
"ec2_metadata_service_endpoint": [
">=1.6.0"
],
"ec2_metadata_service_endpoint_mode": [
">=1.6.0"
],
"http_proxy": [
">=1.6.0"
],
"insecure": [
">=1.6.0"
],
"use_fips_endpoint": [
">=1.6.0"
],
"use_dualstack_endpoint": [
">=1.6.0"
],
"sts_region": [
">=1.6.0"
],
"retry_mode": [
">=1.6.0"
],
"allowed_account_ids": [
">=1.6.0"
],
"forbidden_account_ids": [
">=1.6.0"
],
"skip_requesting_account_id": [
">=1.6.1"
],
"skip_s3_checksum_validation": [
">=1.6.3"
]
},
"environment_variables": {
Expand Down Expand Up @@ -1150,6 +1226,33 @@
],
"AWS_DYNAMODB_ENDPOINT": [
">=0.11.14"
],
"AWS_ENDPOINT_URL_DYNAMODB": [
">=1.6.0"
],
"AWS_ENDPOINT_URL_IAM": [
">=1.6.0"
],
"AWS_ENDPOINT_URL_S3": [
">=1.6.0"
],
"AWS_ENDPOINT_URL_STS": [
">=1.6.0"
],
"AWS_CONFIG_FILE": [
">=1.6.0"
],
"AWS_SHARED_CREDENTIALS_FILE": [
">=1.6.0"
],
"AWS_CA_BUNDLE": [
">=1.6.0"
],
"AWS_EC2_METADATA_SERVICE_ENDPOINT": [
">=1.6.0"
],
"AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE": [
">=1.6.0"
]
}
},
Expand Down
42 changes: 42 additions & 0 deletions tests/terraform_version/test_required_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import hcl2

from terraform.versions import Version
from terraform_version.required_version import get_required_version


def test_required_version():
expected_backend = hcl2.loads('''
terraform {
required_version = "1.0.0"
}
''')

assert get_required_version(expected_backend, [Version('1.0.0')]) == Version('1.0.0')

def test_required_version_v_prefix():
expected_backend = hcl2.loads('''
terraform {
required_version = "v1.0.0"
}
''')

assert get_required_version(expected_backend, [Version('1.0.0')]) == Version('1.0.0')

def test_required_version_greater_than():
expected_backend = hcl2.loads('''
terraform {
required_version = ">= 1.8.0"
}
''')

assert get_required_version(expected_backend, [Version('1.7.0'), Version('1.8.0'), Version('1.8.1')]) == Version('1.8.1')


def test_required_version_greater_than_v_prefix():
expected_backend = hcl2.loads('''
terraform {
required_version = ">= v1.8.0"
}
''')

assert get_required_version(expected_backend, [Version('1.7.0'), Version('1.8.0'), Version('1.8.1')]) == Version('1.8.1')
3 changes: 3 additions & 0 deletions tests/workflows/test-version/v-prefix/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = "<= v0.12.26"
}
Loading