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

Handle version constraints #32

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions spacemk/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import re
import subprocess
from importlib.metadata import version
from pathlib import Path
Expand Down Expand Up @@ -31,6 +32,16 @@ def pypi_version_to_semver(version: str) -> str:
__version__ = pypi_version_to_semver(version("spacemk"))


def terraform_version_to_semver(version: str | None) -> str:
try:
version_number = re.sub(r"[^\d\.]", "", version)
semver_version = SemVerVersion.parse(version_number)
return str(semver_version)
except Exception:
# KLUDGE: Stick to the latest MPL-licensed Terraform version for now
return "1.5.7"


def ensure_folder_exists(path: Path | str) -> None:
if isinstance(path, str):
path = Path(path).resolve()
Expand Down
15 changes: 5 additions & 10 deletions spacemk/exporters/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from requests_toolbelt.utils import dump as request_dump
from slugify import slugify

from spacemk import get_tmp_subfolder, is_command_available
from spacemk import get_tmp_subfolder, is_command_available, terraform_version_to_semver
from spacemk.exporters import BaseExporter


Expand Down Expand Up @@ -180,9 +180,9 @@ def _check_workspaces_data(self, data: list[dict]) -> list[dict]:
if item.get("attributes.vcs-repo.service-provider") is None:
warnings.append("No VCS configuration")

if semver.match(item.get("attributes.terraform-version"), ">=1.5.7"):
terraform_version = terraform_version_to_semver(item.get("attributes.terraform-version"))
if semver.match(terraform_version, ">=1.5.7"):
warnings.append("BSL Terraform version")

data[key]["warnings"] = ", ".join(warnings)

logging.info("Stop checking workspaces data")
Expand Down Expand Up @@ -1266,13 +1266,8 @@ def find_workspace_variable_with_invalid_name(data: dict, workspace_id: str, typ
else:
vcs_namespace = None
vcs_repository = None

terraform_version = workspace.get("attributes.terraform-version")
if terraform_version == "latest":
# KLUDGE: Stick to the latest MPL-licensed Terraform version for now
terraform_version = "1.5.7"

if semver.match(workspace.get("attributes.terraform-version"), ">=1.5.7"):
terraform_version = terraform_version_to_semver(workspace.get("attributes.terraform-version"))
if semver.match(terraform_version, ">=1.5.7"):
terraform_workflow_tool = "CUSTOM"
else:
terraform_workflow_tool = "TERRAFORM_FOSS"
Expand Down
21 changes: 21 additions & 0 deletions tests/version_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest

from spacemk import terraform_version_to_semver


@pytest.mark.parametrize(
("version", "expected_version"),
[
(">= 1.3.0, <1.6.0", "1.5.7"),
("~> 1.3.9", "1.3.9"),
("~> 1.5.0", "1.5.0"),
("~>1.5.7", "1.5.7"),
("1.5.2", "1.5.2"),
("1.6.3", "1.6.3"),
("latest", "1.5.7"),
(None, "1.5.7"),
],
)
def test_terraform_version_to_semver(version: str | None, expected_version: str) -> None:
actual_version = terraform_version_to_semver(version)
assert actual_version == expected_version