Skip to content

Commit

Permalink
Allow dropping Terraform workspace variables
Browse files Browse the repository at this point in the history
  • Loading branch information
jmfontaine committed May 17, 2024
1 parent 250e4a0 commit 09567d1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
22 changes: 22 additions & 0 deletions spacemk/exporters/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import functools
import logging
import re
from abc import ABC, abstractmethod
from functools import reduce
from itertools import filterfalse
from pathlib import Path

import click
Expand Down Expand Up @@ -88,8 +91,27 @@ def _filter_data(self, data: dict) -> dict:
Returns:
dict: Filtered source provider data
"""

def drop_item(attribute_name: str, regex: re.Pattern, item: dict) -> bool:
if f"attributes.{attribute_name}" not in item:
logging.warning(f"Could not find attribute '{attribute_name}' in item {item}")
return True

return regex.match(item.get(f"attributes.{attribute_name}"))

logging.info("No custom data filtering implemented. Using default implementation.")

if "drop_by_name" in self._config and self._config.get("drop_by_name") is not None:
for resource_type, patterns in self._config.get("drop_by_name").items():
if patterns:
for pattern in patterns:
regex = re.compile(pattern)
attribute_name = "key" if resource_type == "workspace_variables" else "name"

data[resource_type][:] = filterfalse(
functools.partial(drop_item, attribute_name, regex), data.get(resource_type)
)

return data

@abstractmethod
Expand Down
5 changes: 4 additions & 1 deletion spacemk/exporters/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,10 @@ def find_space(data: dict, id_: str) -> dict:
for stack in data.get("stacks"):
if stack.get("terraform.workflow_tool") == "CUSTOM":
space = find_space(data, stack.get("_relationships.space"))
space["requires_terraform_workflow_tool"] = True
if space:
space["requires_terraform_workflow_tool"] = True
else:
logging.warning(f"Could not find space '{stack.get('_relationships.space')}'")

logging.info("Stop marking spaces for Terraform custom workflow")

Expand Down

0 comments on commit 09567d1

Please sign in to comment.