blueprint.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package dbstore
  2. import (
  3. "fmt"
  4. "git.linuxforward.com/byop/byop-engine/dbmanager"
  5. "git.linuxforward.com/byop/byop-engine/models"
  6. "github.com/google/uuid"
  7. "gorm.io/gorm"
  8. )
  9. // BlueprintStore handles database operations for deployment blueprints
  10. type BlueprintStore struct {
  11. db *gorm.DB
  12. }
  13. // NewBlueprintStore creates a new BlueprintStore
  14. func NewBlueprintStore(dbManager dbmanager.DbManager) *BlueprintStore {
  15. return &BlueprintStore{
  16. db: dbManager.GetDB(),
  17. }
  18. }
  19. // Create creates a new blueprint
  20. func (bs *BlueprintStore) Create(blueprint *models.Blueprint) error {
  21. // Generate ID if not provided
  22. if blueprint.ID == "" {
  23. blueprint.ID = uuid.New().String()
  24. }
  25. // GORM will handle created_at and updated_at automatically
  26. return bs.db.Create(blueprint).Error
  27. }
  28. // GetByID retrieves a blueprint by ID
  29. func (bs *BlueprintStore) GetByID(id string) (*models.Blueprint, error) {
  30. var blueprint models.Blueprint
  31. result := bs.db.First(&blueprint, "id = ?", id)
  32. if result.Error != nil {
  33. if result.Error == gorm.ErrRecordNotFound {
  34. return nil, nil // No blueprint found
  35. }
  36. return nil, fmt.Errorf("failed to get blueprint: %w", result.Error)
  37. }
  38. return &blueprint, nil
  39. }
  40. // Update updates an existing blueprint
  41. func (bs *BlueprintStore) Update(blueprint *models.Blueprint) error {
  42. return bs.db.Save(blueprint).Error
  43. }
  44. // Delete deletes a blueprint by ID
  45. func (bs *BlueprintStore) Delete(id string) error {
  46. return bs.db.Delete(&models.Blueprint{}, "id = ?", id).Error
  47. }
  48. // List retrieves all blueprints with optional filtering
  49. func (bs *BlueprintStore) List(filter map[string]interface{}) ([]*models.Blueprint, error) {
  50. var blueprints []*models.Blueprint
  51. // Build query from filters
  52. query := bs.db
  53. if filter != nil {
  54. for key, value := range filter {
  55. query = query.Where(key+" = ?", value)
  56. }
  57. }
  58. // Execute query
  59. if err := query.Find(&blueprints).Error; err != nil {
  60. return nil, fmt.Errorf("failed to list blueprints: %w", err)
  61. }
  62. return blueprints, nil
  63. }
  64. // GetBlueprintWithDeployments retrieves a blueprint by ID with associated deployments
  65. func (bs *BlueprintStore) GetBlueprintWithDeployments(id string) (*models.Blueprint, error) {
  66. var blueprint models.Blueprint
  67. result := bs.db.Preload("Deployments").First(&blueprint, "id = ?", id)
  68. if result.Error != nil {
  69. if result.Error == gorm.ErrRecordNotFound {
  70. return nil, nil // No blueprint found
  71. }
  72. return nil, fmt.Errorf("failed to get blueprint: %w", result.Error)
  73. }
  74. return &blueprint, nil
  75. }
  76. // GetByVersion retrieves a template by name and version
  77. func (ts *BlueprintStore) GetByVersion(name string, version string) (*models.Blueprint, error) {
  78. var template models.Blueprint
  79. result := ts.db.Where("name = ? AND version = ?", name, version).First(&template)
  80. if result.Error != nil {
  81. if result.Error == gorm.ErrRecordNotFound {
  82. return nil, nil // No template found
  83. }
  84. return nil, fmt.Errorf("failed to get template: %w", result.Error)
  85. }
  86. return &template, nil
  87. }