Skip to content

Commit

Permalink
refactor and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skarimo committed May 2, 2024
1 parent 7c00b72 commit 2887886
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
23 changes: 15 additions & 8 deletions datadog_sync/model/logs_indexes_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,7 @@ async def create_resource(self, _id: str, resource: Dict) -> Tuple[str, Dict]:

async def update_resource(self, _id: str, resource: Dict) -> Tuple[str, Dict]:
destination_resources = self.destination_indexes_order or self.resource_config.destination_resources[_id]
ids_to_omit = set(resource["index_names"]) - set(destination_resources["index_names"])

extra_ids_to_include = [
_id for _id in destination_resources["index_names"] if _id not in resource["index_names"]
]

resource["index_names"] = [_id for _id in resource["index_names"] if _id not in ids_to_omit]
resource["index_names"] = resource["index_names"] + extra_ids_to_include
self.handle_additional_indexes(resource, destination_resources)

destination_client = self.config.destination_client
resp = await destination_client.put(self.resource_config.base_path, resource)
Expand All @@ -91,3 +84,17 @@ async def get_destination_indexes_order(self):
resp = await self.get_resources(destination_client)

return resp[0]

@staticmethod
def handle_additional_indexes(resource, destination_resource) -> None:
# Logs index order requires all logs indexes in the destination org to be included in the payload
# Additional indexes in the source org which need to be removed from the payload
ids_to_omit = set(resource["index_names"]) - set(destination_resource["index_names"])
resource["index_names"] = [_id for _id in resource["index_names"] if _id not in ids_to_omit]

# Add back additional indexes present in the destination org while retaining the relative ordering
# of the additional indexes
extra_ids_to_include = [
_id for _id in destination_resource["index_names"] if _id not in resource["index_names"]
]
resource["index_names"] = resource["index_names"] + extra_ids_to_include
36 changes: 36 additions & 0 deletions tests/integration/resources/test_logs_indexes_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,39 @@ class TestLogsIndexesOrder(BaseResourcesTestClass):
@pytest.mark.skip(reason="resource is only updated by default")
def test_resource_update_sync(self):
pass


@pytest.mark.parametrize(
"resource, destination_resource, expected",
[
(
{"index_names": ["index1", "index2", "index3"]},
{"index_names": ["index3", "index2", "index4"]},
{"index_names": ["index2", "index3", "index4"]},
),
(
{"index_names": ["index1"]},
{"index_names": ["index3", "index1", "index4"]},
{"index_names": ["index1", "index3", "index4"]},
),
(
{"index_names": ["index1", "index2", "index3"]},
{"index_names": ["index3", "index1"]},
{"index_names": ["index1", "index3"]},
),
(
{"index_names": ["index1", "index2", "index3"]},
{"index_names": ["index1"]},
{"index_names": ["index1"]},
),
(
{"index_names": ["index1"]},
{"index_names": ["index5", "index1", "index3", "index4"]},
{"index_names": ["index1", "index5", "index3", "index4"]},
),
],
)
def test_handle_index_diff(resource, destination_resource, expected):
LogsIndexesOrder.handle_additional_indexes(resource, destination_resource)

assert resource == expected

0 comments on commit 2887886

Please sign in to comment.