Skip to content

Commit 71c150d

Browse files
authored
Add create_diff to the clients to manually create a diff between two times. (#531)
* feat: add create_diff method and update get_diff_summary to accept name parameter * feat: enhance get_diff_summary to support time range filtering * fix: update action field in diff summary to reflect correct status * feat: add create_diff method and update get_diff_summary for time range support * feat: add create_diff method and enhance get_diff_summary to support time range filtering * fix: remove unnecessary blank lines in InfrahubClientSync class * feat: update create_diff method to validate time range and change return type
1 parent f4035f0 commit 71c150d

File tree

4 files changed

+84
-8
lines changed

4 files changed

+84
-8
lines changed

changelog/529.added.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add `create_diff` method to create a diff summary between two timestamps
2+
Update `get_diff_summary` to accept optional time range parameters

infrahub_sdk/client.py

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
import time
77
from collections.abc import Coroutine, MutableMapping
8+
from datetime import datetime
89
from functools import wraps
910
from time import sleep
1011
from typing import (
@@ -24,6 +25,7 @@
2425

2526
from .batch import InfrahubBatch, InfrahubBatchSync
2627
from .branch import (
28+
MUTATION_QUERY_TASK,
2729
BranchData,
2830
InfrahubBranchManager,
2931
InfrahubBranchManagerSync,
@@ -1154,21 +1156,57 @@ async def query_gql_query(
11541156

11551157
return decode_json(response=resp)
11561158

1159+
async def create_diff(
1160+
self, branch: str, name: str, from_time: datetime, to_time: datetime, wait_until_completion: bool = True
1161+
) -> bool | str:
1162+
if from_time > to_time:
1163+
raise ValueError("from_time must be <= to_time")
1164+
input_data = {
1165+
"wait_until_completion": wait_until_completion,
1166+
"data": {
1167+
"name": name,
1168+
"branch": branch,
1169+
"from_time": from_time.isoformat(),
1170+
"to_time": to_time.isoformat(),
1171+
},
1172+
}
1173+
1174+
mutation_query = MUTATION_QUERY_TASK if not wait_until_completion else {"ok": None}
1175+
query = Mutation(mutation="DiffUpdate", input_data=input_data, query=mutation_query)
1176+
response = await self.execute_graphql(query=query.render(), tracker="mutation-diff-update")
1177+
1178+
if not wait_until_completion and "task" in response["DiffUpdate"]:
1179+
return response["DiffUpdate"]["task"]["id"]
1180+
1181+
return response["DiffUpdate"]["ok"]
1182+
11571183
async def get_diff_summary(
11581184
self,
11591185
branch: str,
1186+
name: str | None = None,
1187+
from_time: datetime | None = None,
1188+
to_time: datetime | None = None,
11601189
timeout: int | None = None,
11611190
tracker: str | None = None,
11621191
raise_for_error: bool = True,
11631192
) -> list[NodeDiff]:
11641193
query = get_diff_summary_query()
1194+
input_data = {"branch_name": branch}
1195+
if name:
1196+
input_data["name"] = name
1197+
if from_time and to_time and from_time > to_time:
1198+
raise ValueError("from_time must be <= to_time")
1199+
if from_time:
1200+
input_data["from_time"] = from_time.isoformat()
1201+
if to_time:
1202+
input_data["to_time"] = to_time.isoformat()
11651203
response = await self.execute_graphql(
11661204
query=query,
11671205
branch_name=branch,
11681206
timeout=timeout,
11691207
tracker=tracker,
11701208
raise_for_error=raise_for_error,
1171-
variables={"branch_name": branch},
1209+
variables=input_data,
11721210
)
11731211

11741212
node_diffs: list[NodeDiff] = []
@@ -2293,21 +2331,57 @@ def query_gql_query(
22932331

22942332
return decode_json(response=resp)
22952333

2334+
def create_diff(
2335+
self, branch: str, name: str, from_time: datetime, to_time: datetime, wait_until_completion: bool = True
2336+
) -> bool | str:
2337+
if from_time > to_time:
2338+
raise ValueError("from_time must be <= to_time")
2339+
input_data = {
2340+
"wait_until_completion": wait_until_completion,
2341+
"data": {
2342+
"name": name,
2343+
"branch": branch,
2344+
"from_time": from_time.isoformat(),
2345+
"to_time": to_time.isoformat(),
2346+
},
2347+
}
2348+
2349+
mutation_query = MUTATION_QUERY_TASK if not wait_until_completion else {"ok": None}
2350+
query = Mutation(mutation="DiffUpdate", input_data=input_data, query=mutation_query)
2351+
response = self.execute_graphql(query=query.render(), tracker="mutation-diff-update")
2352+
2353+
if not wait_until_completion and "task" in response["DiffUpdate"]:
2354+
return response["DiffUpdate"]["task"]["id"]
2355+
2356+
return response["DiffUpdate"]["ok"]
2357+
22962358
def get_diff_summary(
22972359
self,
22982360
branch: str,
2361+
name: str | None = None,
2362+
from_time: datetime | None = None,
2363+
to_time: datetime | None = None,
22992364
timeout: int | None = None,
23002365
tracker: str | None = None,
23012366
raise_for_error: bool = True,
23022367
) -> list[NodeDiff]:
23032368
query = get_diff_summary_query()
2369+
input_data = {"branch_name": branch}
2370+
if name:
2371+
input_data["name"] = name
2372+
if from_time and to_time and from_time > to_time:
2373+
raise ValueError("from_time must be <= to_time")
2374+
if from_time:
2375+
input_data["from_time"] = from_time.isoformat()
2376+
if to_time:
2377+
input_data["to_time"] = to_time.isoformat()
23042378
response = self.execute_graphql(
23052379
query=query,
23062380
branch_name=branch,
23072381
timeout=timeout,
23082382
tracker=tracker,
23092383
raise_for_error=raise_for_error,
2310-
variables={"branch_name": branch},
2384+
variables=input_data,
23112385
)
23122386

23132387
node_diffs: list[NodeDiff] = []

infrahub_sdk/diff.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class NodeDiffPeer(TypedDict):
3737

3838
def get_diff_summary_query() -> str:
3939
return """
40-
query GetDiffTree($branch_name: String!) {
41-
DiffTree(branch: $branch_name) {
40+
query GetDiffTree($branch_name: String!, $name: String, $from_time: DateTime, $to_time: DateTime) {
41+
DiffTree(branch: $branch_name, name: $name, from_time: $from_time, to_time: $to_time) {
4242
nodes {
4343
uuid
4444
kind
@@ -121,7 +121,7 @@ def diff_tree_node_to_node_diff(node_dict: dict[str, Any], branch_name: str) ->
121121
branch=branch_name,
122122
kind=str(node_dict.get("kind")),
123123
id=str(node_dict.get("uuid")),
124-
action=str(node_dict.get("action")),
124+
action=str(node_dict.get("status")),
125125
display_label=str(node_dict.get("label")),
126126
elements=element_diffs,
127127
)

tests/unit/sdk/test_diff_summary.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ async def test_diffsummary(clients: BothClients, mock_diff_tree_query, client_ty
109109
"branch": "branch2",
110110
"kind": "TestCar",
111111
"id": "17fbadf0-6637-4fa2-43e6-1677ea170e0f",
112-
"action": "None",
112+
"action": "UPDATED",
113113
"display_label": "nolt #444444",
114114
"elements": [
115115
{
@@ -124,7 +124,7 @@ async def test_diffsummary(clients: BothClients, mock_diff_tree_query, client_ty
124124
"branch": "branch2",
125125
"kind": "TestPerson",
126126
"id": "17fbadf0-634f-05a8-43e4-1677e744d4c0",
127-
"action": "None",
127+
"action": "UPDATED",
128128
"display_label": "Jane",
129129
"elements": [
130130
{
@@ -140,7 +140,7 @@ async def test_diffsummary(clients: BothClients, mock_diff_tree_query, client_ty
140140
"branch": "branch2",
141141
"kind": "TestPerson",
142142
"id": "17fbadf0-6243-5d3c-43ee-167718ff8dac",
143-
"action": "None",
143+
"action": "UPDATED",
144144
"display_label": "Jonathan",
145145
"elements": [
146146
{

0 commit comments

Comments
 (0)