Skip to content

Commit

Permalink
feat: add a migrated_from label so we can know where resources were m…
Browse files Browse the repository at this point in the history
…igrated from
  • Loading branch information
Apollorion committed Oct 21, 2024
1 parent 666c250 commit fd3a388
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
5 changes: 4 additions & 1 deletion spacemk/exporters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@


class BaseExporter(ABC):
def __init__(self, config: dict):
def __init__(self, config: dict, type: str):
"""Constructor
Args:
config (dict): Exporter configuration
type (str): Exporter type. This will be used to identify where the data was migrated from.
"""
self._config = config
self.type = type

def _check_data(self, data: dict) -> dict:
"""Check source provider data and add warnings as needed
Expand Down Expand Up @@ -199,6 +201,7 @@ def export(self) -> None:
data = self._filter_data(data)
data = self._enrich_data(data)
data = self._map_data(data)
data["type"] = self.type
save_normalized_data(data)

logging.info("Stop exporting data")
2 changes: 1 addition & 1 deletion spacemk/exporters/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, organization_id: str, workspace_id: str):

class TerraformExporter(BaseExporter):
def __init__(self, config: dict):
super().__init__(config)
super().__init__(config, "terraform_cloud_enterprise")

self._property_mapping = {
"organizations": {
Expand Down
7 changes: 5 additions & 2 deletions spacemk/templates/base.tf.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ resource "spacelift_space" "{{ space._migration_id }}" {
{{ argument("inherit_entities", True, default=False) }}
{{ argument("name", space.name, required=True) }}
{{ argument("parent_space_id", "root", required=True) }}
{{ argument("labels", ["migrated_from:" + type], required=True) }}
{% block space_arguments_extra scoped %}{% endblock %}
}

{% if space.requires_terraform_workflow_tool %}
resource "spacelift_context" "{{ space._migration_id }}_terraform_workflow_tool" {
{{ argument("description", "Allows to use custom versions of Terraform. Make sure to provide the Terraform binary in the custom runner image for the stack.") }}
{{ argument("name", "Terraform Workflow Tool", required=True) }}
{{ argument("labels", ["migrated_from:" + type], required=True) }}
}

resource "spacelift_mounted_file" "workflow" {
Expand Down Expand Up @@ -83,7 +85,7 @@ resource "spacelift_stack" "{{ stack._relationships.space._migration_id }}_{{ st
{{ argument("branch", stack.vcs.branch, required=True) }}
{{ argument("description", stack.description) }}
{{ argument("local_preview", stack.local_preview) }}
{{ argument("labels", stack.labels) }}
{{ argument("labels", ["migrated_from:" + type] + stack.labels|default([]), required=True) }}
{{ argument("manage_state", generation_config.spacelift.manage_state) }}
{{ argument("name", stack.name, required=True) }}
{{ argument("project_root", stack.vcs.project_root) }}
Expand Down Expand Up @@ -197,7 +199,7 @@ resource "spacelift_environment_variable" "{{ variable._relationships.space._mig
{% for context in contexts %}
resource "spacelift_context" "{{ context._relationships.space._migration_id }}_{{ context._migration_id }}" {
{{ argument("description", context.description) }}
{{ argument("labels", context.labels, default=[]) }}
{{ argument("labels", ["migrated_from:" + type] + context.labels, required=True) }}
{{ argument("name", context.name, required=True) }}
{% block context_arguments_extra scoped %}{% endblock %}
}
Expand Down Expand Up @@ -268,6 +270,7 @@ resource "spacelift_module" "{{ module._relationships.space._migration_id }}_{{
{{ argument("repository", module.vcs.repository, required=True) }}
{{ argument("space_id", "spacelift_space." ~ module._relationships.space._migration_id ~ ".id", serialize=False) }}
{{ argument("terraform_provider", module.terraform_provider) }}
{{ argument("labels", ["migrated_from:" + type], required=True) }}
{% block module_arguments_extra scoped %}{% endblock %}

{% if module.vcs.provider == "github_custom" %}
Expand Down

0 comments on commit fd3a388

Please sign in to comment.