dinemic-cli 0.0.19__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. dinemic_cli/__init__.py +1 -0
  2. dinemic_cli/__main__.py +16 -0
  3. dinemic_cli/client.py +75 -0
  4. dinemic_cli/commands/__init__.py +0 -0
  5. dinemic_cli/commands/auth.py +31 -0
  6. dinemic_cli/commands/caa.py +11 -0
  7. dinemic_cli/commands/cert.py +29 -0
  8. dinemic_cli/commands/cert_peer.py +37 -0
  9. dinemic_cli/commands/dns_entry.py +57 -0
  10. dinemic_cli/commands/dns_value.py +43 -0
  11. dinemic_cli/commands/dns_zone.py +98 -0
  12. dinemic_cli/commands/endpoint.py +39 -0
  13. dinemic_cli/commands/healthcheck.py +40 -0
  14. dinemic_cli/commands/loadbalancer.py +39 -0
  15. dinemic_cli/commands/loadbalancer_backend.py +44 -0
  16. dinemic_cli/commands/loadbalancer_host.py +37 -0
  17. dinemic_cli/commands/loadbalancer_route.py +42 -0
  18. dinemic_cli/commands/network.py +34 -0
  19. dinemic_cli/commands/node.py +29 -0
  20. dinemic_cli/commands/peer.py +33 -0
  21. dinemic_cli/commands/peer_config.py +33 -0
  22. dinemic_cli/commands/project.py +58 -0
  23. dinemic_cli/commands/quota.py +22 -0
  24. dinemic_cli/main.py +68 -0
  25. dinemic_cli/output.py +25 -0
  26. dinemic_cli-0.0.19.dist-info/METADATA +13 -0
  27. dinemic_cli-0.0.19.dist-info/RECORD +63 -0
  28. dinemic_cli-0.0.19.dist-info/WHEEL +5 -0
  29. dinemic_cli-0.0.19.dist-info/entry_points.txt +2 -0
  30. dinemic_cli-0.0.19.dist-info/licenses/LICENSE +696 -0
  31. dinemic_cli-0.0.19.dist-info/top_level.txt +2 -0
  32. tests/01-cli-fixes.patch +418 -0
  33. tests/02-tests-after-fixes.patch +208 -0
  34. tests/api_samples.py +214 -0
  35. tests/cert_peer.py +37 -0
  36. tests/cli_runner.py +113 -0
  37. tests/command_matrix.py +612 -0
  38. tests/conftest.py +235 -0
  39. tests/dns_entry.py +41 -0
  40. tests/dns_value.py +43 -0
  41. tests/dns_zone.py +38 -0
  42. tests/endpoint.py +39 -0
  43. tests/healthcheck.py +40 -0
  44. tests/loadbalancer.py +36 -0
  45. tests/loadbalancer_backend.py +44 -0
  46. tests/loadbalancer_host.py +37 -0
  47. tests/loadbalancer_route.py +42 -0
  48. tests/network.py +34 -0
  49. tests/network_node.py +74 -0
  50. tests/node.py +23 -0
  51. tests/peer.py +33 -0
  52. tests/project.py +58 -0
  53. tests/support.py +246 -0
  54. tests/test_auth.py +114 -0
  55. tests/test_columns.py +58 -0
  56. tests/test_commands.py +98 -0
  57. tests/test_confirmations.py +71 -0
  58. tests/test_envvars.py +119 -0
  59. tests/test_error_handling.py +83 -0
  60. tests/test_lifecycle.py +188 -0
  61. tests/test_network_node.py +116 -0
  62. tests/test_option_contract.py +335 -0
  63. tests/test_smoke.py +83 -0
tests/api_samples.py ADDED
@@ -0,0 +1,214 @@
1
+ """Sample API responses — the CLI/backend contract, offline edition.
2
+
3
+ This is a HAND-RECONSTRUCTED picture of what the backend serializers return.
4
+ It serves two purposes:
5
+
6
+ 1. unit tests get realistic data instead of empty dicts,
7
+ 2. ``test_columns.py`` checks that the columns declared by the commands actually
8
+ exist in the response — which catches columns orphaned by a backend refactor.
9
+
10
+ NOTE: the backend serializers are the source of truth, not this file. When the
11
+ API changes, update the samples HERE and run
12
+ ``pytest -m integration -k columns``, which verifies the same columns against
13
+ the live API and has the final word.
14
+
15
+ Fields marked ``# [VERIFY]`` are names I reconstructed from the architecture
16
+ doc — confirm them on the first integration run.
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ import re
22
+ from typing import Any
23
+
24
+ PROJECT_ID = "prj-0000000001"
25
+ NETWORK_ID = "net-0000000001"
26
+ NODE_ID = "nod-0000000001"
27
+ ZONE_ID = "zon-0000000001"
28
+ ENTRY_ID = "ent-0000000001"
29
+ VALUE_ID = "val-0000000001"
30
+ LB_ID = "lba-0000000001"
31
+ HOST_ID = "hos-0000000001"
32
+ ROUTE_ID = "rou-0000000001"
33
+ BACKEND_ID = "bck-0000000001"
34
+ ENDPOINT_ID = "end-0000000001"
35
+ HEALTHCHECK_ID = "hlc-0000000001"
36
+ PEER_ID = "pee-0000000001"
37
+ CERT_ID = "crt-0000000001"
38
+ CERT_PEER_ID = "cpr-0000000001"
39
+ CAA_ID = "caa-0000000001"
40
+ NETWORK_NODE_ID = "nnd-0000000001"
41
+
42
+ SAMPLES: dict[str, list[dict[str, Any]]] = {
43
+ r"^/project/$": [
44
+ {
45
+ "id": PROJECT_ID,
46
+ "name": "projekt1",
47
+ "public_key": "REPLACE-WITH-A-TEST-KEY-NOT-A-REAL-ONE=",
48
+ "auto_accept_nodes": True,
49
+ }
50
+ ],
51
+ r"^/network/$": [
52
+ {
53
+ "id": NETWORK_ID,
54
+ "name": "mesh",
55
+ "project": PROJECT_ID,
56
+ "ip_address": "10.0.0.0",
57
+ "ip_netmask": "255.255.255.0",
58
+ }
59
+ ],
60
+ r"^/node/$": [
61
+ {
62
+ "id": NODE_ID,
63
+ "project": PROJECT_ID,
64
+ "public_key": "Uo9Xn0mQ2Yl0rLGm3Q9m1Yb2Zx4bQ0m6r8v3s1t5u7w=",
65
+ "description": "vps-fra-1",
66
+ "created": "2026-01-04T10:00:00Z",
67
+ "sync_time": "2026-01-04T10:05:00Z",
68
+ "state": "active",
69
+ "ipv4_address": "203.0.113.10",
70
+ "ipv6_address": "2001:db8::10",
71
+ "continent_code": "EU",
72
+ "country_code": "DE",
73
+ "city_name": "Frankfurt",
74
+ }
75
+ ],
76
+ r"^/peer/$": [
77
+ {
78
+ "id": PEER_ID,
79
+ "node": NODE_ID,
80
+ "network": NETWORK_ID,
81
+ # [VERIFY] the doc (section 2 "Peer", 5.9 to_dict) calls this field
82
+ # ``ipv4_address``; ``peer list`` renders ``ip_address``
83
+ "ipv4_address": "10.0.0.5",
84
+ "service_port": 40001,
85
+ "role": "loadbalancer",
86
+ "configs": [LB_ID],
87
+ # [VERIFY] with NetworkNode in place the WireGuard keys and port
88
+ # should hang off NetworkNode, not off the peer
89
+ "network_node": NETWORK_NODE_ID,
90
+ }
91
+ ],
92
+ r"^/network-node/$": [
93
+ {
94
+ "id": NETWORK_NODE_ID,
95
+ "network": NETWORK_ID,
96
+ "node": NODE_ID,
97
+ "ipv4_address": "10.0.0.5",
98
+ "wg_public_key": "Kx9Yb2Zx4bQ0m6r8v3s1t5u7wUo9Xn0mQ2Yl0rLGm3Q=",
99
+ "wg_port": 30000,
100
+ "state": "active",
101
+ }
102
+ ],
103
+ r"^/endpoint/$": [
104
+ {
105
+ "id": ENDPOINT_ID,
106
+ "project": PROJECT_ID,
107
+ "name": "api-backend",
108
+ "scheme": "HTTP",
109
+ "host": "127.0.0.1",
110
+ "port": 8080,
111
+ "tags": [],
112
+ }
113
+ ],
114
+ r"^/healthcheck/$": [
115
+ {
116
+ "id": HEALTHCHECK_ID,
117
+ "name": "http-ok",
118
+ "project": PROJECT_ID,
119
+ "path": "/healthz",
120
+ "period": 10,
121
+ "max_fail": 3,
122
+ "initial_delay": 30,
123
+ }
124
+ ],
125
+ r"^/loadbalancer/$": [
126
+ {"id": LB_ID, "name": "lb-eu", "project": PROJECT_ID}
127
+ ],
128
+ r"^/loadbalancer/[^/]+/host/$": [
129
+ {"id": HOST_ID, "domain": ZONE_ID, "loadbalancer": LB_ID}
130
+ ],
131
+ r"^/loadbalancer/[^/]+/host/[^/]+/route/$": [
132
+ {
133
+ "id": ROUTE_ID,
134
+ "path": "/",
135
+ "host": HOST_ID,
136
+ "healthcheck": HEALTHCHECK_ID,
137
+ "priority": 10,
138
+ }
139
+ ],
140
+ r"^/loadbalancer/[^/]+/host/[^/]+/route/[^/]+/backend/$": [
141
+ {"id": BACKEND_ID, "endpoint": ENDPOINT_ID, "weight": 1, "route": ROUTE_ID}
142
+ ],
143
+ r"^/zone/$": [
144
+ {"id": ZONE_ID, "domain": "example.com", "project": PROJECT_ID, "name": "example.com"}
145
+ ],
146
+ r"^/zone/[^/]+/entry/$": [
147
+ {"id": ENTRY_ID, "record": "www", "type": "A", "ttl": 300, "zone": ZONE_ID}
148
+ ],
149
+ r"^/zone/[^/]+/entry/[^/]+/value/$": [
150
+ {"id": VALUE_ID, "value": "203.0.113.10", "weight": 1, "entry": ENTRY_ID}
151
+ ],
152
+ r"^/caa/$": [{"id": CAA_ID, "name": "letsencrypt.org"}],
153
+ r"^/certificate/$": [
154
+ {
155
+ "id": CERT_ID,
156
+ "project": PROJECT_ID,
157
+ "common_name": "example.com",
158
+ "status": "DISTRIBUTED",
159
+ "domains": ["example.com", "www.example.com"],
160
+ "not_before": "2026-01-01T00:00:00Z",
161
+ "not_after": "2026-04-01T00:00:00Z",
162
+ "expires_in_days": 61,
163
+ "issued_at": "2026-01-01T00:05:00Z",
164
+ "needs_renewal": False,
165
+ "delivered_to": [PEER_ID],
166
+ "attempts": 0,
167
+ "error": "",
168
+ }
169
+ ],
170
+ r"^/certificate-peer/$": [
171
+ {
172
+ "id": CERT_PEER_ID,
173
+ "name": "acme-1",
174
+ "project": PROJECT_ID,
175
+ "directory_url": "https://acme-v02.api.letsencrypt.org/directory",
176
+ "email": "ops@example.com",
177
+ "max_parallel": 2,
178
+ "dns_servers": ["10.0.0.5"],
179
+ "lb_recipients": [PEER_ID],
180
+ }
181
+ ],
182
+ }
183
+
184
+ _COMPILED = [(re.compile(pattern), rows) for pattern, rows in SAMPLES.items()]
185
+ _DETAIL = re.compile(r"^(?P<collection>.*/)[^/]+/$")
186
+
187
+
188
+ def sample_list(path: str) -> list[dict[str, Any]]:
189
+ """The list of objects the backend would return for this path."""
190
+ for pattern, rows in _COMPILED:
191
+ if pattern.match(path):
192
+ return [dict(row) for row in rows]
193
+ raise KeyError(
194
+ f"No sample response for path {path!r}. Add one to SAMPLES in api_samples.py."
195
+ )
196
+
197
+
198
+ def sample_object(path: str) -> dict[str, Any]:
199
+ return sample_list(path)[0]
200
+
201
+
202
+ def sample_for(path: str):
203
+ """A list for a collection path, a single object for a detail path."""
204
+ try:
205
+ return sample_list(path)
206
+ except KeyError:
207
+ match = _DETAIL.match(path)
208
+ if match:
209
+ return sample_object(match.group("collection"))
210
+ raise
211
+
212
+
213
+ def sample_keys(path: str) -> set[str]:
214
+ return set(sample_object(path))
tests/cert_peer.py ADDED
@@ -0,0 +1,37 @@
1
+ import typer
2
+
3
+ from dinemic_cli.output import render
4
+
5
+ app = typer.Typer(no_args_is_help=True)
6
+
7
+ PRODUCTION_DIRECTORY = "https://acme-v02.api.letsencrypt.org/directory"
8
+
9
+
10
+ @app.command("list")
11
+ def list_cert_peers(ctx: typer.Context):
12
+ data = ctx.obj["client"].get("/certificate-peer/")
13
+ render(data, ctx.obj["output"], columns=["id", "name", "project", "directory_url", "email", "max_parallel", "dns_servers", "lb_recipients"])
14
+
15
+
16
+ @app.command("create")
17
+ def create_cert_peer(
18
+ ctx: typer.Context,
19
+ project: str = typer.Option(..., "--project", "-p", help="Project ID", envvar="DINEMIC_PROJECT"),
20
+ name: str = typer.Option(..., "--name", "-n", help="Name"),
21
+ directory_url: str = typer.Option(PRODUCTION_DIRECTORY, "--directory-url", "-d", help="Let's Encrypt directory url. For tests use https://acme-staging-v02.api.letsencrypt.org/directory"),
22
+ max_parallel: int = typer.Option(..., "--max-parallel", "-m", help="Maximum number of certificates processed by one peer at once"),
23
+ ):
24
+ data = ctx.obj["client"].post("/certificate-peer/", {"project": project, "name": name, "directory_url": directory_url, "max_parallel": max_parallel})
25
+ typer.secho(f"Created certificate peer {data['id']}", fg="green")
26
+
27
+
28
+ @app.command("delete")
29
+ def delete_cert_peer(
30
+ ctx: typer.Context,
31
+ cert_peer_id: str = typer.Option(..., "--cert-peer", "-c", help="Certificate Peer ID"),
32
+ yes: bool = typer.Option(False, "--yes", "-y", help="Don't ask for confirmation"),
33
+ ):
34
+ if not yes:
35
+ typer.confirm(f"Remove Certificate Peer {cert_peer_id}? It will stop renewing your certificates if there is no redundant peer", abort=True)
36
+ ctx.obj["client"].delete(f"/certificate-peer/{cert_peer_id}/")
37
+ typer.secho("Removed.", fg="green")
tests/cli_runner.py ADDED
@@ -0,0 +1,113 @@
1
+ """Running the real CLI in a subprocess, plus the command-path map.
2
+
3
+ Integration tests invoke the installed command through ``subprocess`` rather
4
+ than calling Python functions. That is the only way to catch a broken entry
5
+ point, a bad wheel layout or a missing keyring backend — exactly the things unit
6
+ tests cannot see (sections 7.7 and 7.8 of the architecture doc).
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import json
12
+ import os
13
+ import re
14
+ from dataclasses import dataclass
15
+
16
+ TIMEOUT = 60
17
+
18
+ #: Read at import time. The ``clean_env`` fixture wipes ``os.environ`` before
19
+ #: every unit test, so by the time a session-scoped fixture runs these would be
20
+ #: gone.
21
+ API_URL = os.environ.get("DINEMIC_API_URL")
22
+ API_TOKEN = os.environ.get("DINEMIC_API_TOKEN")
23
+
24
+ #: How ``main.py`` mounts the sub-apps: command module -> path in the CLI.
25
+ #: THE ONLY place to edit if you rename the groups in main.py.
26
+ CLI_PATH = {
27
+ "auth": ["auth"],
28
+ "caa": ["caa"],
29
+ "cert": ["cert"],
30
+ "cert_peer": ["cert-peer"],
31
+ "dns_zone": ["zone"],
32
+ "dns_entry": ["zone", "entry"],
33
+ "dns_value": ["zone", "entry", "value"],
34
+ "endpoint": ["endpoint"],
35
+ "healthcheck": ["healthcheck"],
36
+ "loadbalancer": ["loadbalancer"],
37
+ "loadbalancer_host": ["loadbalancer", "host"],
38
+ "loadbalancer_route": ["loadbalancer", "host", "route"],
39
+ "loadbalancer_backend": ["loadbalancer", "host", "route", "backend"],
40
+ "network": ["network"],
41
+ "node": ["node"],
42
+ "peer": ["peer"],
43
+ "project": ["project"],
44
+ }
45
+
46
+
47
+ def path_of(module: str) -> list[str]:
48
+ return CLI_PATH[module]
49
+
50
+
51
+ @dataclass
52
+ class CliResult:
53
+ args: list[str]
54
+ exit_code: int
55
+ stdout: str
56
+ stderr: str
57
+ data: object | None = None
58
+
59
+ def __repr__(self) -> str:
60
+ return (
61
+ f"<dinemic {' '.join(self.args)} -> {self.exit_code}\n"
62
+ f"stdout: {self.stdout.strip()}\nstderr: {self.stderr.strip()}>"
63
+ )
64
+
65
+ def ok(self) -> "CliResult":
66
+ assert self.exit_code == 0, f"Command failed:\n{self!r}"
67
+ return self
68
+
69
+
70
+ def parse_json(stdout: str):
71
+ stdout = stdout.strip()
72
+ if not stdout:
73
+ return None
74
+ try:
75
+ return json.loads(stdout)
76
+ except json.JSONDecodeError:
77
+ match = re.search(r"[\[{].*[\]}]", stdout, re.S)
78
+ if not match:
79
+ return None
80
+ try:
81
+ return json.loads(match.group(0))
82
+ except json.JSONDecodeError:
83
+ return None
84
+
85
+
86
+ ID_PATTERN = re.compile(r"\(([^()\s]+)\)\s*$")
87
+
88
+
89
+ def extract_id(result: CliResult) -> str:
90
+ """Pull the object ID out of a ``create`` message.
91
+
92
+ The ``create`` commands print a human sentence ("Created network mesh (id)")
93
+ instead of JSON, even under ``--output json``. Until that changes the ID has
94
+ to be scraped with a regex, which is brittle and is therefore documented by
95
+ ``test_create_output_is_machine_readable``.
96
+ """
97
+ if isinstance(result.data, dict) and "id" in result.data:
98
+ return result.data["id"]
99
+
100
+ line = result.stdout.strip().splitlines()[-1] if result.stdout.strip() else ""
101
+ match = ID_PATTERN.search(line)
102
+ if match:
103
+ return match.group(1)
104
+ tokens = line.split()
105
+ assert tokens, f"Cannot extract an ID from: {result!r}"
106
+ return tokens[-1]
107
+
108
+
109
+ def find_by(rows, **criteria):
110
+ for row in rows or []:
111
+ if all(row.get(key) == value for key, value in criteria.items()):
112
+ return row
113
+ return None