blueprints.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // BlueprintHandler handles blueprint-related operations
  10. type BlueprintHandler struct {
  11. service *services.BlueprintService
  12. }
  13. // NewBlueprintHandler creates a new BlueprintHandler
  14. func NewBlueprintHandler(service *services.BlueprintService) *BlueprintHandler {
  15. return &BlueprintHandler{
  16. service: service,
  17. }
  18. }
  19. // ListBlueprints returns all blueprints with optional filtering
  20. func (h *BlueprintHandler) ListBlueprints(c *gin.Context) {
  21. filter := make(map[string]interface{})
  22. // Attempt to bind query parameters, but allow empty filters
  23. if err := c.ShouldBindQuery(&filter); err != nil && len(filter) > 0 {
  24. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid query parameters"})
  25. return
  26. }
  27. blueprints, err := h.service.ListBlueprints(filter)
  28. if err != nil {
  29. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to list blueprints: %v", err)})
  30. return
  31. }
  32. c.JSON(http.StatusOK, blueprints)
  33. }
  34. // CreateBlueprint creates a new deployment blueprint
  35. func (h *BlueprintHandler) CreateBlueprint(c *gin.Context) {
  36. var blueprint models.Blueprint
  37. if err := c.ShouldBindJSON(&blueprint); err != nil {
  38. c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request body: %v", err)})
  39. return
  40. }
  41. // Get the user ID from the context (set by auth middleware)
  42. userID, exists := c.Get("userID")
  43. if exists {
  44. blueprint.CreatedBy = userID.(string)
  45. }
  46. if err := h.service.CreateBlueprint(&blueprint); err != nil {
  47. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to create blueprint: %v", err)})
  48. return
  49. }
  50. c.JSON(http.StatusCreated, blueprint)
  51. }
  52. // GetBlueprint returns a specific blueprint
  53. func (h *BlueprintHandler) GetBlueprint(c *gin.Context) {
  54. id := c.Param("id")
  55. blueprint, err := h.service.GetBlueprint(id)
  56. if err != nil {
  57. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch blueprint: %v", err)})
  58. return
  59. }
  60. if blueprint == nil {
  61. c.JSON(http.StatusNotFound, gin.H{"error": "Blueprint not found"})
  62. return
  63. }
  64. c.JSON(http.StatusOK, blueprint)
  65. }
  66. // UpdateBlueprint updates a blueprint
  67. func (h *BlueprintHandler) UpdateBlueprint(c *gin.Context) {
  68. id := c.Param("id")
  69. var updatedBlueprint models.Blueprint
  70. if err := c.ShouldBindJSON(&updatedBlueprint); err != nil {
  71. c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request body: %v", err)})
  72. return
  73. }
  74. // Ensure the ID matches the URL parameter
  75. updatedBlueprint.ID = id
  76. if err := h.service.UpdateBlueprint(&updatedBlueprint); err != nil {
  77. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to update blueprint: %v", err)})
  78. return
  79. }
  80. c.JSON(http.StatusOK, updatedBlueprint)
  81. }
  82. // DeleteBlueprint deletes a blueprint
  83. func (h *BlueprintHandler) DeleteBlueprint(c *gin.Context) {
  84. id := c.Param("id")
  85. if err := h.service.DeleteBlueprint(id); err != nil {
  86. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to delete blueprint: %v", err)})
  87. return
  88. }
  89. c.Status(http.StatusNoContent)
  90. }
  91. // GetBlueprintDeployments returns all deployments for a template
  92. func (h *BlueprintHandler) GetBlueprintDeployments(c *gin.Context) {
  93. id := c.Param("id")
  94. deployments, err := h.service.GetBlueprintDeployments(id)
  95. if err != nil {
  96. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch template deployments: %v", err)})
  97. return
  98. }
  99. c.JSON(http.StatusOK, deployments)
  100. }
  101. // GetBlueprintByVersion handles retrieval of a template by name and version
  102. func (h *BlueprintHandler) GetBlueprintByVersion(c *gin.Context) {
  103. name := c.Query("name")
  104. version := c.Query("version")
  105. if name == "" || version == "" {
  106. c.JSON(http.StatusBadRequest, gin.H{"error": "Both name and version parameters are required"})
  107. return
  108. }
  109. template, err := h.service.GetBlueprintByVersion(name, version)
  110. if err != nil {
  111. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch template: %v", err)})
  112. return
  113. }
  114. if template == nil {
  115. c.JSON(http.StatusNotFound, gin.H{"error": "Blueprint not found"})
  116. return
  117. }
  118. c.JSON(http.StatusOK, template)
  119. }