Skip to content

Commit 5a4fe35

Browse files
committed
jq logic exp for Request
1 parent 368c991 commit 5a4fe35

2 files changed

Lines changed: 75 additions & 21 deletions

File tree

example/README.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,3 @@ hatch run development
1313
crossplane render xr.yaml composition.yaml functions.yaml \
1414
--required-resources secret.yaml --extra-resources environment.yaml -r
1515
```
16-
17-
```
18-
---
19-
apiVersion: eos.netclab.dev/v1alpha1
20-
kind: EosCommand
21-
metadata:
22-
name: eoscommand-1
23-
spec:
24-
endpoint: ceos01.default.svc.cluster.local
25-
cmds:
26-
ip prefix-list PL-Loopback0:
27-
seq 10 permit 10.0.0.1/32 eq 32: {}
28-
seq 20 permit 10.0.0.2/32 eq 32: {}
29-
---
30-
<...>
31-
```

function/fn.py

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,45 @@ async def RunFunction(
4646
"insecureSkipTLSVerify": insecure_skip_tls_verify,
4747
}
4848

49+
jsonrpc_observe_params = {
50+
"version": 1,
51+
"format": "json",
52+
"cmds": ["enable", "show running-config"],
53+
}
54+
jsonrpc_observe = request_json("runCmds", params=jsonrpc_observe_params)
55+
4956
command_paths = walk_cmds(cmds)
5057
log.info("Generated command paths", count=len(command_paths))
5158

5259
for path in command_paths:
5360
name = name_prefix + "-" + name_from_path(path)
5461

55-
jsonrpc_params = {
62+
jsonrpc_create_params = {
5663
"version": 1,
5764
"format": "json",
5865
"cmds": ["enable", "configure", *path],
5966
}
60-
jsonrpc = request_json("runCmds", params=jsonrpc_params)
67+
jsonrpc_create = request_json("runCmds", params=jsonrpc_create_params)
68+
69+
remove_path = [*path[:-1], f"no {path[-1]}"]
70+
jsonrpc_remove_params = {
71+
"version": 1,
72+
"format": "json",
73+
"cmds": ["enable", "configure", *remove_path],
74+
}
75+
jsonrpc_remove = request_json("runCmds", params=jsonrpc_remove_params)
76+
77+
expected_logic, removed_logic = create_jq_logic_expressions(path)
78+
79+
jsonrpc_ops = {
80+
"create": jsonrpc_create,
81+
"observe": jsonrpc_observe,
82+
"remove": jsonrpc_remove,
83+
"expectedResponseCheck": expected_logic,
84+
"isRemovedCheck": removed_logic,
85+
}
6186

62-
resource_data = construct_resource_request(jsonrpc, jsonrpc_cfg)
87+
resource_data = construct_resource_request(jsonrpc_ops, jsonrpc_cfg)
6388

6489
resource.update(
6590
rsp.desired.resources[name],
@@ -69,6 +94,17 @@ async def RunFunction(
6994
return rsp
7095

7196

97+
def create_jq_logic_expressions(path: list[str]) -> tuple[str, str]:
98+
"""Create jq logic exp for Request."""
99+
logic = ".response.body.error == null and ("
100+
for cmd in path[:-1]:
101+
logic = logic + f'.response.body.result[1].cmds."{cmd}".cmds'
102+
logic1 = logic + f' | has("{path[-1]}")' + ")"
103+
logic2 = logic + f' | has("{path[-1]}")' + " | not)"
104+
105+
return logic1, logic2
106+
107+
72108
def walk_cmds(cmds: dict, path: list[str] | None = None) -> list[list[str]]:
73109
"""walk_cmds function."""
74110
if path is None:
@@ -110,7 +146,7 @@ def get_envs(environment: dict) -> tuple[int, str, bool]:
110146
return port, scheme, insecure_skip_tls_verify
111147

112148

113-
def construct_resource_request(jsonrpc: str, config: dict) -> dict:
149+
def construct_resource_request(ops: dict, config: dict) -> dict:
114150
"""Construct the resource request for the given data."""
115151
return {
116152
"apiVersion": "http.crossplane.io/v1alpha2",
@@ -124,7 +160,41 @@ def construct_resource_request(jsonrpc: str, config: dict) -> dict:
124160
},
125161
"payload": {
126162
"baseUrl": f"{config['scheme']}://{config['endpoint']}:{config['port']}",
127-
"body": jsonrpc,
163+
"body": ops["create"],
164+
},
165+
"mappings": [
166+
{
167+
"action": "CREATE",
168+
"method": "POST",
169+
"url": ".payload.baseUrl",
170+
"body": ".payload.body",
171+
},
172+
{
173+
"action": "UPDATE",
174+
"method": "POST",
175+
"url": ".payload.baseUrl",
176+
"body": ".payload.body",
177+
},
178+
{
179+
"action": "OBSERVE",
180+
"method": "POST",
181+
"url": ".payload.baseUrl",
182+
"body": ops["observe"],
183+
},
184+
{
185+
"action": "REMOVE",
186+
"method": "POST",
187+
"url": ".payload.baseUrl",
188+
"body": ops["remove"],
189+
},
190+
],
191+
"expectedResponseCheck": {
192+
"type": "CUSTOM",
193+
"logic": ops["expectedResponseCheck"],
194+
},
195+
"isRemovedCheck": {
196+
"type": "CUSTOM",
197+
"logic": ops["isRemovedCheck"],
128198
},
129199
},
130200
},

0 commit comments

Comments
 (0)