Skip to content

Commit

Permalink
vdk-control-cli: use common output printer
Browse files Browse the repository at this point in the history
Adopting the new output_printer to print all outputs of all commands

Some commands required small refactoring - to make some fields private.

Signed-off-by: Antoni Ivanov <[email protected]>
  • Loading branch information
antoniivanov committed Apr 5, 2023
1 parent ea36e8c commit 5846dc6
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,22 +178,20 @@ def deploy(
job_version = get_or_prompt("Job Version", job_version)
if job_path:
reason = get_or_prompt("Reason", reason)
return cmd.create(
name, team, job_path, reason, output, vdk_version, enabled
)
return cmd.create(name, team, job_path, reason, vdk_version, enabled)
else:
return cmd.update(name, team, enabled, job_version, vdk_version, output)
return cmd.update(name, team, enabled, job_version, vdk_version)
if operation == DeployOperation.REMOVE.value:
name = get_or_prompt("Job Name", name)
team = get_or_prompt("Job Team", team)
return cmd.remove(name, team)
if operation == DeployOperation.SHOW.value:
name = get_or_prompt("Job Name", name)
team = get_or_prompt("Job Team", team)
return cmd.show(name, team, output)
return cmd.show(name, team)
if operation == DeployOperation.CREATE.value:
job_path = get_or_prompt("Job Path", job_path)
default_name = os.path.basename(job_path)
name = get_or_prompt("Job Name", name, default_name)
reason = get_or_prompt("Reason", reason)
return cmd.create(name, team, job_path, reason, output, vdk_version, enabled)
return cmd.create(name, team, job_path, reason, vdk_version, enabled)
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# Copyright 2021-2023 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0
import glob
import json
import logging
import os
from typing import Optional

import click
import click_spinner
from tabulate import tabulate
from taurus_datajob_api import ApiException
from taurus_datajob_api import DataJob
from taurus_datajob_api import DataJobConfig
Expand All @@ -21,6 +19,7 @@
from vdk.internal.control.job.job_config import JobConfig
from vdk.internal.control.rest_lib.factory import ApiClientFactory
from vdk.internal.control.rest_lib.rest_client_errors import ApiClientErrorDecorator
from vdk.internal.control.utils import output_printer
from vdk.internal.control.utils.cli_utils import get_or_prompt
from vdk.internal.control.utils.output_printer import OutputFormat

Expand All @@ -31,14 +30,16 @@ class JobDeploy:
ZIP_ARCHIVE_TYPE = "zip"
ARCHIVE_SUFFIX = "-archive"

def __init__(self, rest_api_url: str, output):
def __init__(self, rest_api_url: str, output: str):
self.deploy_api = ApiClientFactory(rest_api_url).get_deploy_api()
self.jobs_api = ApiClientFactory(rest_api_url).get_jobs_api()
self.job_sources_api = ApiClientFactory(rest_api_url).get_jobs_sources_api()
# support for multiple deployments is not implemented yet so we can put anything here.
# Ultimately this will be user facing parameter (possibly fetched from config.ini)
self.__deployment_id = "production"
self.__job_archive = JobArchive()
self.__output = output
self.__printer = output_printer.create_printer(self.__output)

@staticmethod
def __detect_keytab_files_in_job_directory(job_path: str) -> None:
Expand Down Expand Up @@ -153,7 +154,6 @@ def update(
enabled: Optional[bool], # true, false or None
job_version: Optional[str],
vdk_version: Optional[str],
output: str,
) -> None:
deployment = DataJobDeployment(enabled=None)
if job_version:
Expand All @@ -163,7 +163,7 @@ def update(
deployment.enabled = enabled

if job_version:
self.__update_job_version(name, team, deployment, output)
self.__update_job_version(name, team, deployment)
elif vdk_version or enabled is not None:
self.__update_deployment(name, team, deployment)
msg = f"Deployment of Data Job {name} updated; "
Expand All @@ -186,16 +186,14 @@ def __update_deployment(
data_job_deployment=deployment,
)

def __update_job_version(
self, name: str, team: str, deployment: DataJobDeployment, output: str
):
def __update_job_version(self, name: str, team: str, deployment: DataJobDeployment):
log.debug(
f"Update Deployment version of a job {name} of team {team} : {deployment}"
)
self.deploy_api.deployment_update(
team_name=team, job_name=name, data_job_deployment=deployment
)
if output == OutputFormat.TEXT.value:
if self.__output == OutputFormat.TEXT.value:
log.info(
f"Request to deploy Data Job {name} using version {deployment.job_version} finished successfully.\n"
f"It would take a few minutes for the Data Job to be deployed in the server.\n"
Expand All @@ -210,7 +208,7 @@ def __update_job_version(
"job_name": name,
"job_version": deployment.job_version,
}
click.echo(json.dumps(result))
self.__printer.print_dict(result)

@ApiClientErrorDecorator()
def remove(self, name: str, team: str) -> None:
Expand All @@ -221,7 +219,7 @@ def remove(self, name: str, team: str) -> None:
log.info(f"Deployment of Data Job {name} removed.")

@ApiClientErrorDecorator()
def show(self, name: str, team: str, output: str) -> None:
def show(self, name: str, team: str) -> None:
log.debug(f"Get list of deployments for job {name} of team {team} ")
deployments = self.deploy_api.deployment_list(team_name=team, job_name=name)
log.debug(
Expand All @@ -239,20 +237,17 @@ def show(self, name: str, team: str, output: str) -> None:
),
deployments,
)
if output == OutputFormat.TEXT.value:
if self.__output == OutputFormat.TEXT.value:
click.echo(
"You can compare the version seen here to the one seen when "
"deploying to verify your deployment was successful."
)
click.echo("")
click.echo(tabulate(deployments, headers="keys"))
self.__printer.print_table(list(deployments))
else:
click.echo(json.dumps(list(deployments)))
self.__printer.print_table(list(deployments))
else:
if output == OutputFormat.TEXT.value:
click.echo("No deployments.")
else:
click.echo(json.dumps([]))
self.__printer.print_table(None)

@ApiClientErrorDecorator()
def create(
Expand All @@ -261,7 +256,6 @@ def create(
team: str,
job_path: str,
reason: str,
output: str,
vdk_version: Optional[str],
enabled: Optional[bool],
) -> None:
Expand All @@ -287,7 +281,7 @@ def create(
"Team Name", team or job_config.get_team() or load_default_team_name()
)

if output == OutputFormat.TEXT.value:
if self.__output == OutputFormat.TEXT.value:
log.info(
f"Deploy Data Job with name {name} from directory {job_path} ... \n"
)
Expand All @@ -298,9 +292,11 @@ def create(
try:
job_archive_binary = self.__archive_binary(archive_path)

if output == OutputFormat.TEXT.value:
if self.__output == OutputFormat.TEXT.value:
log.info("Uploading the data job might take some time ...")
with click_spinner.spinner(disable=(output == OutputFormat.JSON.value)):
with click_spinner.spinner(
disable=(self.__output == OutputFormat.JSON.value)
):
data_job_version = self.job_sources_api.sources_upload(
team_name=team,
job_name=name,
Expand All @@ -309,8 +305,6 @@ def create(
)

self.__update_data_job_deploy_configuration(job_path, name, team)
self.update(
name, team, enabled, data_job_version.version_sha, vdk_version, output
)
self.update(name, team, enabled, data_job_version.version_sha, vdk_version)
finally:
self.__cleanup_archive(archive_path=archive_path)
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import logging
import operator
import os
import sys
import webbrowser
from enum import Enum
from enum import unique
from typing import List
from typing import Optional

import click
import click_spinner
from tabulate import tabulate
from taurus_datajob_api import DataJobExecution
from taurus_datajob_api import DataJobExecutionLogs
from taurus_datajob_api import DataJobExecutionRequest
Expand All @@ -21,6 +20,7 @@
from vdk.internal.control.rest_lib.factory import ApiClientFactory
from vdk.internal.control.rest_lib.rest_client_errors import ApiClientErrorDecorator
from vdk.internal.control.utils import cli_utils
from vdk.internal.control.utils import output_printer
from vdk.internal.control.utils.cli_utils import get_or_prompt
from vdk.internal.control.utils.output_printer import OutputFormat

Expand Down Expand Up @@ -54,11 +54,7 @@ def transform_execution(e: DataJobExecution):
return d

executions = list(map(lambda e: transform_execution(e), executions))

if output == OutputFormat.TEXT.value:
return tabulate(executions, headers="keys")
elif output == OutputFormat.JSON.value:
return cli_utils.json_format(list(executions))
output_printer.create_printer(output).print_table(executions)

@staticmethod
def __validate_and_parse_args(arguments: str) -> str:
Expand Down Expand Up @@ -101,13 +97,13 @@ def start(self, name: str, team: str, output: OutputFormat, arguments: str) -> N
f"See execution logs using: \n\n"
f"vdk execute --logs --execution-id {execution_id} -n {name} -t {team}"
)
elif output == OutputFormat.JSON.value:
else:
result = {
"job_name": name,
"team": team,
"execution_id": execution_id,
}
click.echo(json.dumps(result))
output_printer.create_printer(output).print_dict(result)

@ApiClientErrorDecorator()
def cancel(self, name: str, team: str, execution_id: str) -> None:
Expand All @@ -126,14 +122,14 @@ def show(
execution: DataJobExecution = self.__execution_api.data_job_execution_read(
team_name=team, job_name=name, execution_id=execution_id
)
click.echo(self.__model_executions([execution], output))
self.__model_executions([execution], output)

@ApiClientErrorDecorator()
def list(self, name: str, team: str, output: OutputFormat) -> None:
executions: list[
executions: List[
DataJobExecution
] = self.__execution_api.data_job_execution_list(team_name=team, job_name=name)
click.echo(self.__model_executions(executions, output))
self.__model_executions(executions, output)

def __get_execution_to_log(
self, name: str, team: str, execution_id: str
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# Copyright 2021-2023 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0
import datetime
import json
import logging
from enum import Enum
from enum import unique
from typing import Dict
from typing import List

import click
from tabulate import tabulate
from taurus_datajob_api import DataJobQueryResponse
from vdk.internal.control.configuration.defaults_config import load_default_team_name
from vdk.internal.control.rest_lib.factory import ApiClientFactory
from vdk.internal.control.rest_lib.rest_client_errors import ApiClientErrorDecorator
from vdk.internal.control.utils import cli_utils
from vdk.internal.control.utils import output_printer
from vdk.internal.control.utils.cli_utils import GqlQueryBuilder
from vdk.internal.control.utils.output_printer import OutputFormat

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -55,13 +53,7 @@ def list_jobs(
page_number += 1

jobs = list(map(self.job_to_dict, jobs))
if output == OutputFormat.TEXT.value:
if len(jobs) > 0:
click.echo(tabulate(jobs, headers="keys"))
else:
click.echo("No Data Jobs.")
else:
click.echo(json.dumps(list(jobs)))
output_printer.create_printer(output).print_table(jobs)

@staticmethod
def job_to_dict(job: Dict):
Expand Down
Loading

0 comments on commit 5846dc6

Please sign in to comment.