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

feat: support gitlab modules #74

Merged
merged 1 commit into from
Aug 6, 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
5 changes: 5 additions & 0 deletions config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ generator:
# # Default: N/A, must be provided (however, not always used. If it is used and not provided an exception will be raised during code generation)
# custom_runner_image: public.ecr.aws/spacelift/runner-terraform

# # The default branch for all modules. Only used if the module does not have a branch specified.
# # Default: empty string
# modules:
# default_branch: main

extra_vars:
foo: bar # "{{ extra_vars.foo }}" in a template will be replaced by "bar"

Expand Down
3 changes: 3 additions & 0 deletions spacemk/commands/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def default(value, _default):
},
"custom_runner_image": default(config.get("generator.custom_runner_image"),
"SPACELIFT_DEFAULT_INVALID"),
"modules": {
"default_branch": default(config.get("generator.modules.default_branch"), ""),
}
}

generator = Generator()
Expand Down
17 changes: 14 additions & 3 deletions spacemk/exporters/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def __init__(self, config: dict):
}
}

self.is_gitlab = False

def _build_stack_slug(self, workspace: dict) -> str:
return slugify(workspace.get("attributes.name"))

Expand Down Expand Up @@ -1114,9 +1116,15 @@ def _map_contexts_data(self, src_data: dict) -> dict:
def _map_modules_data(self, src_data: dict) -> dict:
logging.info("Start mapping modules data")

vcs_provider = "gitlab" if self.is_gitlab else "github_custom"

data = []
for module in src_data.get("modules"):
if module.get("attributes.vcs-repo.identifier"):
if self.is_gitlab and module.get("attributes.vcs-repo.identifier"):
segments = module.get("attributes.vcs-repo.identifier").split("/")
vcs_namespace = "/".join(segments[:-1])
vcs_repository = segments[-1]
elif module.get("attributes.vcs-repo.identifier"):
segments = module.get("attributes.vcs-repo.identifier").split("/")
vcs_namespace = segments[0]
vcs_repository = segments[1]
Expand All @@ -1140,7 +1148,7 @@ def _map_modules_data(self, src_data: dict) -> dict:
"vcs": {
"branch": module.get("attributes.vcs-repo.branch"),
"namespace": vcs_namespace,
"provider": "github_custom", # KLUDGE: TFC/TFE does not provide that information
"provider": vcs_provider,
"repository": vcs_repository,
},
}
Expand Down Expand Up @@ -1277,6 +1285,7 @@ def find_workspace_variable_with_invalid_name(data: dict, workspace_id: str, typ
raise ValueError(f"Unknown VCS provider name ({provider})")

if provider == "gitlab" and workspace.get("attributes.vcs-repo.identifier"):
self.is_gitlab = True
segments = workspace.get("attributes.vcs-repo.identifier").split("/")
vcs_namespace = "/".join(segments[:-1])
vcs_repository = segments[-1]
Expand Down Expand Up @@ -1343,8 +1352,10 @@ def _map_data(self, src_data: dict) -> dict:
# "context_variables": self._map_context_variables_data(
# src_data
# ), # Must be after contexts due to dependency
"modules": self._map_modules_data(src_data),

# Stacks must be before modules so we can determine is_gitlab so we set VCS properly
"stacks": self._map_stacks_data(src_data),
"modules": self._map_modules_data(src_data),
"stack_variables": self._map_stack_variables_data(src_data), # Must be after stacks due to dependency
}
)
Expand Down
9 changes: 9 additions & 0 deletions spacemk/templates/base.tf.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,12 @@ resource "spacelift_environment_variable" "{{ variable._relationships.space._mig
{% for module in modules %}
{% if module.status == "setup_complete" and module.visibility == "private" %}
resource "spacelift_module" "{{ module._relationships.space._migration_id }}_{{ module._migration_id }}" {

{% if module.vcs.branch == ""%}
{{ argument("branch", generation_config.modules.default_branch, required=True) }}
{% else %}
{{ argument("branch", module.vcs.branch, required=True) }}
{% endif %}
{{ argument("name", module.name) }}
{{ argument("repository", module.vcs.repository, required=True) }}
{{ argument("space_id", "spacelift_space." ~ module._relationships.space._migration_id ~ ".id", serialize=False) }}
Expand All @@ -271,6 +276,10 @@ resource "spacelift_module" "{{ module._relationships.space._migration_id }}_{{
{{ argument("namespace", module.vcs.namespace) }}
}
{% endif %}
{% elif module.vcs.provider %}
{{ module.vcs.provider }} {
{{ argument("namespace", module.vcs.namespace) }}
}
{% endif %}
}
{% block module_extra scoped %}{% endblock %}
Expand Down