From d7cee78c222ccb6c806edc1d5e2f9df90038ef11 Mon Sep 17 00:00:00 2001 From: Dave the Dev Date: Mon, 6 Jul 2026 00:07:18 +0000 Subject: [PATCH] feat(portainer-automation): add deploy controller with X-API-Key auth --- .../controller/DeployController.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 portainer-automation/src/main/java/com/hithomelabs/portainer/controller/DeployController.java diff --git a/portainer-automation/src/main/java/com/hithomelabs/portainer/controller/DeployController.java b/portainer-automation/src/main/java/com/hithomelabs/portainer/controller/DeployController.java new file mode 100644 index 0000000..cb20fe9 --- /dev/null +++ b/portainer-automation/src/main/java/com/hithomelabs/portainer/controller/DeployController.java @@ -0,0 +1,43 @@ +package com.hithomelabs.portainer.controller; + +import com.hithomelabs.portainer.config.PortainerAutomationProperties; +import com.hithomelabs.portainer.service.DeployService; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/deploy") +public class DeployController { + + private final DeployService deployService; + private final String serviceApiKey; + + public DeployController(DeployService deployService, + PortainerAutomationProperties properties) { + this.deployService = deployService; + this.serviceApiKey = properties.getService().getApiKey(); + } + + @PostMapping("/{stackId}") + public ResponseEntity deploy(@PathVariable Long stackId, + @RequestHeader("X-API-Key") String apiKey) { + if (serviceApiKey == null || !serviceApiKey.equals(apiKey)) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED) + .body("Invalid or missing X-API-Key"); + } + + try { + deployService.deploy(stackId); + return ResponseEntity.ok("Stack " + stackId + " redeploy triggered"); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("Redeploy failed: " + e.getMessage()); + } + } +}