11# Copyright 2021-2023 VMware, Inc.
22# SPDX-License-Identifier: Apache-2.0
33import glob
4- import json
54import logging
65import os
76from typing import Optional
87
98import click
109import click_spinner
11- from tabulate import tabulate
1210from taurus_datajob_api import ApiException
1311from taurus_datajob_api import DataJob
1412from taurus_datajob_api import DataJobConfig
2119from vdk .internal .control .job .job_config import JobConfig
2220from vdk .internal .control .rest_lib .factory import ApiClientFactory
2321from vdk .internal .control .rest_lib .rest_client_errors import ApiClientErrorDecorator
22+ from vdk .internal .control .utils import output_printer
2423from vdk .internal .control .utils .cli_utils import get_or_prompt
2524from vdk .internal .control .utils .output_printer import OutputFormat
2625
@@ -31,14 +30,16 @@ class JobDeploy:
3130 ZIP_ARCHIVE_TYPE = "zip"
3231 ARCHIVE_SUFFIX = "-archive"
3332
34- def __init__ (self , rest_api_url : str , output ):
33+ def __init__ (self , rest_api_url : str , output : str ):
3534 self .deploy_api = ApiClientFactory (rest_api_url ).get_deploy_api ()
3635 self .jobs_api = ApiClientFactory (rest_api_url ).get_jobs_api ()
3736 self .job_sources_api = ApiClientFactory (rest_api_url ).get_jobs_sources_api ()
3837 # support for multiple deployments is not implemented yet so we can put anything here.
3938 # Ultimately this will be user facing parameter (possibly fetched from config.ini)
4039 self .__deployment_id = "production"
4140 self .__job_archive = JobArchive ()
41+ self .__output = output
42+ self .__printer = output_printer .create_printer (self .__output )
4243
4344 @staticmethod
4445 def __detect_keytab_files_in_job_directory (job_path : str ) -> None :
@@ -153,7 +154,6 @@ def update(
153154 enabled : Optional [bool ], # true, false or None
154155 job_version : Optional [str ],
155156 vdk_version : Optional [str ],
156- output : str ,
157157 ) -> None :
158158 deployment = DataJobDeployment (enabled = None )
159159 if job_version :
@@ -163,7 +163,7 @@ def update(
163163 deployment .enabled = enabled
164164
165165 if job_version :
166- self .__update_job_version (name , team , deployment , output )
166+ self .__update_job_version (name , team , deployment )
167167 elif vdk_version or enabled is not None :
168168 self .__update_deployment (name , team , deployment )
169169 msg = f"Deployment of Data Job { name } updated; "
@@ -186,16 +186,14 @@ def __update_deployment(
186186 data_job_deployment = deployment ,
187187 )
188188
189- def __update_job_version (
190- self , name : str , team : str , deployment : DataJobDeployment , output : str
191- ):
189+ def __update_job_version (self , name : str , team : str , deployment : DataJobDeployment ):
192190 log .debug (
193191 f"Update Deployment version of a job { name } of team { team } : { deployment } "
194192 )
195193 self .deploy_api .deployment_update (
196194 team_name = team , job_name = name , data_job_deployment = deployment
197195 )
198- if output == OutputFormat .TEXT .value :
196+ if self . __output == OutputFormat .TEXT .value :
199197 log .info (
200198 f"Request to deploy Data Job { name } using version { deployment .job_version } finished successfully.\n "
201199 f"It would take a few minutes for the Data Job to be deployed in the server.\n "
@@ -210,7 +208,7 @@ def __update_job_version(
210208 "job_name" : name ,
211209 "job_version" : deployment .job_version ,
212210 }
213- click . echo ( json . dumps (result ) )
211+ self . __printer . print_dict (result )
214212
215213 @ApiClientErrorDecorator ()
216214 def remove (self , name : str , team : str ) -> None :
@@ -221,7 +219,7 @@ def remove(self, name: str, team: str) -> None:
221219 log .info (f"Deployment of Data Job { name } removed." )
222220
223221 @ApiClientErrorDecorator ()
224- def show (self , name : str , team : str , output : str ) -> None :
222+ def show (self , name : str , team : str ) -> None :
225223 log .debug (f"Get list of deployments for job { name } of team { team } " )
226224 deployments = self .deploy_api .deployment_list (team_name = team , job_name = name )
227225 log .debug (
@@ -239,20 +237,17 @@ def show(self, name: str, team: str, output: str) -> None:
239237 ),
240238 deployments ,
241239 )
242- if output == OutputFormat .TEXT .value :
240+ if self . __output == OutputFormat .TEXT .value :
243241 click .echo (
244242 "You can compare the version seen here to the one seen when "
245243 "deploying to verify your deployment was successful."
246244 )
247245 click .echo ("" )
248- click . echo ( tabulate (deployments , headers = "keys" ))
246+ self . __printer . print_table ( list (deployments ))
249247 else :
250- click . echo ( json . dumps (list (deployments ) ))
248+ self . __printer . print_table (list (deployments ))
251249 else :
252- if output == OutputFormat .TEXT .value :
253- click .echo ("No deployments." )
254- else :
255- click .echo (json .dumps ([]))
250+ self .__printer .print_table (None )
256251
257252 @ApiClientErrorDecorator ()
258253 def create (
@@ -261,7 +256,6 @@ def create(
261256 team : str ,
262257 job_path : str ,
263258 reason : str ,
264- output : str ,
265259 vdk_version : Optional [str ],
266260 enabled : Optional [bool ],
267261 ) -> None :
@@ -287,7 +281,7 @@ def create(
287281 "Team Name" , team or job_config .get_team () or load_default_team_name ()
288282 )
289283
290- if output == OutputFormat .TEXT .value :
284+ if self . __output == OutputFormat .TEXT .value :
291285 log .info (
292286 f"Deploy Data Job with name { name } from directory { job_path } ... \n "
293287 )
@@ -298,9 +292,11 @@ def create(
298292 try :
299293 job_archive_binary = self .__archive_binary (archive_path )
300294
301- if output == OutputFormat .TEXT .value :
295+ if self . __output == OutputFormat .TEXT .value :
302296 log .info ("Uploading the data job might take some time ..." )
303- with click_spinner .spinner (disable = (output == OutputFormat .JSON .value )):
297+ with click_spinner .spinner (
298+ disable = (self .__output == OutputFormat .JSON .value )
299+ ):
304300 data_job_version = self .job_sources_api .sources_upload (
305301 team_name = team ,
306302 job_name = name ,
@@ -309,8 +305,6 @@ def create(
309305 )
310306
311307 self .__update_data_job_deploy_configuration (job_path , name , team )
312- self .update (
313- name , team , enabled , data_job_version .version_sha , vdk_version , output
314- )
308+ self .update (name , team , enabled , data_job_version .version_sha , vdk_version )
315309 finally :
316310 self .__cleanup_archive (archive_path = archive_path )
0 commit comments