From fb6f6396e2fe3e1342638af7e133f1532f7d4fe7 Mon Sep 17 00:00:00 2001 From: Ente Date: Sun, 12 Jan 2025 19:01:06 +0100 Subject: [PATCH] PPAPI-1: Endpoints, Gitops, Ldap, Motd, Registries --- CHANGELOG.md | 9 ++ src/Endpoints/Endpoints/Endpoints.php | 133 ++++++++++++++++++++++++ src/Endpoints/Gitops/Gitops.php | 24 +++++ src/Endpoints/LDAP/LDAP.php | 19 ++++ src/Endpoints/Motd/Motd.php | 18 ++++ src/Endpoints/Registries/Registries.php | 70 +++++++++++++ src/Helper/endpoints.json | 17 ++- src/Portainer.php | 12 +++ 8 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 CHANGELOG.md create mode 100644 src/Endpoints/Endpoints/Endpoints.php create mode 100644 src/Endpoints/Gitops/Gitops.php create mode 100644 src/Endpoints/LDAP/LDAP.php create mode 100644 src/Endpoints/Motd/Motd.php create mode 100644 src/Endpoints/Registries/Registries.php diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..5b1c1c8 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +## CHANGELOG.md + +## v0.2 (2025-01-12) + +- Added support to endpoint groups `endpoints` (incomplete), `gitops`, `ldap`, `motd`, `registries` + +## v0.1 (2024-12-15) + +- Initial release diff --git a/src/Endpoints/Endpoints/Endpoints.php b/src/Endpoints/Endpoints/Endpoints.php new file mode 100644 index 0000000..0e631b7 --- /dev/null +++ b/src/Endpoints/Endpoints/Endpoints.php @@ -0,0 +1,133 @@ +portainer = $portainer; + } + + /** + * `delete_endpoint()` - Remove multiple environments and optionally clean-up associated resources + * @param array $body Request body + * @return bool Returns true if the endpoint is deleted + */ + public function delete_endpoints(array $body){ + $response = $this->portainer->sendRequest(["endpoints" => $body], "endpoints", "DELETE", false, false); + return !$response; + } + + /** + * `get_endpoints()` - List all endpoints + * @param array $body Filter settings + * @return array + */ + public function get_endpoints(array $body){ + $response = $this->portainer->sendRequest([$body], "endpoints", "GET", false, false); + return $response; + } + + /** + * `create_endpoint()` - Create a new endpoint + * @param array $body endpoint configuration + * @return array + */ + public function create_endpoint(array $body){ + $response = $this->portainer->sendRequest([$body], "endpoints", "POST", false, false); + return $response; + } + + /** + * `delete_endpoint()` - Remove an endpoint + * @param int $id Endpoint ID + * @return bool Returns true if the endpoint is deleted + */ + public function delete_endpoint(int $id){ + $response = $this->portainer->sendRequest(["id" => $id], "endpoints/{$id}", "DELETE", false, true); + return !$response; + } + + /** + * `get_endpoint()` - Get an endpoint + * @param int $id Endpoint ID + * @return array + */ + public function get_endpoint(int $id){ + $response = $this->portainer->sendRequest(["id" => $id], "endpoints/{$id}", "GET", false, true); + return $response; + } + + /** + * `update_endpoint()` - Update an endpoint + * @param int $id Endpoint ID + * @param array $body endpoint configuration + * @return array + */ + public function update_endpoint(int $id, array $body){ + $response = $this->portainer->sendRequest(["id" => $id, "body" => $body], "endpoints/{$id}", "PUT", false, true); + return $response; + } + + /** + * `fetch_endpoint_registry_limits()` - Retrieve endpoint registry limits + * @param int $id Endpoint ID + * @return array + */ + public function fetch_endpoint_registry_limits(int $id, int $registryId){ + $response = $this->portainer->sendRequest(["id" => $id, "registryId" => $registryId], "endpoints/{$id}/dockerhub/{$registryId}", "GET", false, true); + return $response; + } + + /** + * `update_endpoint_registry_limits()` - Update endpoint docker service + * @param int $id Endpoint ID + * @param int $serviceId service ID + * @param array $body configuration + * @return array + */ + public function force_update_dockerService_endpoint(int $id, string $serviceId, bool $pullImage = true){ + $response = $this->portainer->sendRequest(["id" => $id, "serviceId" => $serviceId, "pullImage" => $pullImage], "endpoints/{$id}/forceupdateservice", "PUT", false, true); + return $response; + } + + /** + * `list_endpoint_registries()` - List endpoint registries + * @param int $id Endpoint ID + * @param string $namespace + * @return array + */ + public function list_endpoint_registries(int $id, string $namespace = ""){ + $response = $this->portainer->sendRequest(["id" => $id, "namespace" => $namespace], "endpoints/{$id}/registries", "GET", false, true); + return $response; + } + + /** + * `update_endpoint_settings()` - Update endpoint settings + * @param int $id Endpoint ID + * @param array $body configuration + * @return array + */ + public function update_endpoint_settings(int $id, array $body){ + $response = $this->portainer->sendRequest(["id" => $id, "body" => $body], "endpoints/{$id}/settings", "PUT", false, true); + return $response; + } + + /** + * `snapshot_endpoint()` - Snapshot an endpoint + * @param int $id Endpoint ID + * @return bool + */ + public function snapshot_endpoint(int $id){ + $response = $this->portainer->sendRequest(["id" => $id], "endpoints/{$id}/snapshot", "POST", false, true); + return !$response; + } + + /** + * `snapshot_all_endpoints()` - Snapshot all endpoints + * @return bool + */ + public function snapshot_all_endpoints(){ + $response = $this->portainer->sendRequest([], "endpoints/snapshot", "POST", false, false); + return !$response; + } +} diff --git a/src/Endpoints/Gitops/Gitops.php b/src/Endpoints/Gitops/Gitops.php new file mode 100644 index 0000000..f72541c --- /dev/null +++ b/src/Endpoints/Gitops/Gitops.php @@ -0,0 +1,24 @@ +portainer = $portainer; + } + + /** + * `preview()` - Preview a compose file based on git repository configuration + * @param string $username + * @param string $password + * @param string $reference + * @param string $repository + * @param string $targetFile + * @param bool $tlsskipverify + * @return bool + */ + public function preview(string $username, string $password, string $reference = "refs/heads/master", string $repository, string $targetFile, bool $tlsskipverify = false): bool{ + $response = $this->portainer->sendRequest(["username" => $username, "password" => $password, "reference" => $reference, "repository" => $repository, "targetFile" => $targetFile, "tlsskipverify" => $tlsskipverify], "gitops/preview", "POST", false, false); + return !$response; + } +} diff --git a/src/Endpoints/LDAP/LDAP.php b/src/Endpoints/LDAP/LDAP.php new file mode 100644 index 0000000..a71d285 --- /dev/null +++ b/src/Endpoints/LDAP/LDAP.php @@ -0,0 +1,19 @@ +portainer = $portainer; + } + + /** + * `check()` - Check LDAP settings + * @param array $body LDAP settings + * @return bool + */ + public function check(array $body){ + $response = $this->portainer->sendRequest([$body], "ldap/check", "POST", false, false); + return $response; + } +} diff --git a/src/Endpoints/Motd/Motd.php b/src/Endpoints/Motd/Motd.php new file mode 100644 index 0000000..5c62d60 --- /dev/null +++ b/src/Endpoints/Motd/Motd.php @@ -0,0 +1,18 @@ +portainer = $portainer; + } + + /** + * `motd()` - Get the message of the day + * @return array + */ + public function motd(){ + $response = $this->portainer->sendRequest([], "motd", "GET", false, false); + return $response; + } +} diff --git a/src/Endpoints/Registries/Registries.php b/src/Endpoints/Registries/Registries.php new file mode 100644 index 0000000..b922305 --- /dev/null +++ b/src/Endpoints/Registries/Registries.php @@ -0,0 +1,70 @@ +portainer = $portainer; + } + + /** + * `get_registries()` - Get all registries + * @return array + */ + public function get_registries(){ + $response = $this->portainer->sendRequest([], "registries", "GET", false, false); + return $response; + } + + /** + * `create_registry()` - Create a registry + * @param array $body Registry configuration + * @return array + */ + public function create_registry(array $body){ + $response = $this->portainer->sendRequest([$body], "registries", "POST", false, false); + return $response; + } + + /** + * `delete_registry()` - Delete a registry + * @param int $id Registry ID + * @return bool + */ + public function delete_registry(int $id){ + $response = $this->portainer->sendRequest([], "registries/" . $id, "DELETE", false, true); + return $response; + } + + /** + * `get_registry()` - Get a registry + * @param int $id Registry ID + * @return array + */ + public function get_registry(int $id){ + $response = $this->portainer->sendRequest([], "registries/" . $id, "GET", false, true); + return $response; + } + + /** + * `update_registry()` - Update a registry + * @param int $id Registry ID + * @param array $body Registry configuration + * @return array + */ + public function update_registry(int $id, array $body){ + $response = $this->portainer->sendRequest([$body], "registries/" . $id, "PUT", false, true); + return $response; + } + + /** + * `configure_registry()` - Configure a registry + * @param int $id Registry ID + * @param array $body Registry configuration + * @return bool + */ + public function configure_registry(int $id, array $body){ + $response = $this->portainer->sendRequest([$body], "registries/" . $id . "/configure", "POST", false, true); + return !$response; + } +} diff --git a/src/Helper/endpoints.json b/src/Helper/endpoints.json index e3d023c..1844b2d 100644 --- a/src/Helper/endpoints.json +++ b/src/Helper/endpoints.json @@ -9,5 +9,20 @@ "custom_templates", "custom_templates/create/file", "custom_templates/create/string", - "custom_templates/create/repository" + "custom_templates/create/repository", + + "endpoints", + "endpoints/global-key", + "endpoints/relations", + "endpoints/snapshot", + + "gitops/repo/file/preview", + + "templates/helm", + + "ldap/check", + + "motd", + + "registries" ] diff --git a/src/Portainer.php b/src/Portainer.php index 8ec4d1d..d091a9a 100644 --- a/src/Portainer.php +++ b/src/Portainer.php @@ -10,6 +10,11 @@ use Portainer\Endpoints\Backup; use Portainer\Endpoints\CustomTemplates; use Portainer\Endpoints\Docker; +use Portainer\Endpoints\Endpoints; +use Portainer\Endpoints\Gitops; +use Portainer\Endpoints\LDAP; +use Portainer\Endpoints\Motd; +use Portainer\Endpoints\Registries; class Portainer { private $username; @@ -22,6 +27,7 @@ class Portainer { private $backup; private $customTemplates; private $docker; + private $endpoints; public function __construct(string $envPath, string $envName , string $username = null, string $password = null, string $host = null){ $this->loader(); @@ -58,6 +64,7 @@ public function loader(){ require_once __DIR__ . "/Endpoints/Backup/Backup.php"; require_once __DIR__ . "/Endpoints/CustomTemplates/CustomTemplates.php"; require_once __DIR__ . "/Endpoints/Docker/Docker.php"; + require_once __DIR__ . "/Endpoints/Endpoints/Endpoints.php"; } public function login(){ @@ -237,4 +244,9 @@ public function docker(){ if(!$this->docker) $this->docker = new Docker($this); return $this->docker; } + + public function endpoints(){ + if(!$this->endpoints) $this->endpoints = new Endpoints($this); + return $this->endpoints; + } } \ No newline at end of file