Skip to content

Commit

Permalink
Explicit config (#2294)
Browse files Browse the repository at this point in the history
Co-authored-by: Fangchen Li <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Amit Kumar <[email protected]>
Co-authored-by: Vinicius D. Cerutti <[email protected]>
  • Loading branch information
5 people authored Jun 17, 2024
1 parent 749a1f1 commit cf0f754
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/_nebari/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def write_configuration(
"""Write the nebari configuration file to disk"""
with config_filename.open(mode) as f:
if isinstance(config, pydantic.BaseModel):
yaml.dump(config.model_dump(), f)
config_dict = config.model_dump()
yaml.dump(config_dict, f)
else:
config = dump_nested_model(config)
yaml.dump(config, f)
Expand Down
19 changes: 18 additions & 1 deletion src/_nebari/subcommands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class InitInputs(schema.Base):
ssl_cert_email: Optional[schema.email_pydantic] = None
disable_prompt: bool = False
output: pathlib.Path = pathlib.Path("nebari-config.yaml")
explicit: int = 0


def enum_to_list(enum_cls):
Expand Down Expand Up @@ -152,7 +153,7 @@ def handle_init(inputs: InitInputs, config_schema: BaseModel):
try:
write_configuration(
inputs.output,
config,
config if not inputs.explicit else config_schema(**config),
mode="x",
)
except FileExistsError:
Expand Down Expand Up @@ -565,6 +566,13 @@ def init(
"-o",
help="Output file path for the rendered config file.",
),
explicit: int = typer.Option(
0,
"--explicit",
"-e",
count=True,
help="Write explicit nebari config file (advanced users only).",
),
):
"""
Create and initialize your [purple]nebari-config.yaml[/purple] file.
Expand Down Expand Up @@ -604,6 +612,7 @@ def init(
inputs.ssl_cert_email = ssl_cert_email
inputs.disable_prompt = disable_prompt
inputs.output = output
inputs.explicit = explicit

from nebari.plugins import nebari_plugin_manager

Expand Down Expand Up @@ -894,6 +903,14 @@ def guided_init_wizard(ctx: typer.Context, guided_init: str):
)
inputs.kubernetes_version = kubernetes_version

# EXPLICIT CONFIG
inputs.explicit = questionary.confirm(
"Would you like the nebari config to show all available options? (recommended for advanced users only)",
default=False,
qmark=qmark,
auto_enter=False,
).unsafe_ask()

from nebari.plugins import nebari_plugin_manager

config_schema = nebari_plugin_manager.config_schema
Expand Down
2 changes: 1 addition & 1 deletion src/nebari/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def config_schema(self):
classes = [schema.Main] + [
_.input_schema for _ in self.ordered_stages if _.input_schema is not None
]
return type("ConfigSchema", tuple(classes), {})
return type("ConfigSchema", tuple(classes[::-1]), {})


nebari_plugin_manager = NebariPluginManager()
4 changes: 3 additions & 1 deletion src/nebari/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

class Base(pydantic.BaseModel):
model_config = ConfigDict(
extra="forbid", validate_assignment=True, populate_by_name=True
extra="forbid",
validate_assignment=True,
populate_by_name=True,
)


Expand Down
34 changes: 21 additions & 13 deletions tests/tests_unit/test_cli_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
(["--ssl-cert-email"], 2, ["requires an argument"]),
(["--output"], 2, ["requires an argument"]),
(["-o"], 2, ["requires an argument"]),
(["--explicit"], 2, ["Missing option"]),
(["-e"], 2, ["Missing option"]),
],
)
def test_cli_init_stdout(args: List[str], exit_code: int, content: List[str]):
Expand Down Expand Up @@ -90,20 +92,22 @@ def generate_test_data_test_cli_init_happy_path():
) in get_kubernetes_versions(provider) + [
"latest"
]:
test_data.append(
(
provider,
region,
project_name,
domain_name,
namespace,
auth_provider,
ci_provider,
terraform_state,
email,
kubernetes_version,
for explicit in [True, False]:
test_data.append(
(
provider,
region,
project_name,
domain_name,
namespace,
auth_provider,
ci_provider,
terraform_state,
email,
kubernetes_version,
explicit,
)
)
)

keys = [
"provider",
Expand All @@ -116,6 +120,7 @@ def generate_test_data_test_cli_init_happy_path():
"terraform_state",
"email",
"kubernetes_version",
"explicit",
]
return {"keys": keys, "test_data": test_data}

Expand All @@ -131,6 +136,7 @@ def test_cli_init_happy_path(
terraform_state: str,
email: str,
kubernetes_version: str,
explicit: bool,
):
app = create_cli()
args = [
Expand Down Expand Up @@ -159,6 +165,8 @@ def test_cli_init_happy_path(
"--region",
region,
]
if explicit:
args += ["--explicit"]

expected_yaml = f"""
provider: {provider}
Expand Down

0 comments on commit cf0f754

Please sign in to comment.