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

Add global mute flag support #212

Merged
merged 3 commits into from
Feb 12, 2024
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ Options:
--force-missing-dependencies Force importing and syncing resources that
could be potential dependencies to the
requested resources. [sync only]
--create-global-downtime Scheduled downtime is meant to be removed
during failover when user determines
monitors have enough telemetry to trigger
appropriately.
--help Show this message and exit.

Commands:
Expand Down
9 changes: 9 additions & 0 deletions datadog_sync/commands/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@
help="Force importing and syncing resources that could be potential dependencies to the requested resources.",
cls=CustomOptionClass,
)
@option(
"--create-global-downtime",
required=False,
is_flag=True,
default=False,
help="Scheduled downtime is meant to be removed during failover when "
"user determines monitors have enough telemetry to trigger appropriately.",
cls=CustomOptionClass,
)
def sync(**kwargs):
"""Sync Datadog resources to destination."""
cfg = build_config(CMD_SYNC, **kwargs)
Expand Down
3 changes: 3 additions & 0 deletions datadog_sync/utils/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Configuration(object):
skip_failed_resource_connections: bool
max_workers: int
cleanup: int
create_global_downtime: bool
resources: Dict[str, BaseResource] = field(default_factory=dict)
resources_arg: List[str] = field(default_factory=list)

Expand Down Expand Up @@ -88,6 +89,7 @@ def build_config(cmd: str, **kwargs: Optional[Any]) -> Configuration:
force_missing_dependencies = kwargs.get("force_missing_dependencies")
skip_failed_resource_connections = kwargs.get("skip_failed_resource_connections")
max_workers = kwargs.get("max_workers", 10)
create_global_downtime = kwargs.get("create_global_downtime")

cleanup = kwargs.get("cleanup")
if cleanup:
Expand All @@ -108,6 +110,7 @@ def build_config(cmd: str, **kwargs: Optional[Any]) -> Configuration:
skip_failed_resource_connections=skip_failed_resource_connections,
max_workers=max_workers,
cleanup=cleanup,
create_global_downtime=create_global_downtime,
)

# Initialize resources
Expand Down
32 changes: 32 additions & 0 deletions datadog_sync/utils/resource_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,38 @@ def give_up_diffing(self, level, diff_instance) -> bool:
return False


def create_global_downtime(config: Configuration):
"""Create global downtime"""
payload = {
"data": {
"attributes": {
"message": "Downtime created by datadog-sync-cli to mute all monitors synced. "
"To be manually removed during failover when monitors have enough telemetry"
"to trigger appropriately.",
"monitor_identifier": {"monitor_tags": DEFAULT_TAGS},
"scope": "*",
"schedule": {
"start": None,
},
},
"type": "downtime",
}
}

try:
resp = config.destination_client.post(
config.resources["downtime_schedules"].resource_config.base_path, payload
).json()
config.logger.info(f"Global downtime for datadog-sync-cli created successfully - {resp['data']['id']}")
except CustomClientHTTPError as e:
if e.status_code == 400 and "downtime being created is a duplicate" in str(e):
config.logger.info(f"Global downtime for datadog-sync-cli already exists: {str(e)}")
else:
config.logger.error(f"Error creating global downtime for datadog-sync-cli: {str(e)}")
except Exception as e:
config.logger.error(f"Error creating global downtime for datadog-sync-cli: {str(e)}")


def find_attr(keys_list_str: str, resource_to_connect: str, r_obj: Any, connect_func: Callable) -> Optional[List[str]]:
if not r_obj:
return None
Expand Down
6 changes: 6 additions & 0 deletions datadog_sync/utils/resources_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
ResourceConnectionError,
SkipResource,
check_diff,
create_global_downtime,
dump_resources,
prep_resource,
thread_pool_executor,
Expand Down Expand Up @@ -88,6 +89,11 @@ def apply_resources(self) -> Tuple[int, int]:
# Run pre-apply hooks
for resource_type in set(self.resources_manager.all_resources.values()):
futures.append(parralel_executor.submit(self.config.resources[resource_type]._pre_apply_hook))

# Additional pre-apply actions
if self.config.create_global_downtime:
futures.append(parralel_executor.submit(create_global_downtime, self.config))

wait(futures)
for future in futures:
try:
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def config():
force_missing_dependencies=False,
skip_failed_resource_connections=True,
cleanup=False,
create_global_downtime=False,
)

resources = init_resources(cfg)
Expand Down