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

HAMR-164: Add migrate command #287

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions datadog_sync/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from datadog_sync.commands.sync import sync
from datadog_sync.commands._import import _import
from datadog_sync.commands.diffs import diffs
from datadog_sync.commands.migrate import migrate


ALL_COMMANDS = [
sync,
_import,
diffs,
migrate,
]
2 changes: 1 addition & 1 deletion datadog_sync/commands/_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

from click import command

from datadog_sync.constants import Command
from datadog_sync.commands.shared.options import (
common_options,
destination_auth_options,
source_auth_options,
)
from datadog_sync.commands.shared.utils import run_cmd
from datadog_sync.constants import Command


@command(Command.IMPORT.value, short_help="Import Datadog resources.")
Expand Down
8 changes: 4 additions & 4 deletions datadog_sync/commands/diffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

from datadog_sync.commands.shared.options import (
common_options,
source_auth_options,
destination_auth_options,
non_import_common_options,
diffs_common_options,
source_auth_options,
)
from datadog_sync.constants import Command
from datadog_sync.commands.shared.utils import run_cmd
from datadog_sync.constants import Command


@command(Command.DIFFS.value, short_help="Log resource diffs.")
@source_auth_options
@destination_auth_options
@common_options
@non_import_common_options
@diffs_common_options
def diffs(**kwargs):
"""Log Datadog resources diffs."""
run_cmd(Command.DIFFS, **kwargs)
27 changes: 27 additions & 0 deletions datadog_sync/commands/migrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Unless explicitly stated otherwise all files in this repository are licensed
# under the 3-clause BSD style license (see LICENSE).
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2019 Datadog, Inc.

from click import command

from datadog_sync.commands.shared.options import (
common_options,
destination_auth_options,
diffs_common_options,
source_auth_options,
sync_common_options,
)
from datadog_sync.commands.shared.utils import run_cmd
from datadog_sync.constants import Command


@command(Command.MIGRATE.value, short_help="Migrate Datadog resources from one datacenter to another.")
@source_auth_options
@destination_auth_options
@common_options
@diffs_common_options
@sync_common_options
def migrate(**kwargs):
"""Migrate Datadog resources from one datqaacenter to another."""
run_cmd(Command.MIGRATE, **kwargs)
43 changes: 37 additions & 6 deletions datadog_sync/commands/shared/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def click_config_file_provider(ctx: Context, opts: CustomOptionClass, value: Non
type=int,
default=60,
show_default=True,
help="The HTTP request retry timeout period. Defaults to 60s",
help="The HTTP request retry timeout period in seconds.",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the show_default=True already shows the default of 60.

cls=CustomOptionClass,
),
option(
Expand All @@ -104,7 +104,7 @@ def click_config_file_provider(ctx: Context, opts: CustomOptionClass, value: Non
type=int,
default=30,
show_default=True,
help="The HTTP request timeout period. Defaults to 30s",
help="The HTTP request timeout period in seconds.",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the show_default=True already shows the default of 30.

cls=CustomOptionClass,
),
option(
Expand All @@ -126,6 +126,7 @@ def click_config_file_provider(ctx: Context, opts: CustomOptionClass, value: Non
"--max-workers",
envvar=constants.MAX_WORKERS,
default=100,
show_default=True,
required=False,
type=int,
help="Max number of workers when running operations in multi-threads.",
Expand All @@ -136,6 +137,7 @@ def click_config_file_provider(ctx: Context, opts: CustomOptionClass, value: Non
envvar=constants.DD_FILTER_OPERATOR,
required=False,
default="OR",
show_default=True,
help="Filter operator when multiple filters are passed. Supports `AND` or `OR`.",
cls=CustomOptionClass,
),
Expand All @@ -161,21 +163,23 @@ def click_config_file_provider(ctx: Context, opts: CustomOptionClass, value: Non
default=True,
show_default=True,
help="Enables validation of the provided API during client initialization. On import, "
"only source api key is validated. On sync/diffs, only destination api key is validated.",
"only source api key is validated. On sync/diffs, only destination api key is validated. "
"On migrate, both source and destination api keys are validated.",
cls=CustomOptionClass,
),
option(
"--send-metrics",
type=bool,
required=False,
default=True,
show_default=True,
help="Enables sync-cli metrics being sent to both source and destination",
cls=CustomOptionClass,
),
]


_non_import_common_options = [
_diffs_common_options = [
option(
"--skip-failed-resource-connections",
type=bool,
Expand All @@ -199,6 +203,29 @@ def click_config_file_provider(ctx: Context, opts: CustomOptionClass, value: Non
]


_sync_common_options = [
option(
"--force-missing-dependencies",
required=False,
is_flag=True,
default=False,
show_default=True,
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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably switch the create-global-downtime to default to true since we almost always want it to happen.

show_default=True,
help="Scheduled downtime is meant to be removed during failover when "
"user determines monitors have enough telemetry to trigger appropriately.",
cls=CustomOptionClass,
),
]


def source_auth_options(func: Callable) -> Callable:
return _build_options_helper(func, _source_auth_options)

Expand All @@ -211,8 +238,12 @@ def common_options(func: Callable) -> Callable:
return _build_options_helper(func, _common_options)


def non_import_common_options(func: Callable) -> Callable:
return _build_options_helper(func, _non_import_common_options)
def diffs_common_options(func: Callable) -> Callable:
return _build_options_helper(func, _diffs_common_options)


def sync_common_options(func: Callable) -> Callable:
return _build_options_helper(func, _sync_common_options)


def _build_options_helper(func: Callable, options: List[Callable]) -> Callable:
Expand Down
5 changes: 4 additions & 1 deletion datadog_sync/commands/shared/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def run_cmd(cmd: Command, **kwargs):
asyncio.run(run_cmd_async(cfg, handler, cmd))
except KeyboardInterrupt:
cfg.logger.error("Process interrupted by user")
if cmd == Command.SYNC:
if cmd in [Command.SYNC, Command.MIGRATE]:
cfg.logger.info("Writing synced resources to disk before exit...")
cfg.state.dump_state()
exit(0)
Expand All @@ -41,6 +41,9 @@ async def run_cmd_async(cfg: Configuration, handler: ResourcesHandler, cmd: Comm
await handler.apply_resources()
elif cmd == Command.DIFFS:
await handler.diffs()
elif cmd == Command.MIGRATE:
await handler.import_resources()
await handler.apply_resources()
else:
cfg.logger.error(f"Command {cmd.value} not found")
return
Expand Down
30 changes: 7 additions & 23 deletions datadog_sync/commands/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,25 @@
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2019 Datadog, Inc.

from click import command, option
from click import command

from datadog_sync.constants import Command
from datadog_sync.commands.shared.options import (
CustomOptionClass,
common_options,
source_auth_options,
destination_auth_options,
non_import_common_options,
diffs_common_options,
source_auth_options,
sync_common_options,
)
from datadog_sync.commands.shared.utils import run_cmd
from datadog_sync.constants import Command


@command(Command.SYNC.value, short_help="Sync Datadog resources to destination.")
@source_auth_options
@destination_auth_options
@common_options
@non_import_common_options
@option(
"--force-missing-dependencies",
required=False,
is_flag=True,
default=False,
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,
)
@diffs_common_options
@sync_common_options
def sync(**kwargs):
"""Sync Datadog resources to destination."""
run_cmd(Command.SYNC, **kwargs)
1 change: 1 addition & 0 deletions datadog_sync/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Command(Enum):
IMPORT = "import"
SYNC = "sync"
DIFFS = "diffs"
MIGRATE = "migrate"


# Origin
Expand Down
4 changes: 2 additions & 2 deletions datadog_sync/utils/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ async def init_async(self, cmd: Command):
# Validate the clients. For import we only validate the source client
# For sync/diffs we validate the destination client.
if self.validate:
if cmd in [Command.SYNC, Command.DIFFS]:
if cmd in [Command.SYNC, Command.DIFFS, Command.MIGRATE]:
try:
await _validate_client(self.destination_client)
except Exception:
exit(1)
if cmd == Command.IMPORT:
if cmd in [Command.IMPORT, Command.MIGRATE]:
try:
await _validate_client(self.source_client)
except Exception:
Expand Down
Loading