deployments.go 5.6 KB

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