Skip to content
This repository has been archived by the owner on Feb 25, 2024. It is now read-only.

refactor select template function in interactive mode #108

Merged
merged 1 commit into from
Apr 11, 2022
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
7 changes: 6 additions & 1 deletion bentoctl/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
from bentoctl.cli.utils import BentoctlCommandGroup, handle_bentoctl_exceptions
from bentoctl.console import print_generated_files_list, prompt_user_for_filename
from bentoctl.deployment_config import DeploymentConfig
from bentoctl.docker_utils import build_docker_image, push_docker_image_to_repository, tag_docker_image
from bentoctl.docker_utils import (
build_docker_image,
push_docker_image_to_repository,
tag_docker_image,
)
from bentoctl.exceptions import BentoctlException
from bentoctl.utils import TempDirectory, console

Expand Down Expand Up @@ -161,5 +165,6 @@ def build(
else:
console.print(f"[green]Create docker image: {local_docker_tag}[/]")


# subcommands
bentoctl.add_command(get_operator_management_subcommands())
36 changes: 19 additions & 17 deletions bentoctl/cli/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,20 +242,13 @@ def select_operator():
return operator


def select_template_type(operator_name):
available_template_types = local_operator_registry.get(
operator_name
).available_template_types

if len(available_template_types) == 1:
return available_template_types[0]

def select_template_type(available_templates):
try:
from simple_term_menu import TerminalMenu

tmenu = TerminalMenu(available_template_types, title="Choose a Template Type")
tmenu = TerminalMenu(available_templates, title="Choose a Template Type")
choice = tmenu.show()
return available_template_types[choice]
return available_templates[choice]
except ImportError:
template_type = prompt_input_value(
"template_type", deployment_config_schema.get("template_type")
Expand All @@ -281,17 +274,26 @@ def deployment_config_builder():
console.print(f"[b]name:[/] {name}")

# get operators
operator = select_operator()
deployment_config["operator"] = operator
console.print(f"[b]operator:[/] {operator}")
available_operators = list(local_operator_registry.list())
# automatically select the first operator if there is only one
operator_name = (
available_operators[0] if len(available_operators) == 1 else select_operator()
)
deployment_config["operator"] = operator_name
console.print(f"[b]operator:[/] {operator_name}")
operator = local_operator_registry.get(operator_name)

# get template_type
deployment_config["template_type"] = select_template_type(operator)
console.print(f"[b]template_type:[/] {deployment_config['template_type']}")
template_name = (
operator.default_template
if len(operator.available_templates) == 1
else select_template_type(operator.available_templates)
)
deployment_config["template"] = template_name
console.print(f"[b]template:[/] {template_name}")

console.print("[bold]spec: [/]")
operator = local_operator_registry.get(deployment_config["operator"])
spec = generate_spec(operator.operator_schema)
spec = generate_spec(operator.schema)
deployment_config["spec"] = dict(spec)

return DeploymentConfig(deployment_config)
16 changes: 9 additions & 7 deletions bentoctl/deployment_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,17 @@ def _set_operator(self):
self.operator = local_operator_registry.get(self.operator_name)

def _set_template_type(self):
self.template_type = self.deployment_config.get("template_type")
self.template_type = self.deployment_config.get("template")
if self.template_type is None:
raise InvalidDeploymentConfig("template_type is a required field")
elif self.template_type not in self.operator.available_template_types:
raise InvalidDeploymentConfig("template is a required field")
elif self.template_type not in self.operator.available_templates:
raise InvalidDeploymentConfig(
f"template_type '{self.template_type}' not supported by operator {self.operator_name}. Available template types are {self.operator.available_template_types}."
f"template '{self.template_type}' not supported by operator {self.operator_name}. Available template types are {self.operator.available_templates}."
)

def _set_operator_spec(self):
# cleanup operator_schema by removing 'help_message' field
operator_schema = remove_help_message(schema=self.operator.operator_schema)
operator_schema = remove_help_message(schema=self.operator.schema)
copied_operator_spec = copy.deepcopy(self.deployment_config["spec"])
v = cerberus.Validator()
validated_spec = v.validated(copied_operator_spec, schema=operator_schema)
Expand Down Expand Up @@ -186,7 +186,7 @@ def generate(self, destination_dir=os.curdir, values_only=False):
generated_files = self.operator.generate(
name=self.deployment_name,
spec=self.operator_spec,
template_type=self.deployment_config.get("template_type"),
template_type=self.template_type,
destination_dir=destination_dir,
values_only=values_only,
)
Expand Down Expand Up @@ -226,5 +226,7 @@ def generate_docker_image_tag(self, registry_url: str) -> str:
return image_tag

def generate_local_image_tag(self) -> str:
image_tag = f"{self.operator_name}-{self.bento.tag.name}:{self.bento.tag.version}"
image_tag = (
f"{self.operator_name}-{self.bento.tag.name}:{self.bento.tag.version}"
)
return image_tag
8 changes: 6 additions & 2 deletions bentoctl/docker_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ def tag_docker_image(image_name, image_tag):
try:
docker_client.images.tag(image_name, image_tag)
except docker.errors.APIError as error:
raise BentoctlDockerException(f"Failed to tag docker image {image_tag}: {error}")
raise BentoctlDockerException(
f"Failed to tag docker image {image_tag}: {error}"
)


def push_docker_image_to_repository(
Expand All @@ -108,4 +110,6 @@ def push_docker_image_to_repository(
)
console.print(":rocket: Image pushed!")
except docker.errors.APIError as error:
raise BentoctlDockerException(f"Failed to push docker image {image_tag}: {error}")
raise BentoctlDockerException(
f"Failed to push docker image {image_tag}: {error}"
)
16 changes: 9 additions & 7 deletions bentoctl/operator/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

logger = logging.getLogger(__name__)

DEFAULT_TEMPLATE_TYPES = ["terraform"]


class Operator:
def __init__(self, path):
Expand All @@ -39,15 +37,19 @@ def module_name(self):
self.self.operator_name

@property
def operator_schema(self):
def schema(self):
return self.operator_config.OPERATOR_SCHEMA

@property
def available_template_types(self):
if hasattr(self.operator_config, "AVAILABLE_TEMPLATE_TYPES"):
return self.operator_config.AVAILABLE_TEMPLATE_TYPES
def default_template(self):
return self.operator_config.OPERATOR_DEFAULT_TEMPLATE

@property
def available_templates(self):
if hasattr(self.operator_config, "OPERATOR_AVAILABLE_TEMPLATES"):
return self.operator_config.OPERATOR_AVAILABLE_TEMPLATES
else:
return DEFAULT_TEMPLATE_TYPES
return [self.operator_config.OPERATOR_DEFAULT_TEMPLATE]

def generate(self, name, spec, template_type, destination_dir, values_only=True):
operator = self._load_operator_module()
Expand Down