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