github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/certsubjectmapping/service.go (about)

     1  package certsubjectmapping
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/internal/model"
     7  	"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
     8  	"github.com/kyma-incubator/compass/components/director/pkg/log"
     9  	"github.com/kyma-incubator/compass/components/director/pkg/resource"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  // CertMappingRepository represents the certificate subject mapping repository layer
    14  //go:generate mockery --name=CertMappingRepository --output=automock --outpkg=automock --case=underscore --disable-version-string
    15  type CertMappingRepository interface {
    16  	Create(ctx context.Context, item *model.CertSubjectMapping) error
    17  	Get(ctx context.Context, id string) (*model.CertSubjectMapping, error)
    18  	Update(ctx context.Context, model *model.CertSubjectMapping) error
    19  	Delete(ctx context.Context, id string) error
    20  	Exists(ctx context.Context, id string) (bool, error)
    21  	List(ctx context.Context, pageSize int, cursor string) (*model.CertSubjectMappingPage, error)
    22  }
    23  
    24  type service struct {
    25  	repo CertMappingRepository
    26  }
    27  
    28  // NewService returns a new object responsible for service-layer certificate subject mapping operations.
    29  func NewService(repo CertMappingRepository) *service {
    30  	return &service{
    31  		repo: repo,
    32  	}
    33  }
    34  
    35  // Create creates a certificate subject mapping using `item`
    36  func (s *service) Create(ctx context.Context, item *model.CertSubjectMapping) (string, error) {
    37  	log.C(ctx).Infof("Creating certificate subject mapping with ID: %s, subject: %s and consumer type: %s", item.ID, item.Subject, item.ConsumerType)
    38  	if err := s.repo.Create(ctx, item); err != nil {
    39  		return "", errors.Wrapf(err, "while creating certificate subject mapping with subject: %s and consumer type: %s", item.Subject, item.ConsumerType)
    40  	}
    41  
    42  	return item.ID, nil
    43  }
    44  
    45  // Get queries certificate subject mapping matching ID `id`
    46  func (s *service) Get(ctx context.Context, id string) (*model.CertSubjectMapping, error) {
    47  	log.C(ctx).Infof("Getting certificate subject mapping with ID: %s", id)
    48  	csm, err := s.repo.Get(ctx, id)
    49  	if err != nil {
    50  		return nil, errors.Wrapf(err, "while getting certificate subject mapping with ID: %s", id)
    51  	}
    52  
    53  	return csm, nil
    54  }
    55  
    56  // Update updates a certificate subject mapping using `in`
    57  func (s *service) Update(ctx context.Context, in *model.CertSubjectMapping) error {
    58  	log.C(ctx).Infof("Updating certificate subject mapping with ID: %s, subject: %s and consumer type: %s", in.ID, in.Subject, in.ConsumerType)
    59  
    60  	if exists, err := s.repo.Exists(ctx, in.ID); err != nil {
    61  		return errors.Wrapf(err, "while ensuring certificate subject mapping with ID: %s exists", in.ID)
    62  	} else if !exists {
    63  		return apperrors.NewNotFoundError(resource.CertSubjectMapping, in.ID)
    64  	}
    65  
    66  	if err := s.repo.Update(ctx, in); err != nil {
    67  		return errors.Wrapf(err, "while updating certificate subject mapping with ID: %s", in.ID)
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  // Delete deletes a certificate subject mapping matching ID `id`
    74  func (s *service) Delete(ctx context.Context, id string) error {
    75  	log.C(ctx).Infof("Deleting certificate subject mapping with ID: %s", id)
    76  	if err := s.repo.Delete(ctx, id); err != nil {
    77  		return errors.Wrapf(err, "while deleting certificate subject mapping with ID: %s", id)
    78  	}
    79  	return nil
    80  }
    81  
    82  // Exists check if a certificate subject mapping with ID `id` exists
    83  func (s *service) Exists(ctx context.Context, id string) (bool, error) {
    84  	log.C(ctx).Infof("Checking certificate subject mapping existence for ID: %s", id)
    85  	exists, err := s.repo.Exists(ctx, id)
    86  	if err != nil {
    87  		return false, errors.Wrapf(err, "while checking certificate subject mapping existence for ID: %s", id)
    88  	}
    89  	return exists, nil
    90  }
    91  
    92  // List retrieves certificate subject mappings with pagination based on `pageSize` and `cursor`
    93  func (s *service) List(ctx context.Context, pageSize int, cursor string) (*model.CertSubjectMappingPage, error) {
    94  	log.C(ctx).Info("Listing certificate subject mappings")
    95  	if pageSize < 1 || pageSize > 300 {
    96  		return nil, apperrors.NewInvalidDataError("page size must be between 1 and 300")
    97  	}
    98  
    99  	csmPage, err := s.repo.List(ctx, pageSize, cursor)
   100  	if err != nil {
   101  		return nil, errors.Wrap(err, "while listing certificate subject mapping")
   102  	}
   103  
   104  	return csmPage, nil
   105  }