Skip to content

Commit

Permalink
more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
iakov-aws committed Nov 15, 2024
1 parent 9a4d9b7 commit 895f475
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
43 changes: 23 additions & 20 deletions cid/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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 <BOLD>{dashboard.cid_version}<END> to <BOLD>{dashboard.latest_available_cid_version}<END>')
Expand Down Expand Up @@ -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}"')
Expand Down
4 changes: 2 additions & 2 deletions cid/helpers/quicksight/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 895f475

Please sign in to comment.