templates.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package services
  2. import (
  3. "fmt"
  4. "git.linuxforward.com/byop/byop-engine/dbstore"
  5. "git.linuxforward.com/byop/byop-engine/models"
  6. "github.com/google/uuid"
  7. )
  8. // TemplateService handles business logic for templates
  9. type TemplateService struct {
  10. store *dbstore.TemplateStore
  11. }
  12. // NewTemplateService creates a new TemplateService
  13. func NewTemplateService(store *dbstore.TemplateStore) *TemplateService {
  14. return &TemplateService{store: store}
  15. }
  16. // CreateTemplate creates a new deployment template
  17. func (s *TemplateService) CreateTemplate(template *models.Template) error {
  18. // Generate UUID if not provided
  19. if template.ID == "" {
  20. template.ID = uuid.New().String()
  21. }
  22. // Validate template configuration
  23. if err := validateTemplateConfig(template.Config); err != nil {
  24. return fmt.Errorf("invalid template configuration: %w", err)
  25. }
  26. // Persist the template
  27. return s.store.Create(template)
  28. }
  29. // GetTemplate retrieves a template by ID
  30. func (s *TemplateService) GetTemplate(id string) (*models.Template, error) {
  31. template, err := s.store.GetByID(id)
  32. if err != nil {
  33. return nil, fmt.Errorf("failed to retrieve template: %w", err)
  34. }
  35. return template, nil
  36. }
  37. // UpdateTemplate updates an existing template
  38. func (s *TemplateService) UpdateTemplate(template *models.Template) error {
  39. if template.ID == "" {
  40. return fmt.Errorf("template ID is required for update")
  41. }
  42. // Check if template exists
  43. existingTemplate, err := s.store.GetByID(template.ID)
  44. if err != nil {
  45. return fmt.Errorf("failed to check if template exists: %w", err)
  46. }
  47. if existingTemplate == nil {
  48. return fmt.Errorf("template with ID %s not found", template.ID)
  49. }
  50. // Validate template configuration
  51. if err := validateTemplateConfig(template.Config); err != nil {
  52. return fmt.Errorf("invalid template configuration: %w", err)
  53. }
  54. return s.store.Update(template)
  55. }
  56. // DeleteTemplate deletes a template by ID
  57. func (s *TemplateService) DeleteTemplate(id string) error {
  58. // Check if template exists
  59. template, err := s.store.GetByID(id)
  60. if err != nil {
  61. return fmt.Errorf("failed to check if template exists: %w", err)
  62. }
  63. if template == nil {
  64. return fmt.Errorf("template with ID %s not found", id)
  65. }
  66. // Check if the template has deployments
  67. templateWithDeployments, err := s.store.GetTemplateWithDeployments(id)
  68. if err != nil {
  69. return fmt.Errorf("failed to check template deployments: %w", err)
  70. }
  71. // Don't allow deletion if there are active deployments
  72. if len(templateWithDeployments.Deployments) > 0 {
  73. return fmt.Errorf("cannot delete template with active deployments")
  74. }
  75. return s.store.Delete(id)
  76. }
  77. // ListTemplates retrieves all templates with optional filtering
  78. func (s *TemplateService) ListTemplates(filter map[string]interface{}) ([]*models.Template, error) {
  79. return s.store.List(filter)
  80. }
  81. // GetTemplateDeployments retrieves all deployments for a template
  82. func (s *TemplateService) GetTemplateDeployments(id string) ([]models.Deployment, error) {
  83. // First check if the template exists
  84. template, err := s.store.GetByID(id)
  85. if err != nil {
  86. return nil, fmt.Errorf("failed to check if template exists: %w", err)
  87. }
  88. if template == nil {
  89. return nil, fmt.Errorf("template with ID %s not found", id)
  90. }
  91. // Get template with deployments
  92. templateWithDeployments, err := s.store.GetTemplateWithDeployments(id)
  93. if err != nil {
  94. return nil, fmt.Errorf("failed to retrieve template deployments: %w", err)
  95. }
  96. return templateWithDeployments.Deployments, nil
  97. }
  98. // GetTemplateByVersion retrieves a template by name and version
  99. func (s *TemplateService) GetTemplateByVersion(name, version string) (*models.Template, error) {
  100. template, err := s.store.GetByVersion(name, version)
  101. if err != nil {
  102. return nil, fmt.Errorf("failed to retrieve template: %w", err)
  103. }
  104. return template, nil
  105. }
  106. // validateTemplateConfig validates the template configuration
  107. func validateTemplateConfig(config models.TemplateConfig) error {
  108. // Validate that at least one app is defined
  109. if len(config.Apps) == 0 {
  110. return fmt.Errorf("template must define at least one app")
  111. }
  112. // Validate each app in the template
  113. for i, app := range config.Apps {
  114. if app.Name == "" {
  115. return fmt.Errorf("app at index %d must have a name", i)
  116. }
  117. // Validate resource configuration
  118. if app.Resources.CPU == "" {
  119. return fmt.Errorf("app '%s' must specify CPU resources", app.Name)
  120. }
  121. if app.Resources.Memory == "" {
  122. return fmt.Errorf("app '%s' must specify memory resources", app.Name)
  123. }
  124. }
  125. // Add additional validation logic as needed
  126. return nil
  127. }