python-rundeck 0.1.0__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.
@@ -0,0 +1,104 @@
1
+ """
2
+ Gestion des webhooks projet et envoi d'événements.
3
+ """
4
+
5
+ from typing import Any
6
+
7
+ from rundeck.base import RundeckObject, RundeckObjectManager
8
+
9
+
10
+ class Webhook(RundeckObject):
11
+ """Représente un webhook de projet."""
12
+
13
+ _id_attr = "id"
14
+ _repr_attr = "name"
15
+
16
+
17
+ class ProjectWebhookManager(RundeckObjectManager[Webhook]):
18
+ """Manager pour les webhooks d'un projet."""
19
+
20
+ _path = "/project/{parent}/webhook"
21
+ _obj_cls = Webhook
22
+
23
+ def list(self) -> list[Webhook]:
24
+ """Liste les webhooks d'un projet."""
25
+ if not self.parent:
26
+ raise ValueError("Parent projet requis pour lister les webhooks")
27
+ path = f"/project/{self.parent.id}/webhooks"
28
+ return self._list(path=path)
29
+
30
+ def get(self, webhook_id: str | int) -> Webhook:
31
+ """Récupère un webhook par son ID."""
32
+ path = self._build_path(str(webhook_id))
33
+ return self._get(str(webhook_id), path=path)
34
+
35
+ def create(self, **data: Any) -> Any:
36
+ """
37
+ Crée un webhook de projet.
38
+
39
+ Args:
40
+ data: Champs du webhook (name, user, roles, eventPlugin, config,
41
+ enabled, project...).
42
+ """
43
+ payload = dict(data)
44
+ if self.parent and "project" not in payload:
45
+ payload["project"] = self.parent.id
46
+ path = self._build_path()
47
+ return self.rd.http_post(path, json=payload or None)
48
+
49
+ def update(self, webhook_id: str | int, **data: Any) -> Any:
50
+ """Met à jour un webhook de projet."""
51
+ # L'API attend souvent les champs existants (roles, eventPlugin, config,
52
+ # user, project, id).
53
+ current_dict: dict[str, Any] = {}
54
+ try:
55
+ current = self.get(webhook_id)
56
+ current_dict = current.to_dict()
57
+ except Exception:
58
+ current_dict = {}
59
+
60
+ payload: dict[str, Any] = {}
61
+ payload.update({k: v for k, v in current_dict.items() if v is not None})
62
+ payload.update({k: v for k, v in data.items() if v is not None})
63
+ payload.setdefault("id", webhook_id)
64
+ if self.parent:
65
+ payload["project"] = self.parent.id
66
+
67
+ path = self._build_path(str(webhook_id))
68
+ return self.rd.http_post(path, json=payload or None)
69
+
70
+ def delete(self, webhook_id: str | int) -> None:
71
+ """Supprime un webhook de projet."""
72
+ path = self._build_path(str(webhook_id))
73
+ self.rd.http_delete(path)
74
+
75
+
76
+ class WebhookEventManager(RundeckObjectManager):
77
+ """Manager pour envoyer des événements sur un webhook (via auth token)."""
78
+
79
+ _path = "/webhook"
80
+
81
+ def send(
82
+ self,
83
+ auth_token: str,
84
+ *,
85
+ json: dict[str, Any] | None = None,
86
+ data: Any = None,
87
+ headers: dict[str, str] | None = None,
88
+ ) -> Any:
89
+ """
90
+ Envoie un payload vers un webhook par son token.
91
+
92
+ Args:
93
+ auth_token: Token d'authentification du webhook.
94
+ json: Payload JSON (optionnel).
95
+ data: Payload brut (si json non fourni).
96
+ headers: En-têtes additionnels (facultatif).
97
+ """
98
+ path = self._build_path(auth_token)
99
+ if json is not None:
100
+ return self.rd.http_post(path, json=json, headers=headers)
101
+ return self.rd.http_post(path, data=data, headers=headers)
102
+
103
+
104
+ __all__ = ["Webhook", "ProjectWebhookManager", "WebhookEventManager"]