Skip to content

Commit 90d3b32

Browse files
author
Nick Selpa
committed
Factored out duplicate code
1 parent ec906c6 commit 90d3b32

File tree

1 file changed

+29
-50
lines changed

1 file changed

+29
-50
lines changed

cloudendure/cloudendure.py

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -488,63 +488,42 @@ def get_machine_sync_details(self) -> List[Any]:
488488
# output to stdout for interactive usage
489489
return response_list
490490

491-
def get_machines_not_synced(self) -> List[Any]:
492-
"""Returns machines in a CloudEndure project which are either rescanning or
493-
backlogged for replication data.
494-
"""
495-
backlogged_machines: List[Any] = []
491+
def inspect_ce_project(self, check_type: str) -> List[Any]:
492+
if not check_type:
493+
print(f"ERROR: Unknown check_type of '{check_type}'; Please use 'not_synced', 'not_started', or 'not_current' ...")
494+
return
495+
result: List[Any] = []
496496
sync_report: List[Any] = self.get_machine_sync_details()
497-
print(f"INFO: Filtering for backlogged servers in Project: ({self.project_name})")
497+
print(f"INFO: Using check '{check_type}' on Project: ({self.project_name})")
498+
inspector = getattr(self, check_type)
498499
for item in sync_report:
499-
if item["backlogged_storage_bytes"] > 0 or item["rescanned_storage_bytes"] > 0:
500-
backlogged_machines.append(item)
501-
if len(backlogged_machines) > 0:
502-
print(f"INFO: {len(backlogged_machines)} machines are backlogged in Project: ({self.project_name})")
503-
return backlogged_machines
500+
mcheck = inspector(machine=item)
501+
if mcheck:
502+
result.append(item)
503+
print(f"INFO: Check '{check_type}' completed; {len(result)} machines matched in Project: ({self.project_name})")
504+
return result
505+
506+
def not_synced(self, machine) -> bool:
507+
if machine["backlogged_storage_bytes"] > 0 or machine["rescanned_storage_bytes"] > 0:
508+
return True
504509
else:
505-
print(f"INFO: All machines are in Continuous Data Replication in Project: ({self.project_name})")
510+
return False
506511

507-
def get_machines_not_started(self) -> List[Any]:
508-
"""Returns machines in a CloudEndure project which are not in a STARTED
509-
state.
510-
"""
511-
not_started_machines: List[Any] = []
512-
sync_report: List[Any] = self.get_machine_sync_details()
513-
print(f"INFO: Getting replication not STARTED for machines in Project: ({self.project_name})")
514-
for item in sync_report:
515-
if item["replication_status"] != "STARTED":
516-
not_started_machines.append(item)
517-
if len(not_started_machines) > 0:
518-
print(f"INFO: {len(not_started_machines)} machines not STARTED found in Project: ({self.project_name})")
519-
return not_started_machines
512+
def not_started(self, machine) -> bool:
513+
if machine["replication_status"] == "STARTED":
514+
return False
520515
else:
521-
print(f"INFO: All machines are STARTED in Project: ({self.project_name})")
522-
523-
def get_stale_machines(self, delta_seconds: int = 86400) -> List[Any]:
524-
"""Returns machines in a CloudEndure project which have not been seen by
525-
the CloudEndure console for greater than 24 hours (86400 seconds [default]).
526-
"""
527-
stale_machines: List[Any] = []
528-
sync_report: List[Any] = self.get_machine_sync_details()
529-
print(
530-
f"INFO: Getting stale machines (not seen for {delta_seconds} seconds or later) in Project: ({self.project_name})"
531-
)
516+
return True
517+
518+
def not_current(self, machine, delta_seconds: int=86400) -> bool:
532519
now: datetime = datetime.now(timezone.utc)
533-
for item in sync_report:
534-
item_last_seen: datetime = datetime.fromisoformat(item["last_seen_utc"])
535-
last_seen_delta: datetime = now - item_last_seen
536-
# If you're exceeding the size of int, you have bigger problems
537-
if int(last_seen_delta.total_seconds()) >= delta_seconds:
538-
stale_machines.append(item)
539-
if len(stale_machines) > 0:
540-
print(
541-
f"INFO: {len(stale_machines)} machines not seen for {delta_seconds} seconds found in Project: ({self.project_name})"
542-
)
543-
return stale_machines
520+
machine_last_seen: datetime = datetime.fromisoformat(machine["last_seen_utc"])
521+
last_seen_delta: datetime = now - machine_last_seen
522+
# If you're exceeding the size of int, you have bigger problems
523+
if int(last_seen_delta.total_seconds()) >= delta_seconds:
524+
return True
544525
else:
545-
print(
546-
f"INFO: All machines have been seen at least {delta_seconds} seconds ago in Project: ({self.project_name})"
547-
)
526+
return False
548527

549528
def update_blueprint(self) -> bool:
550529
"""Update the blueprint associated with the specified machines."""

0 commit comments

Comments
 (0)