Skip to content

Commit ed1bc23

Browse files
authored
Merge pull request #338 from dflook/version-v-prefix
Allow required_version constraint to have a 'v' prefix
2 parents 50c684e + 6e823d5 commit ed1bc23

File tree

6 files changed

+178
-7
lines changed

6 files changed

+178
-7
lines changed

.github/workflows/test-version.yaml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,29 @@ jobs:
5050
exit 1
5151
fi
5252
53+
required_version_v_prefix:
54+
runs-on: ubuntu-latest
55+
name: required_version with a v prefix
56+
steps:
57+
- name: Checkout
58+
uses: actions/checkout@v3
59+
60+
- name: Test terraform-version
61+
uses: ./terraform-version
62+
id: terraform-version
63+
with:
64+
path: tests/workflows/test-version/v-prefix
65+
66+
- name: Print the version
67+
run: echo "The terraform version was ${{ steps.terraform-version.outputs.terraform }}"
68+
69+
- name: Check the version
70+
run: |
71+
if [[ "${{ steps.terraform-version.outputs.terraform }}" != "0.12.26" ]]; then
72+
echo "::error:: Terraform version not set from required_version range"
73+
exit 1
74+
fi
75+
5376
tfswitch:
5477
runs-on: ubuntu-latest
5578
name: tfswitch
@@ -503,7 +526,7 @@ jobs:
503526

504527
- name: Check the version
505528
run: |
506-
if [[ "${{ steps.terraform-version.outputs.terraform }}" != *"1.7"* ]]; then
529+
if [[ "${{ steps.terraform-version.outputs.terraform }}" != *"1.8"* ]]; then
507530
echo "::error:: Latest version was not used"
508531
exit 1
509532
fi

image/src/terraform/module.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def get_version_constraints(module: TerraformModule) -> Optional[list[Constraint
126126
try:
127127
return [Constraint(c) for c in str(block['required_version']).split(',')]
128128
except Exception:
129-
debug('required_version constraint is malformed')
129+
debug(f'required_version constraint is malformed: {str(block["required_version"])}')
130130

131131
return None
132132

image/src/terraform/versions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def __init__(self, constraint: str):
8080
else:
8181
raise ValueError(f'Invalid version constraint {constraint}')
8282

83-
if match := re.match(r'(?P<major>\d+)(?:\.(?P<minor>\d+))?(?:\.(?P<patch>\d+))?(?:-(?P<pre_release>.*))?', constraint):
83+
if match := re.match(r'v?(?P<major>\d+)(?:\.(?P<minor>\d+))?(?:\.(?P<patch>\d+))?(?:-(?P<pre_release>.*))?', constraint):
8484
self.major = int(match.group('major'))
8585
self.minor = int(match.group('minor')) if match.group('minor') else None
8686
self.patch = int(match.group('patch')) if match.group('patch') else None

image/src/terraform_version/backend_constraints.json

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,12 @@
446446
],
447447
"accelerate": [
448448
">=1.3.0"
449+
],
450+
"endpoint": [
451+
">=1.5.0"
452+
],
453+
"domain": [
454+
">=1.5.0"
449455
]
450456
},
451457
"environment_variables": {
@@ -1004,6 +1010,23 @@
10041010
"skip_table_creation": [
10051011
">=0.14.0"
10061012
]
1013+
},
1014+
"environment_variables": {
1015+
"PG_CONN_STR": [
1016+
">=1.5.0"
1017+
],
1018+
"PG_SCHEMA_NAME": [
1019+
">=1.5.0"
1020+
],
1021+
"PG_SKIP_SCHEMA_CREATION": [
1022+
">=1.5.0"
1023+
],
1024+
"PG_SKIP_TABLE_CREATION": [
1025+
">=1.5.0"
1026+
],
1027+
"PG_SKIP_INDEX_CREATION": [
1028+
">=1.5.0"
1029+
]
10071030
}
10081031
},
10091032
"s3": {
@@ -1100,10 +1123,6 @@
11001123
"skip_region_validation": [
11011124
">=0.11.2"
11021125
],
1103-
"skip_requesting_account_id": [
1104-
">=0.10.8",
1105-
"<=0.12.31"
1106-
],
11071126
"sse_customer_key": [
11081127
">=0.12.8"
11091128
],
@@ -1115,6 +1134,63 @@
11151134
],
11161135
"workspace_key_prefix": [
11171136
">=0.10.0"
1137+
],
1138+
"assume_role": [
1139+
">=1.6.0"
1140+
],
1141+
"endpoints": [
1142+
">=1.6.0"
1143+
],
1144+
"shared_credentials_files": [
1145+
">=1.6.0"
1146+
],
1147+
"use_path_style": [
1148+
">=1.6.0"
1149+
],
1150+
"assume_role_with_web_identity": [
1151+
">=1.6.0"
1152+
],
1153+
"shared_config_files": [
1154+
">=1.6.0"
1155+
],
1156+
"custom_ca_bundle": [
1157+
">=1.6.0"
1158+
],
1159+
"ec2_metadata_service_endpoint": [
1160+
">=1.6.0"
1161+
],
1162+
"ec2_metadata_service_endpoint_mode": [
1163+
">=1.6.0"
1164+
],
1165+
"http_proxy": [
1166+
">=1.6.0"
1167+
],
1168+
"insecure": [
1169+
">=1.6.0"
1170+
],
1171+
"use_fips_endpoint": [
1172+
">=1.6.0"
1173+
],
1174+
"use_dualstack_endpoint": [
1175+
">=1.6.0"
1176+
],
1177+
"sts_region": [
1178+
">=1.6.0"
1179+
],
1180+
"retry_mode": [
1181+
">=1.6.0"
1182+
],
1183+
"allowed_account_ids": [
1184+
">=1.6.0"
1185+
],
1186+
"forbidden_account_ids": [
1187+
">=1.6.0"
1188+
],
1189+
"skip_requesting_account_id": [
1190+
">=1.6.1"
1191+
],
1192+
"skip_s3_checksum_validation": [
1193+
">=1.6.3"
11181194
]
11191195
},
11201196
"environment_variables": {
@@ -1150,6 +1226,33 @@
11501226
],
11511227
"AWS_DYNAMODB_ENDPOINT": [
11521228
">=0.11.14"
1229+
],
1230+
"AWS_ENDPOINT_URL_DYNAMODB": [
1231+
">=1.6.0"
1232+
],
1233+
"AWS_ENDPOINT_URL_IAM": [
1234+
">=1.6.0"
1235+
],
1236+
"AWS_ENDPOINT_URL_S3": [
1237+
">=1.6.0"
1238+
],
1239+
"AWS_ENDPOINT_URL_STS": [
1240+
">=1.6.0"
1241+
],
1242+
"AWS_CONFIG_FILE": [
1243+
">=1.6.0"
1244+
],
1245+
"AWS_SHARED_CREDENTIALS_FILE": [
1246+
">=1.6.0"
1247+
],
1248+
"AWS_CA_BUNDLE": [
1249+
">=1.6.0"
1250+
],
1251+
"AWS_EC2_METADATA_SERVICE_ENDPOINT": [
1252+
">=1.6.0"
1253+
],
1254+
"AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE": [
1255+
">=1.6.0"
11531256
]
11541257
}
11551258
},
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import hcl2
2+
3+
from terraform.versions import Version
4+
from terraform_version.required_version import get_required_version
5+
6+
7+
def test_required_version():
8+
expected_backend = hcl2.loads('''
9+
terraform {
10+
required_version = "1.0.0"
11+
}
12+
''')
13+
14+
assert get_required_version(expected_backend, [Version('1.0.0')]) == Version('1.0.0')
15+
16+
def test_required_version_v_prefix():
17+
expected_backend = hcl2.loads('''
18+
terraform {
19+
required_version = "v1.0.0"
20+
}
21+
''')
22+
23+
assert get_required_version(expected_backend, [Version('1.0.0')]) == Version('1.0.0')
24+
25+
def test_required_version_greater_than():
26+
expected_backend = hcl2.loads('''
27+
terraform {
28+
required_version = ">= 1.8.0"
29+
}
30+
''')
31+
32+
assert get_required_version(expected_backend, [Version('1.7.0'), Version('1.8.0'), Version('1.8.1')]) == Version('1.8.1')
33+
34+
35+
def test_required_version_greater_than_v_prefix():
36+
expected_backend = hcl2.loads('''
37+
terraform {
38+
required_version = ">= v1.8.0"
39+
}
40+
''')
41+
42+
assert get_required_version(expected_backend, [Version('1.7.0'), Version('1.8.0'), Version('1.8.1')]) == Version('1.8.1')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
terraform {
2+
required_version = "<= v0.12.26"
3+
}

0 commit comments

Comments
 (0)