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: settings during code generation #49

Merged
merged 12 commits into from
Aug 5, 2024
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:
# custom_app: 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
13 changes: 12 additions & 1 deletion spacemk/commands/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,16 @@
)
@pass_meta_key("config")
def generate(config):
def default(value, _default):
return value if value is not None else _default

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

generator = Generator()
generator.generate(extra_vars=config.get("generator.extra_vars"))
generator.generate(extra_vars=config.get("generator.extra_vars"), generation_config=generation_config)
17 changes: 14 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,25 @@ 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: Optional[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()
6 changes: 5 additions & 1 deletion 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 @@ -99,9 +99,11 @@ resource "spacelift_stack" "{{ stack._relationships.space._migration_id }}_{{ st
{% endif %}

{% if stack.vcs.provider == "github_custom" %}
{% if generation_config.github.custom_app %}
github_enterprise {
{{ argument("namespace", stack.vcs.namespace) }}
}
{% endif %}
{% elif stack.vcs.provider %}
{{ stack.vcs.provider }} {
{{ argument("namespace", stack.vcs.namespace) }}
Expand Down Expand Up @@ -261,10 +263,12 @@ resource "spacelift_module" "{{ module._relationships.space._migration_id }}_{{
{% block module_arguments_extra scoped %}{% endblock %}

{% if module.vcs.provider == "github_custom" %}
{% if generation_config.github.custom_app %}
github_enterprise {
{{ argument("namespace", module.vcs.namespace) }}
}
{% endif %}
{% endif %}
}
{% block module_extra scoped %}{% endblock %}
{% endif %}
Expand Down