From 895f4752e3680add86feff6446024c144080531a Mon Sep 17 00:00:00 2001 From: Iakov Gan Date: Fri, 15 Nov 2024 23:46:33 +0100 Subject: [PATCH] more refactoring --- cid/common.py | 43 ++++++++++++++++-------------- cid/helpers/quicksight/__init__.py | 4 +-- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/cid/common.py b/cid/common.py index 0def26bb..598097f2 100644 --- a/cid/common.py +++ b/cid/common.py @@ -300,7 +300,7 @@ def track(self, action, dashboard_id): logger.debug(f"Issue logging action {action} for dashboard {dashboard_id} , due to a urllib3 exception {str(e)} . This issue will be ignored") def get_page(self, source): - resp = requests.get(source, timeout=10, headers={'User-Agent': 'cid'}) + resp = requests.get(source, timeout=10) resp.raise_for_status() return resp @@ -391,6 +391,7 @@ def get_template_parameters(self, parameters: dict, param_prefix: str='', others message=f"Required parameter: {key} ({value.get('description')})", choices=options, default=default if default in options else None, + fuzzy=False, ) elif isinstance(value, dict): params[key] = value.get('value') @@ -435,7 +436,7 @@ def _deploy(self, dashboard_id: str=None, recursive=True, update=False, **kwargs # In case if we cannot discover datasets, we need to discover dashboards # TODO: check if datasets returns explicit permission denied and only then discover dashboards as a workaround - self.qs.dashboards + self.qs.discover_dashboards() dashboard_id = dashboard_id or get_parameters().get('dashboard-id') category_filter = [cat for cat in get_parameters().get('category', '').upper().split(',') if cat] @@ -527,14 +528,15 @@ def _deploy(self, dashboard_id: str=None, recursive=True, update=False, **kwargs compatible = self.check_dashboard_version_compatibility(dashboard_id) if not recursive and compatible == False: - if get_parameter( - param_name=f'confirm-recursive', - message=f'This is a major update and require recursive action. This could lead to the loss of dataset customization. Continue anyway?', - choices=['yes', 'no'], - default='yes') != 'yes': - return - logger.info("Switch to recursive mode") - recursive = True + if get_parameters().get('confirm-recursive') != 'no': + if get_yesno_parameter( + param_name=f'confirm-recursive', + message=f'This is a major update and require recursive action. This could lead to the loss of dataset customization. Continue anyway?', + default='yes') != 'yes': + return + else: + logger.info("Switch to recursive mode") + recursive = True if recursive: self.create_datasets(required_datasets_names, dashboard_datasets, recursive=recursive, update=update) @@ -782,7 +784,8 @@ def delete_dataset(self, name: str, id: str=None): # Check if dataset is used in some other dashboard for dashboard in (self.qs.dashboards or {}).values(): if dataset.id in dashboard.datasets.values(): - cid_print(f'Dataset {dataset.name} ({dataset.id}) is still used by dashboard "{dashboard.id}". Skipping.') + logger.info(f'Dataset {dataset.name} ({dataset.id}) is still used by dashboard "{dashboard.id}". Skipping.') + print (f'Dataset {dataset.name} ({dataset.id}) is still used by dashboard "{dashboard.id}". Skipping.') return False else: #not used @@ -792,10 +795,9 @@ def delete_dataset(self, name: str, id: str=None): logger.debug(f'Picking the first of dataset databases: {dataset.schemas}') self.athena.DatabaseName = schema - if get_parameter( + if get_yesno_parameter( param_name=f'confirm-{dataset.name}', message=f'Delete QuickSight Dataset {dataset.name}?', - choices=['yes', 'no'], default='no') == 'yes': print(f'Deleting dataset {dataset.name} ({dataset.id})') self.qs.delete_dataset(dataset.id) @@ -1072,10 +1074,10 @@ def check_dashboard_version_compatibility(self, dashboard_id): if dashboard.latest: cid_print("You are up to date!") - cid_print(f" Version {dashboard.cid_version}") + cid_print(f" Version {str(dashboard.cid_version)}") else: cid_print(f"An update is available:") - cid_print(f" Version {dashboard.cid_version} -> {dashboard.latest_available_cid_version}") + cid_print(f" Version {str(dashboard.cid_version): <9} -> {str(dashboard.latest_available_cid_version): <9}") try: return dashboard.cid_version.compatible_versions(dashboard.latest_available_cid_version) @@ -1090,10 +1092,10 @@ def update_dashboard(self, dashboard_id, dashboard_definition): print(f'Dashboard "{dashboard_id}" is not deployed') return - if isinstance(dashboard.deployedTemplate, CidQsTemplate): - print(f'Deployed template: {dashboard.deployedTemplate.arn}') - if isinstance(dashboard.source_template, CidQsTemplate): - print(f"Latest template: {dashboard.source_template.arn}/version/{dashboard.source_template.version}") + if isinstance(dashboard.deployed_template, CidQsTemplate): + print(f'Deployed template: {dashboard.deployed_template.arn}') + if isinstance(dashboard.sourceTemplate, CidQsTemplate): + print(f"Latest template: {dashboard.sourceTemplate.arn}/version/{dashboard.latest_version}") try: cid_print(f'\nUpdating {dashboard_id} from {dashboard.cid_version} to {dashboard.latest_available_cid_version}') @@ -1146,7 +1148,8 @@ def create_datasets(self, _datasets: list, known_datasets: dict={}, recursive: b param_name=f'{dataset_name}-dataset-id', message=f'Multiple "{dataset_name}" datasets detected, please select one', choices=[v.id for v in datasets], - default=datasets[0].id + default=datasets[0].id, + fuzzy=False, ) known_datasets.update({dataset_name: dataset_id}) print(f'Updating dataset: "{dataset_name}"') diff --git a/cid/helpers/quicksight/__init__.py b/cid/helpers/quicksight/__init__.py index 8c8b096b..15a78c92 100644 --- a/cid/helpers/quicksight/__init__.py +++ b/cid/helpers/quicksight/__init__.py @@ -255,7 +255,7 @@ def _safe_int(value, default=None): try: _template = self.describe_template(**params) if isinstance(_template, CidQsTemplate): - dashboard.deployedTemplate = _template + dashboard.deployed_template = _template except Exception as exc: logger.debug(exc, exc_info=True) logger.info(f'Unable to describe template for {dashboardId}, {exc}') @@ -303,7 +303,7 @@ def _safe_int(value, default=None): logger.info(f'Unable to describe template {template_id} in {source_template_account_id} ({region})') # Checking for version override in template definition - for dashboard_template in [dashboard.deployedTemplate, dashboard.source_template]: + for dashboard_template in [dashboard.deployed_template, dashboard.source_template]: if not isinstance(dashboard_template, CidQsTemplate)\ or int(dashboard_template.version) <= 0 \ or not version_obj: