components.go 3.2 KB

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