components.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // ComponentService handles business logic for components
  9. type ComponentService struct {
  10. store *dbstore.ComponentStore
  11. }
  12. // NewComponentService creates a new ComponentService
  13. func NewComponentService(store *dbstore.ComponentStore) *ComponentService {
  14. return &ComponentService{store: store}
  15. }
  16. // CreateComponent creates a new component
  17. func (s *ComponentService) CreateComponent(component *models.Component) error {
  18. // Generate UUID if not provided
  19. if component.ID == "" {
  20. component.ID = uuid.New().String()
  21. }
  22. // Set default resource values if not provided
  23. if component.Resources.CPU == "" {
  24. component.Resources.CPU = "0.5"
  25. }
  26. if component.Resources.Memory == "" {
  27. component.Resources.Memory = "512Mi"
  28. }
  29. if component.Resources.Storage == "" {
  30. component.Resources.Storage = "1Gi"
  31. }
  32. // Set default scale settings if not provided
  33. if component.ScaleSettings.MinInstances == 0 {
  34. component.ScaleSettings.MinInstances = 1
  35. }
  36. if component.ScaleSettings.MaxInstances == 0 {
  37. component.ScaleSettings.MaxInstances = 3
  38. }
  39. if component.ScaleSettings.CPUThreshold == 0 {
  40. component.ScaleSettings.CPUThreshold = 80
  41. }
  42. // Persist the component
  43. return s.store.Create(component)
  44. }
  45. // GetComponent retrieves a component by ID
  46. func (s *ComponentService) GetComponent(id string) (*models.Component, error) {
  47. component, err := s.store.GetByID(id)
  48. if err != nil {
  49. return nil, fmt.Errorf("failed to retrieve component: %w", err)
  50. }
  51. return component, nil
  52. }
  53. // UpdateComponent updates an existing component
  54. func (s *ComponentService) UpdateComponent(component *models.Component) error {
  55. if component.ID == "" {
  56. return fmt.Errorf("component ID is required for update")
  57. }
  58. // Check if component exists
  59. existingComponent, err := s.store.GetByID(component.ID)
  60. if err != nil {
  61. return fmt.Errorf("failed to check if component exists: %w", err)
  62. }
  63. if existingComponent == nil {
  64. return fmt.Errorf("component with ID %s not found", component.ID)
  65. }
  66. return s.store.Update(component)
  67. }
  68. // DeleteComponent deletes a component by ID
  69. func (s *ComponentService) DeleteComponent(id string) error {
  70. // Check if component exists
  71. component, err := s.store.GetByID(id)
  72. if err != nil {
  73. return fmt.Errorf("failed to check if component exists: %w", err)
  74. }
  75. if component == nil {
  76. return fmt.Errorf("component with ID %s not found", id)
  77. }
  78. return s.store.Delete(id)
  79. }
  80. // ListComponents retrieves all components with optional filtering
  81. func (s *ComponentService) ListComponents(filter map[string]interface{}) ([]*models.Component, error) {
  82. return s.store.List(filter)
  83. }
  84. // GetComponentDeployments retrieves all deployments for a component
  85. func (s *ComponentService) GetComponentDeployments(id string) ([]models.DeployedComponent, error) {
  86. // First check if the component exists
  87. component, err := s.store.GetByID(id)
  88. if err != nil {
  89. return nil, fmt.Errorf("failed to check if component exists: %w", err)
  90. }
  91. if component == nil {
  92. return nil, fmt.Errorf("component with ID %s not found", id)
  93. }
  94. // Get component with deployments
  95. componentWithDeployments, err := s.store.GetComponentWithDeployments(id)
  96. if err != nil {
  97. return nil, fmt.Errorf("failed to retrieve component deployments: %w", err)
  98. }
  99. return componentWithDeployments.Deployments, nil
  100. }