88: Add DeployController with X-API-Key auth
This commit is contained in:
parent
21e67dadea
commit
76dd43c7b2
@ -0,0 +1,41 @@
|
||||
package com.hithomelabs.portainer.controller;
|
||||
|
||||
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;
|
||||
|
||||
import com.hithomelabs.portainer.config.PortainerAutomationProperties;
|
||||
import com.hithomelabs.portainer.service.DeployService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/deploy")
|
||||
public class DeployController {
|
||||
|
||||
private final DeployService deployService;
|
||||
private final PortainerAutomationProperties props;
|
||||
|
||||
public DeployController(DeployService deployService, PortainerAutomationProperties props) {
|
||||
this.deployService = deployService;
|
||||
this.props = props;
|
||||
}
|
||||
|
||||
@PostMapping("/{stackId}")
|
||||
public ResponseEntity<String> deploy(@PathVariable Long stackId,
|
||||
@RequestHeader(value = "X-API-Key", required = false) String apiKey) {
|
||||
String expectedApiKey = props.getService().getApiKey();
|
||||
if (expectedApiKey == null || !expectedApiKey.equals(apiKey)) {
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid API key");
|
||||
}
|
||||
try {
|
||||
deployService.redeploy(stackId);
|
||||
return ResponseEntity.ok("Deployment initiated for stack " + stackId);
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body("Deployment failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user