Moves the `domain` and `ports` packages from `internal/core` to `internal`. This refactoring simplifies the directory structure by elevating the core architectural concepts of domain and ports to the top level of the `internal` directory. The `core` directory is now removed as its only purpose was to house these two packages. All import paths across the project have been updated to reflect this change.
118 lines
3.7 KiB
Go
118 lines
3.7 KiB
Go
package cloudlet
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"edp.buildth.ing/DevFW-CICD/edge-connect-client/internal/domain"
|
|
"edp.buildth.ing/DevFW-CICD/edge-connect-client/internal/ports/driven"
|
|
"edp.buildth.ing/DevFW-CICD/edge-connect-client/internal/ports/driving"
|
|
)
|
|
|
|
type service struct {
|
|
cloudletRepo driven.CloudletRepository
|
|
}
|
|
|
|
func NewService(cloudletRepo driven.CloudletRepository) driving.CloudletService {
|
|
return &service{cloudletRepo: cloudletRepo}
|
|
}
|
|
|
|
func (s *service) CreateCloudlet(ctx context.Context, region string, cloudlet *domain.Cloudlet) error {
|
|
if err := s.validateCloudlet(cloudlet); err != nil {
|
|
return err
|
|
}
|
|
|
|
if region == "" {
|
|
return domain.ErrMissingRegion
|
|
}
|
|
|
|
if err := s.cloudletRepo.CreateCloudlet(ctx, region, cloudlet); err != nil {
|
|
if domain.IsNotFoundError(err) {
|
|
return domain.NewCloudletError(domain.ErrResourceConflict, "CreateCloudlet", cloudlet.Key, region,
|
|
"cloudlet may already exist or have conflicting configuration")
|
|
}
|
|
return domain.NewCloudletError(domain.ErrInternalError, "CreateCloudlet", cloudlet.Key, region,
|
|
"failed to create cloudlet").WithDetails(err.Error())
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *service) ShowCloudlet(ctx context.Context, region string, cloudletKey domain.CloudletKey) (*domain.Cloudlet, error) {
|
|
if err := s.validateCloudletKey(cloudletKey); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if region == "" {
|
|
return nil, domain.ErrMissingRegion
|
|
}
|
|
|
|
cloudlet, err := s.cloudletRepo.ShowCloudlet(ctx, region, cloudletKey)
|
|
if err != nil {
|
|
if domain.IsNotFoundError(err) {
|
|
return nil, domain.NewCloudletError(domain.ErrResourceNotFound, "ShowCloudlet", cloudletKey, region,
|
|
"cloudlet does not exist")
|
|
}
|
|
return nil, domain.NewCloudletError(domain.ErrInternalError, "ShowCloudlet", cloudletKey, region,
|
|
"failed to retrieve cloudlet").WithDetails(err.Error())
|
|
}
|
|
|
|
return cloudlet, nil
|
|
}
|
|
|
|
func (s *service) ShowCloudlets(ctx context.Context, region string, cloudletKey domain.CloudletKey) ([]domain.Cloudlet, error) {
|
|
if region == "" {
|
|
return nil, domain.ErrMissingRegion
|
|
}
|
|
|
|
cloudlets, err := s.cloudletRepo.ShowCloudlets(ctx, region, cloudletKey)
|
|
if err != nil {
|
|
return nil, domain.NewCloudletError(domain.ErrInternalError, "ShowCloudlets", cloudletKey, region,
|
|
"failed to list cloudlets").WithDetails(err.Error())
|
|
}
|
|
|
|
return cloudlets, nil
|
|
}
|
|
|
|
func (s *service) DeleteCloudlet(ctx context.Context, region string, cloudletKey domain.CloudletKey) error {
|
|
if err := s.validateCloudletKey(cloudletKey); err != nil {
|
|
return err
|
|
}
|
|
|
|
if region == "" {
|
|
return domain.ErrMissingRegion
|
|
}
|
|
|
|
if err := s.cloudletRepo.DeleteCloudlet(ctx, region, cloudletKey); err != nil {
|
|
if domain.IsNotFoundError(err) {
|
|
return domain.NewCloudletError(domain.ErrResourceNotFound, "DeleteCloudlet", cloudletKey, region,
|
|
"cloudlet does not exist")
|
|
}
|
|
return domain.NewCloudletError(domain.ErrInternalError, "DeleteCloudlet", cloudletKey, region,
|
|
"failed to delete cloudlet").WithDetails(err.Error())
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// validateCloudlet performs business logic validation on a cloudlet
|
|
func (s *service) validateCloudlet(cloudlet *domain.Cloudlet) error {
|
|
if cloudlet == nil {
|
|
return domain.NewDomainError(domain.ErrValidationFailed, "cloudlet cannot be nil")
|
|
}
|
|
|
|
return s.validateCloudletKey(cloudlet.Key)
|
|
}
|
|
|
|
// validateCloudletKey performs business logic validation on a cloudlet key
|
|
func (s *service) validateCloudletKey(cloudletKey domain.CloudletKey) error {
|
|
if strings.TrimSpace(cloudletKey.Organization) == "" {
|
|
return domain.ErrInvalidCloudletKey.WithDetails("organization is required")
|
|
}
|
|
|
|
if strings.TrimSpace(cloudletKey.Name) == "" {
|
|
return domain.ErrInvalidCloudletKey.WithDetails("name is required")
|
|
}
|
|
|
|
return nil
|
|
}
|