Skip to content

Commit

Permalink
feat: do this smarter and add in argument for defaulting manage_state
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollorion committed Jun 6, 2024
1 parent 041f864 commit e5cf122
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
13 changes: 13 additions & 0 deletions config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ exporter:
workspaces: ^example-.*$

generator:

# # If you use a custom app in github with spacelift, set this to `true`.
# # If you use the marketplace github app with spacelift, set this to `false`.
# # Default: false
# github:
# customApp: false
#
# # If you want spacelift to manage your state set to `true`.
# # If you want to use a third-party backend, like s3, set to `false`.
# # Default: true
# spacelift:
# manage_state: true

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

Expand Down
9 changes: 8 additions & 1 deletion spacemk/commands/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@
)
@pass_meta_key("config")
def generate(config):
def default(value, default):
return value if value is not None else default

spacelift = default(config.get("generator.spacelift"), {"manage_state": True})
github = default(config.get("generator.github"), {"custom_app": False})
generation_config = {"spacelift": spacelift, "github": github}

generator = Generator()
generator.generate(extra_vars=config.get("generator.extra_vars"))
generator.generate(extra_vars=config.get("generator.extra_vars"), generation_config=generation_config)
9 changes: 6 additions & 3 deletions spacemk/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ def _format_code(self) -> None:
else:
logging.info("Formatted generated Terraform code")

def _generate_code(self, data: dict, extra_vars: dict, template_name: str):
def _generate_code(self, data: dict, extra_vars: dict, template_name: str, generation_config: dict):
data["extra_vars"] = extra_vars
data["generation_config"] = generation_config

current_file_path = Path(__file__).parent.resolve()

Expand Down Expand Up @@ -123,15 +124,17 @@ def _validate_code(self) -> None:
else:
logging.info("Generated Terraform code is valid")

def generate(self, extra_vars: Optional[dict] = None, template_name: str = "main.tf.jinja"):
def generate(self, extra_vars: Optional[dict] = None, template_name: str = "main.tf.jinja", generation_config: dict = None):
"""Generate source code for managing Spacelift entities"""

if generation_config is None:
generation_config = {}
if extra_vars is None:
extra_vars = {}

self._check_requirements()
data = self._load_data()
data = self._process_data(data)
self._generate_code(data=data, extra_vars=extra_vars, template_name=template_name)
self._generate_code(data=data, extra_vars=extra_vars, template_name=template_name, generation_config=generation_config)
self._format_code()
self._validate_code()
16 changes: 3 additions & 13 deletions spacemk/templates/base.tf.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ resource "spacelift_stack" "{{ stack._relationships.space._migration_id }}_{{ st
{{ argument("description", stack.description) }}
{{ argument("local_preview", stack.local_preview) }}
{{ argument("labels", stack.labels) }}
{{ argument("manage_state", False) }}
{{ argument("manage_state", generation_config.spacelift.manage_state) }}
{{ argument("name", stack.name, required=True) }}
{{ argument("project_root", stack.vcs.project_root) }}
{{ argument("repository", stack.vcs.repository, required=True) }}
Expand All @@ -97,12 +97,7 @@ resource "spacelift_stack" "{{ stack._relationships.space._migration_id }}_{{ st
{% if stack.terraform.workflow_tool == "CUSTOM" %}
{{ argument("runner_image ", extra_vars.custom_terraform_runner_image ~ ":" ~ stack.terraform.version, required=True) }}
{% endif %}

{% if stack.vcs.provider == "github_custom" %}
{% if extra_vars is defined
and extra_vars.github is defined
and extra_vars.github.customApp is defined
and extra_vars.github.customApp %}
{% if stack.vcs.provider == "github_custom" and generation.github.custom_app %}
github_enterprise {
{{ argument("namespace", stack.vcs.namespace) }}
}
Expand Down Expand Up @@ -264,12 +259,7 @@ resource "spacelift_module" "{{ module._relationships.space._migration_id }}_{{
{{ argument("space_id", "spacelift_space." ~ module._relationships.space._migration_id ~ ".id", serialize=False) }}
{{ argument("terraform_provider", module.terraform_provider) }}
{% block module_arguments_extra scoped %}{% endblock %}

{% if module.vcs.provider == "github_custom" %}
{% if extra_vars is defined
and extra_vars.github is defined
and extra_vars.github.customApp is defined
and extra_vars.github.customApp %}
{% if module.vcs.provider == "github_custom" and generation_config.github.custom_app %}
github_enterprise {
{{ argument("namespace", module.vcs.namespace) }}
}
Expand Down

0 comments on commit e5cf122

Please sign in to comment.