deployments.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package handlers
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "git.linuxforward.com/byop/byop-engine/models"
  7. "git.linuxforward.com/byop/byop-engine/services"
  8. "github.com/gin-gonic/gin"
  9. )
  10. // DeploymentHandler handles deployment-related operations
  11. type DeploymentHandler struct {
  12. service *services.DeploymentService
  13. }
  14. // NewDeploymentHandler creates a new DeploymentHandler
  15. func NewDeploymentHandler(service *services.DeploymentService) *DeploymentHandler {
  16. return &DeploymentHandler{
  17. service: service,
  18. }
  19. }
  20. // RegisterRoutes registers routes for deployment operations
  21. func (h *DeploymentHandler) RegisterRoutes(r *gin.RouterGroup) {
  22. r.GET("/", h.ListDeployments)
  23. r.POST("/", h.CreateDeployment)
  24. r.GET("/:id", h.GetDeployment)
  25. r.PUT("/:id", h.UpdateDeployment)
  26. r.DELETE("/:id", h.DeleteDeployment)
  27. r.PUT("/:id/status", h.UpdateDeploymentStatus)
  28. r.GET("/by-client/:clientId", h.GetDeploymentsByClient)
  29. r.GET("/by-template/:templateId", h.GetDeploymentsByTemplate)
  30. r.GET("/by-user/:userId", h.GetDeploymentsByUser)
  31. }
  32. // ListDeployments returns all deployments with optional filtering
  33. func (h *DeploymentHandler) ListDeployments(c *gin.Context) {
  34. filter := make(map[string]interface{})
  35. // Attempt to bind query parameters, but allow empty filters
  36. if err := c.ShouldBindQuery(&filter); err != nil && len(filter) > 0 {
  37. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid query parameters"})
  38. return
  39. }
  40. deployments, err := h.service.ListDeployments(filter)
  41. if err != nil {
  42. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to list deployments: %v", err)})
  43. return
  44. }
  45. c.JSON(http.StatusOK, deployments)
  46. }
  47. // CreateDeployment creates a new deployment
  48. func (h *DeploymentHandler) CreateDeployment(c *gin.Context) {
  49. var deployment models.Deployment
  50. if err := c.ShouldBindJSON(&deployment); err != nil {
  51. c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request body: %v", err)})
  52. return
  53. }
  54. // Get the user ID from the context (set by auth middleware)
  55. userID, exists := c.Get("userID")
  56. if exists {
  57. deployment.CreatedBy = userID.(string)
  58. }
  59. if err := h.service.CreateDeployment(&deployment); err != nil {
  60. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to create deployment: %v", err)})
  61. return
  62. }
  63. c.JSON(http.StatusCreated, deployment)
  64. }
  65. // GetDeployment returns a specific deployment
  66. func (h *DeploymentHandler) GetDeployment(c *gin.Context) {
  67. idStr := c.Param("id")
  68. id, err := strconv.ParseInt(idStr, 10, 64)
  69. if err != nil {
  70. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid deployment ID"})
  71. return
  72. }
  73. deployment, err := h.service.GetDeployment(id)
  74. if err != nil {
  75. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch deployment: %v", err)})
  76. return
  77. }
  78. if deployment == nil {
  79. c.JSON(http.StatusNotFound, gin.H{"error": "Deployment not found"})
  80. return
  81. }
  82. c.JSON(http.StatusOK, deployment)
  83. }
  84. // UpdateDeployment updates a deployment
  85. func (h *DeploymentHandler) UpdateDeployment(c *gin.Context) {
  86. idStr := c.Param("id")
  87. id, err := strconv.ParseInt(idStr, 10, 64)
  88. if err != nil {
  89. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid deployment ID"})
  90. return
  91. }
  92. var updatedDeployment models.Deployment
  93. if err := c.ShouldBindJSON(&updatedDeployment); err != nil {
  94. c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request body: %v", err)})
  95. return
  96. }
  97. // Ensure the ID matches the URL parameter
  98. updatedDeployment.ID = id
  99. if err := h.service.UpdateDeployment(&updatedDeployment); err != nil {
  100. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to update deployment: %v", err)})
  101. return
  102. }
  103. c.JSON(http.StatusOK, updatedDeployment)
  104. }
  105. // DeleteDeployment deletes a deployment
  106. func (h *DeploymentHandler) DeleteDeployment(c *gin.Context) {
  107. idStr := c.Param("id")
  108. id, err := strconv.ParseInt(idStr, 10, 64)
  109. if err != nil {
  110. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid deployment ID"})
  111. return
  112. }
  113. if err := h.service.DeleteDeployment(id); err != nil {
  114. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to delete deployment: %v", err)})
  115. return
  116. }
  117. c.Status(http.StatusNoContent)
  118. }
  119. // UpdateDeploymentStatus updates the status of a deployment
  120. func (h *DeploymentHandler) UpdateDeploymentStatus(c *gin.Context) {
  121. idStr := c.Param("id")
  122. id, err := strconv.ParseInt(idStr, 10, 64)
  123. if err != nil {
  124. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid deployment ID"})
  125. return
  126. }
  127. var statusUpdate struct {
  128. Status string `json:"status" binding:"required"`
  129. }
  130. if err := c.ShouldBindJSON(&statusUpdate); err != nil {
  131. c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request body: %v", err)})
  132. return
  133. }
  134. if err := h.service.UpdateDeploymentStatus(id, statusUpdate.Status); err != nil {
  135. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to update deployment status: %v", err)})
  136. return
  137. }
  138. c.Status(http.StatusOK)
  139. }
  140. // GetDeploymentsByClient returns all deployments for a specific client
  141. func (h *DeploymentHandler) GetDeploymentsByClient(c *gin.Context) {
  142. clientIDStr := c.Param("clientId")
  143. clientID, err := strconv.ParseInt(clientIDStr, 10, 64)
  144. if err != nil {
  145. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid client ID"})
  146. return
  147. }
  148. deployments, err := h.service.GetDeploymentsByClientID(clientID)
  149. if err != nil {
  150. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch client deployments: %v", err)})
  151. return
  152. }
  153. c.JSON(http.StatusOK, deployments)
  154. }
  155. // GetDeploymentsByTemplate returns all deployments for a specific app (was template)
  156. func (h *DeploymentHandler) GetDeploymentsByTemplate(c *gin.Context) {
  157. appIDStr := c.Param("templateId") // Note: keeping templateId param for backward compatibility
  158. appID, err := strconv.ParseInt(appIDStr, 10, 64)
  159. if err != nil {
  160. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid app ID"})
  161. return
  162. }
  163. deployments, err := h.service.GetDeploymentsByAppID(appID)
  164. if err != nil {
  165. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch app deployments: %v", err)})
  166. return
  167. }
  168. c.JSON(http.StatusOK, deployments)
  169. }
  170. // GetDeploymentsByUser returns all deployments created by a specific user
  171. func (h *DeploymentHandler) GetDeploymentsByUser(c *gin.Context) {
  172. userID := c.Param("userId")
  173. deployments, err := h.service.GetDeploymentsByUserID(userID)
  174. if err != nil {
  175. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch user deployments: %v", err)})
  176. return
  177. }
  178. c.JSON(http.StatusOK, deployments)
  179. }