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

     1  package operators
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"runtime/debug"
     7  	"unsafe"
     8  
     9  	"github.com/kyma-incubator/compass/components/director/internal/domain/formationassignment"
    10  	"github.com/kyma-incubator/compass/components/director/internal/model"
    11  	"github.com/kyma-incubator/compass/components/director/pkg/log"
    12  	"github.com/kyma-incubator/compass/components/director/pkg/persistence"
    13  	"github.com/pkg/errors"
    14  	"github.com/sirupsen/logrus"
    15  )
    16  
    17  // RetrieveFormationAssignmentPointer converts the provided uninterpreted memory address in form of an integer back to the model.FormationAssignment pointer structure
    18  func RetrieveFormationAssignmentPointer(ctx context.Context, jointPointDetailsAssignmentMemoryAddress uintptr) (*model.FormationAssignment, error) {
    19  	if jointPointDetailsAssignmentMemoryAddress == 0 { // the default value of uintptr is 0
    20  		return nil, errors.New("The joint point details' assignment memory address cannot be 0")
    21  	}
    22  
    23  	defer func() {
    24  		if err := recover(); err != nil {
    25  			log.C(ctx).WithField(logrus.ErrorKey, err).Panicf("A panic occurred while converting joint point details' assignment address: %d to type: %T", jointPointDetailsAssignmentMemoryAddress, &model.FormationAssignment{})
    26  			debug.PrintStack()
    27  		}
    28  	}()
    29  	jointPointAssignmentPointer := (*model.FormationAssignment)(unsafe.Pointer(jointPointDetailsAssignmentMemoryAddress))
    30  
    31  	return jointPointAssignmentPointer, nil
    32  }
    33  
    34  func (e *ConstraintEngine) transaction(ctx context.Context, dbCall func(ctxWithTransact context.Context) error) error {
    35  	tx, err := e.transact.Begin()
    36  	if err != nil {
    37  		log.C(ctx).WithError(err).Error("Failed to begin DB transaction")
    38  		return err
    39  	}
    40  	defer e.transact.RollbackUnlessCommitted(ctx, tx)
    41  
    42  	ctx = persistence.SaveToContext(ctx, tx)
    43  
    44  	if err = dbCall(ctx); err != nil {
    45  		return err
    46  	}
    47  
    48  	if err = tx.Commit(); err != nil {
    49  		log.C(ctx).WithError(err).Error("Failed to commit database transaction")
    50  		return err
    51  	}
    52  	return nil
    53  }
    54  
    55  func (e *ConstraintEngine) setAssignmentToErrorState(ctx context.Context, assignment *model.FormationAssignment, errorMessage string) error {
    56  	assignment.State = string(model.CreateErrorAssignmentState)
    57  	assignmentError := formationassignment.AssignmentErrorWrapper{Error: formationassignment.AssignmentError{
    58  		Message:   errorMessage,
    59  		ErrorCode: formationassignment.ClientError,
    60  	}}
    61  	marshaled, err := json.Marshal(assignmentError)
    62  	if err != nil {
    63  		return errors.Wrapf(err, "While preparing error message for assignment with ID %q", assignment.ID)
    64  	}
    65  	assignment.Value = marshaled
    66  	if err := e.formationAssignmentRepo.Update(ctx, assignment); err != nil {
    67  		return errors.Wrapf(err, "While updating formation assignment with id %q", assignment.ID)
    68  	}
    69  	log.C(ctx).Infof("Assignment with ID %s set to state %s", assignment.ID, assignment.State)
    70  	return nil
    71  }