github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/tombstone/service.go (about) 1 package tombstone 2 3 import ( 4 "context" 5 6 "github.com/kyma-incubator/compass/components/director/pkg/resource" 7 8 "github.com/kyma-incubator/compass/components/director/internal/domain/tenant" 9 "github.com/kyma-incubator/compass/components/director/internal/model" 10 "github.com/kyma-incubator/compass/components/director/pkg/log" 11 "github.com/pkg/errors" 12 ) 13 14 // TombstoneRepository missing godoc 15 // 16 //go:generate mockery --name=TombstoneRepository --output=automock --outpkg=automock --case=underscore --disable-version-string 17 type TombstoneRepository interface { 18 Create(ctx context.Context, tenant string, item *model.Tombstone) error 19 CreateGlobal(ctx context.Context, model *model.Tombstone) error 20 Update(ctx context.Context, tenant string, item *model.Tombstone) error 21 UpdateGlobal(ctx context.Context, model *model.Tombstone) error 22 Delete(ctx context.Context, tenant, id string) error 23 Exists(ctx context.Context, tenant, id string) (bool, error) 24 GetByID(ctx context.Context, tenant, id string) (*model.Tombstone, error) 25 GetByIDGlobal(ctx context.Context, id string) (*model.Tombstone, error) 26 ListByResourceID(ctx context.Context, tenantID, resourceID string, resourceType resource.Type) ([]*model.Tombstone, error) 27 } 28 29 // UIDService missing godoc 30 // 31 //go:generate mockery --name=UIDService --output=automock --outpkg=automock --case=underscore --disable-version-string 32 type UIDService interface { 33 Generate() string 34 } 35 36 type service struct { 37 tombstoneRepo TombstoneRepository 38 uidService UIDService 39 } 40 41 // NewService missing godoc 42 func NewService(tombstoneRepo TombstoneRepository, uidService UIDService) *service { 43 return &service{ 44 tombstoneRepo: tombstoneRepo, 45 uidService: uidService, 46 } 47 } 48 49 // Create missing godoc 50 func (s *service) Create(ctx context.Context, resourceType resource.Type, resourceID string, in model.TombstoneInput) (string, error) { 51 id := s.uidService.Generate() 52 tombstone := in.ToTombstone(id, resourceType, resourceID) 53 54 if err := s.createTombstone(ctx, tombstone, resourceType); err != nil { 55 return "", errors.Wrapf(err, "error occurred while creating a Tombstone with id %s for %s with id %s", id, resourceType, resourceID) 56 } 57 58 log.C(ctx).Debugf("Successfully created a Tombstone with id %s for %s with id %s", id, resourceType, resourceID) 59 60 return tombstone.OrdID, nil 61 } 62 63 // Update missing godoc 64 func (s *service) Update(ctx context.Context, resourceType resource.Type, id string, in model.TombstoneInput) error { 65 tombstone, err := s.getTombstone(ctx, id, resourceType) 66 if err != nil { 67 return errors.Wrapf(err, "while getting Tombstone with id %s", id) 68 } 69 70 tombstone.SetFromUpdateInput(in) 71 72 if err = s.updateTombstone(ctx, tombstone, resourceType); err != nil { 73 return errors.Wrapf(err, "while updating Tombstone with id %s", id) 74 } 75 76 return nil 77 } 78 79 // Delete missing godoc 80 func (s *service) Delete(ctx context.Context, id string) error { 81 tnt, err := tenant.LoadFromContext(ctx) 82 if err != nil { 83 return errors.Wrap(err, "while loading tenant from context") 84 } 85 86 err = s.tombstoneRepo.Delete(ctx, tnt, id) 87 if err != nil { 88 return errors.Wrapf(err, "while deleting Tombstone with id %s", id) 89 } 90 91 return nil 92 } 93 94 // Exist missing godoc 95 func (s *service) Exist(ctx context.Context, id string) (bool, error) { 96 tnt, err := tenant.LoadFromContext(ctx) 97 if err != nil { 98 return false, errors.Wrap(err, "while loading tenant from context") 99 } 100 101 exist, err := s.tombstoneRepo.Exists(ctx, tnt, id) 102 if err != nil { 103 return false, errors.Wrapf(err, "while getting Tombstone with ID: %q", id) 104 } 105 106 return exist, nil 107 } 108 109 // Get missing godoc 110 func (s *service) Get(ctx context.Context, id string) (*model.Tombstone, error) { 111 tnt, err := tenant.LoadFromContext(ctx) 112 if err != nil { 113 return nil, errors.Wrap(err, "while loading tenant from context") 114 } 115 116 tombstone, err := s.tombstoneRepo.GetByID(ctx, tnt, id) 117 if err != nil { 118 return nil, errors.Wrapf(err, "while getting Tombstone with ID: %q", id) 119 } 120 121 return tombstone, nil 122 } 123 124 // ListByApplicationID missing godoc 125 func (s *service) ListByApplicationID(ctx context.Context, appID string) ([]*model.Tombstone, error) { 126 tnt, err := tenant.LoadFromContext(ctx) 127 if err != nil { 128 return nil, err 129 } 130 131 return s.tombstoneRepo.ListByResourceID(ctx, tnt, appID, resource.Application) 132 } 133 134 // ListByApplicationTemplateVersionID lists Tombstones by Application Template Version ID 135 func (s *service) ListByApplicationTemplateVersionID(ctx context.Context, id string) ([]*model.Tombstone, error) { 136 return s.tombstoneRepo.ListByResourceID(ctx, "", id, resource.ApplicationTemplateVersion) 137 } 138 139 func (s *service) createTombstone(ctx context.Context, tombstone *model.Tombstone, resourceType resource.Type) error { 140 if resourceType.IsTenantIgnorable() { 141 return s.tombstoneRepo.CreateGlobal(ctx, tombstone) 142 } 143 144 tnt, err := tenant.LoadFromContext(ctx) 145 if err != nil { 146 return err 147 } 148 149 return s.tombstoneRepo.Create(ctx, tnt, tombstone) 150 } 151 152 func (s *service) getTombstone(ctx context.Context, id string, resourceType resource.Type) (*model.Tombstone, error) { 153 if resourceType.IsTenantIgnorable() { 154 return s.tombstoneRepo.GetByIDGlobal(ctx, id) 155 } 156 157 return s.Get(ctx, id) 158 } 159 160 func (s *service) updateTombstone(ctx context.Context, tombstone *model.Tombstone, resourceType resource.Type) error { 161 if resourceType.IsTenantIgnorable() { 162 return s.tombstoneRepo.UpdateGlobal(ctx, tombstone) 163 } 164 165 tnt, err := tenant.LoadFromContext(ctx) 166 if err != nil { 167 return err 168 } 169 170 return s.tombstoneRepo.Update(ctx, tnt, tombstone) 171 }