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

vdk-control-cli: use common output printer #1852

Merged
merged 2 commits into from
Apr 6, 2023
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
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_format: 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_format = output_format
self.__printer = output_printer.create_printer(self.__output_format)

@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_format == 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_format == 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_format == 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_format == 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_format == 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 All @@ -46,19 +46,15 @@ def __init__(self, rest_api_url: str):
self.__execution_api = ApiClientFactory(rest_api_url).get_execution_api()

@staticmethod
def __model_executions(executions, output: OutputFormat) -> str:
def __model_executions(executions, output_format: OutputFormat) -> str:
def transform_execution(e: DataJobExecution):
d = e.to_dict()
d["job_version"] = e.deployment.job_version
del d["deployment"]
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_format).print_table(executions)

@staticmethod
def __validate_and_parse_args(arguments: str) -> str:
Expand All @@ -77,7 +73,9 @@ def __validate_and_parse_args(arguments: str) -> str:
raise vdk_ex

@ApiClientErrorDecorator()
def start(self, name: str, team: str, output: OutputFormat, arguments: str) -> None:
def start(
self, name: str, team: str, output_format: OutputFormat, arguments: str
) -> None:
execution_request = DataJobExecutionRequest(
started_by=f"vdk-control-cli",
args=self.__validate_and_parse_args(arguments),
Expand All @@ -93,21 +91,21 @@ def start(self, name: str, team: str, output: OutputFormat, arguments: str) -> N

location = headers["Location"]
execution_id = os.path.basename(location)
if output == OutputFormat.TEXT.value:
if output_format == OutputFormat.TEXT.value:
click.echo(
f"Execution of Data Job {name} started. "
f"See execution status using: \n\n"
f"vdk execute --show --execution-id {execution_id} -n {name} -t {team}\n\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_format).print_dict(result)

@ApiClientErrorDecorator()
def cancel(self, name: str, team: str, execution_id: str) -> None:
Expand All @@ -121,19 +119,19 @@ def cancel(self, name: str, team: str, execution_id: str) -> None:

@ApiClientErrorDecorator()
def show(
self, name: str, team: str, execution_id: str, output: OutputFormat
self, name: str, team: str, execution_id: str, output_format: OutputFormat
) -> None:
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_format)

@ApiClientErrorDecorator()
def list(self, name: str, team: str, output: OutputFormat) -> None:
executions: list[
def list(self, name: str, team: str, output_format: OutputFormat) -> None:
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_format)

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 All @@ -27,7 +25,7 @@ def __init__(self, rest_api_url: str):

@ApiClientErrorDecorator()
def list_jobs(
self, team: str, jobs_for_all_teams: bool, more_details: int, output: str
self, team: str, jobs_for_all_teams: bool, more_details: int, output_format: str
):
has_more_jobs = True
page_number = 1
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_format).print_table(jobs)

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