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 support for paginated monitors #167

Merged
merged 1 commit into from
Aug 24, 2023
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
12 changes: 11 additions & 1 deletion datadog_sync/model/monitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import TYPE_CHECKING, Optional, List, Dict, cast

from datadog_sync.utils.base_resource import BaseResource, ResourceConfig
from datadog_sync.utils.custom_client import PaginationConfig

if TYPE_CHECKING:
from datadog_sync.utils.custom_client import CustomClient
Expand Down Expand Up @@ -36,9 +37,18 @@ class Monitors(BaseResource):
],
)
# Additional Monitors specific attributes
pagination_config = PaginationConfig(
page_size=100,
page_number_param="page",
page_size_param="page_size",
remaining_func=lambda *args: 1,
response_list_accessor=None,
)

def get_resources(self, client: CustomClient) -> List[Dict]:
resp = client.get(self.resource_config.base_path).json()
resp = client.paginated_request(client.get)(
self.resource_config.base_path, pagination_config=self.pagination_config
)

return resp

Expand Down
16 changes: 12 additions & 4 deletions datadog_sync/utils/custom_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,17 @@ def wrapper(*args, **kwargs):
resp.raise_for_status()

resp_json = resp.json()
resources.extend(resp_json["data"])
if len(resp_json["data"]) < page_size:
remaining = 0
continue
resp_len = 0
if pagination_config.response_list_accessor:
resources.extend(resp_json[pagination_config.response_list_accessor])
resp_len = len(resp_json[pagination_config.response_list_accessor])
else:
resources.extend(resp_json)
resp_len = len(resp_json)

if resp_len < page_size:
break

remaining = pagination_config.remaining_func(idx, resp_json, page_size, page_number)
page_number = pagination_config.page_number_func(idx, page_size, page_number)
idx += 1
Expand Down Expand Up @@ -163,3 +170,4 @@ class PaginationConfig(object):
page_number_param: Optional[str] = "page[number]"
remaining_func: Optional[Callable] = remaining_func
page_number_func: Optional[Callable] = page_number_func
response_list_accessor: Optional[str] = "data"
9 changes: 5 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@

tracer = None
try:
from ddtrace import config, patch
if os.getenv("DD_AGENT_HOST"):
from ddtrace import config, patch

config.httplib["distributed_tracing"] = True
config.requests["distributed_tracing"] = True
patch(httplib=True, requests=True)
config.httplib["distributed_tracing"] = True
config.requests["distributed_tracing"] = True
patch(httplib=True, requests=True)
except ImportError:
pass

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023-07-13T11:45:13.357791-04:00
2023-08-24T10:06:43.745795-04:00
2,545 changes: 1,919 additions & 626 deletions tests/integration/cassettes/test_cli/TestCli.test_cleanup.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023-07-13T11:44:26.267672-04:00
2023-08-24T10:05:52.402421-04:00
2,688 changes: 1,407 additions & 1,281 deletions tests/integration/cassettes/test_cli/TestCli.test_import.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023-07-13T11:44:30.260569-04:00
2023-08-24T10:05:55.640028-04:00
2,638 changes: 1,347 additions & 1,291 deletions tests/integration/cassettes/test_cli/TestCli.test_sync.yaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023-01-04T16:34:43.927845-05:00
2023-08-24T10:09:49.346349-04:00
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023-01-04T16:34:43.945229-05:00
2023-08-24T10:09:49.354636-04:00
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,23 @@ interactions:
- gzip, deflate
Content-Type:
- application/json
traceparent:
- 00-00000000000000009508ae4e0b6b527f-19c057d2dd41a74e-01
tracestate:
- dd=s:1;t.dm:-0
x-datadog-parent-id:
- '1855579609643460430'
x-datadog-sampling-priority:
- '1'
x-datadog-tags:
- _dd.p.dm=-0
x-datadog-trace-id:
- '10739024961687212671'
method: DELETE
uri: https://api.datadoghq.eu/api/v1/monitor/9380053?force=True
uri: https://api.datadoghq.eu/api/v1/monitor/12742272?force=True
response:
body:
string: '{"deleted_monitor_id": 9380053}'
string: '{"deleted_monitor_id": 12742272}'
headers: {}
status:
code: 200
Expand All @@ -26,11 +38,23 @@ interactions:
- gzip, deflate
Content-Type:
- application/json
traceparent:
- 00-0000000000000000e136065bacb87bbb-3fb554e5a7c2d43b-01
tracestate:
- dd=s:1;t.dm:-0
x-datadog-parent-id:
- '4590668740512896059'
x-datadog-sampling-priority:
- '1'
x-datadog-tags:
- _dd.p.dm=-0
x-datadog-trace-id:
- '16228165298085723067'
method: DELETE
uri: https://api.datadoghq.eu/api/v1/monitor/9380047?force=True
uri: https://api.datadoghq.eu/api/v1/monitor/12742271?force=True
response:
body:
string: '{"deleted_monitor_id": 9380047}'
string: '{"deleted_monitor_id": 12742271}'
headers: {}
status:
code: 200
Expand All @@ -44,11 +68,23 @@ interactions:
- gzip, deflate
Content-Type:
- application/json
traceparent:
- 00-0000000000000000643a0edc743014ee-ffa2030a825c4c33-01
tracestate:
- dd=s:1;t.dm:-0
x-datadog-parent-id:
- '18420288769570393139'
x-datadog-sampling-priority:
- '1'
x-datadog-tags:
- _dd.p.dm=-0
x-datadog-trace-id:
- '7222101292446913774'
method: DELETE
uri: https://api.datadoghq.eu/api/v1/monitor/9380049?force=True
uri: https://api.datadoghq.eu/api/v1/monitor/12742274?force=True
response:
body:
string: '{"deleted_monitor_id": 9380049}'
string: '{"deleted_monitor_id": 12742274}'
headers: {}
status:
code: 200
Expand All @@ -62,11 +98,23 @@ interactions:
- gzip, deflate
Content-Type:
- application/json
traceparent:
- 00-000000000000000043fa3a09cfef7e95-aa4c066c97edab4e-01
tracestate:
- dd=s:1;t.dm:-0
x-datadog-parent-id:
- '12271190148152929102'
x-datadog-sampling-priority:
- '1'
x-datadog-tags:
- _dd.p.dm=-0
x-datadog-trace-id:
- '4898291358536531605'
method: DELETE
uri: https://api.datadoghq.eu/api/v1/monitor/9380051?force=True
uri: https://api.datadoghq.eu/api/v1/monitor/12742275?force=True
response:
body:
string: '{"deleted_monitor_id": 9380051}'
string: '{"deleted_monitor_id": 12742275}'
headers: {}
status:
code: 200
Expand All @@ -80,11 +128,23 @@ interactions:
- gzip, deflate
Content-Type:
- application/json
traceparent:
- 00-0000000000000000378e81de1d16febc-d457418428aa4478-01
tracestate:
- dd=s:1;t.dm:-0
x-datadog-parent-id:
- '15300770294888285304'
x-datadog-sampling-priority:
- '1'
x-datadog-tags:
- _dd.p.dm=-0
x-datadog-trace-id:
- '4003279909749718716'
method: DELETE
uri: https://api.datadoghq.eu/api/v1/monitor/9380054?force=True
uri: https://api.datadoghq.eu/api/v1/monitor/12742273?force=True
response:
body:
string: '{"deleted_monitor_id": 9380054}'
string: '{"deleted_monitor_id": 12742273}'
headers: {}
status:
code: 200
Expand All @@ -98,11 +158,23 @@ interactions:
- gzip, deflate
Content-Type:
- application/json
traceparent:
- 00-000000000000000099a7d55763f9b1dd-ef3b7d8247e0381e-01
tracestate:
- dd=s:1;t.dm:-0
x-datadog-parent-id:
- '17238509997195802654'
x-datadog-sampling-priority:
- '1'
x-datadog-tags:
- _dd.p.dm=-0
x-datadog-trace-id:
- '11072052780229833181'
method: DELETE
uri: https://api.datadoghq.eu/api/v1/monitor/9380050?force=True
uri: https://api.datadoghq.eu/api/v1/monitor/12742269?force=True
response:
body:
string: '{"deleted_monitor_id": 9380050}'
string: '{"deleted_monitor_id": 12742269}'
headers: {}
status:
code: 200
Expand All @@ -116,11 +188,23 @@ interactions:
- gzip, deflate
Content-Type:
- application/json
traceparent:
- 00-0000000000000000e52fb0ce368f4567-b10aa7a25a76b11c-01
tracestate:
- dd=s:1;t.dm:-0
x-datadog-parent-id:
- '12757193210224619804'
x-datadog-sampling-priority:
- '1'
x-datadog-tags:
- _dd.p.dm=-0
x-datadog-trace-id:
- '16514612758316008807'
method: DELETE
uri: https://api.datadoghq.eu/api/v1/monitor/9380048?force=True
uri: https://api.datadoghq.eu/api/v1/monitor/12742270?force=True
response:
body:
string: '{"deleted_monitor_id": 9380048}'
string: '{"deleted_monitor_id": 12742270}'
headers: {}
status:
code: 200
Expand All @@ -134,29 +218,23 @@ interactions:
- gzip, deflate
Content-Type:
- application/json
traceparent:
- 00-0000000000000000667832788d872316-724b1ae6138983bf-01
tracestate:
- dd=s:1;t.dm:-0
x-datadog-parent-id:
- '8235705919049663423'
x-datadog-sampling-priority:
- '1'
x-datadog-tags:
- _dd.p.dm=-0
x-datadog-trace-id:
- '7383707082425836310'
method: DELETE
uri: https://api.datadoghq.eu/api/v1/monitor/9380046?force=True
uri: https://api.datadoghq.eu/api/v1/monitor/12742268?force=True
response:
body:
string: '{"deleted_monitor_id": 9380046}'
headers: {}
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Content-Type:
- application/json
method: DELETE
uri: https://api.datadoghq.eu/api/v1/monitor/9380052?force=True
response:
body:
string: '{"deleted_monitor_id": 9380052}'
string: '{"deleted_monitor_id": 12742268}'
headers: {}
status:
code: 200
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023-01-04T16:34:39.476177-05:00
2023-08-24T10:09:42.606412-04:00
Loading
Loading