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

     1  package runtimectx
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
     8  
     9  	"github.com/kyma-incubator/compass/components/director/pkg/persistence"
    10  
    11  	"github.com/kyma-incubator/compass/components/director/internal/model"
    12  
    13  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    14  )
    15  
    16  // RuntimeContextService missing godoc
    17  //go:generate mockery --name=RuntimeContextService --output=automock --outpkg=automock --case=underscore --disable-version-string
    18  type RuntimeContextService interface {
    19  	Create(ctx context.Context, in model.RuntimeContextInput) (string, error)
    20  	Update(ctx context.Context, id string, in model.RuntimeContextInput) error
    21  	GetByID(ctx context.Context, id string) (*model.RuntimeContext, error)
    22  	Delete(ctx context.Context, id string) error
    23  	ListLabels(ctx context.Context, runtimeID string) (map[string]*model.Label, error)
    24  }
    25  
    26  // RuntimeContextConverter missing godoc
    27  //go:generate mockery --name=RuntimeContextConverter --output=automock --outpkg=automock --case=underscore --disable-version-string
    28  type RuntimeContextConverter interface {
    29  	ToGraphQL(in *model.RuntimeContext) *graphql.RuntimeContext
    30  	InputFromGraphQL(in graphql.RuntimeContextInput) model.RuntimeContextInput
    31  	InputFromGraphQLWithRuntimeID(in graphql.RuntimeContextInput, runtimeID string) model.RuntimeContextInput
    32  }
    33  
    34  // Resolver missing godoc
    35  type Resolver struct {
    36  	transact              persistence.Transactioner
    37  	runtimeContextService RuntimeContextService
    38  	converter             RuntimeContextConverter
    39  }
    40  
    41  // NewResolver missing godoc
    42  func NewResolver(transact persistence.Transactioner, runtimeContextService RuntimeContextService, conv RuntimeContextConverter) *Resolver {
    43  	return &Resolver{
    44  		transact:              transact,
    45  		runtimeContextService: runtimeContextService,
    46  		converter:             conv,
    47  	}
    48  }
    49  
    50  // RegisterRuntimeContext registers RuntimeContext from `in` and associates it with Runtime with ID `runtimeID`
    51  func (r *Resolver) RegisterRuntimeContext(ctx context.Context, runtimeID string, in graphql.RuntimeContextInput) (*graphql.RuntimeContext, error) {
    52  	convertedIn := r.converter.InputFromGraphQLWithRuntimeID(in, runtimeID)
    53  
    54  	tx, err := r.transact.Begin()
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	defer r.transact.RollbackUnlessCommitted(ctx, tx)
    59  
    60  	ctx = persistence.SaveToContext(ctx, tx)
    61  
    62  	id, err := r.runtimeContextService.Create(ctx, convertedIn)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	runtimeContext, err := r.runtimeContextService.GetByID(ctx, id)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	if err = tx.Commit(); err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	gqlRuntimeContext := r.converter.ToGraphQL(runtimeContext)
    77  
    78  	return gqlRuntimeContext, nil
    79  }
    80  
    81  // UpdateRuntimeContext updates RuntimeContext with ID `id` using `in`
    82  func (r *Resolver) UpdateRuntimeContext(ctx context.Context, id string, in graphql.RuntimeContextInput) (*graphql.RuntimeContext, error) {
    83  	convertedIn := r.converter.InputFromGraphQL(in)
    84  
    85  	tx, err := r.transact.Begin()
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  	defer r.transact.RollbackUnlessCommitted(ctx, tx)
    90  
    91  	ctx = persistence.SaveToContext(ctx, tx)
    92  
    93  	if err = r.runtimeContextService.Update(ctx, id, convertedIn); err != nil {
    94  		return nil, err
    95  	}
    96  
    97  	runtimeContext, err := r.runtimeContextService.GetByID(ctx, id)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	if err = tx.Commit(); err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	gqlRuntimeContext := r.converter.ToGraphQL(runtimeContext)
   107  
   108  	return gqlRuntimeContext, nil
   109  }
   110  
   111  // DeleteRuntimeContext deletes RuntimeContext with ID `id`
   112  func (r *Resolver) DeleteRuntimeContext(ctx context.Context, id string) (*graphql.RuntimeContext, error) {
   113  	tx, err := r.transact.Begin()
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  	defer r.transact.RollbackUnlessCommitted(ctx, tx)
   118  
   119  	ctx = persistence.SaveToContext(ctx, tx)
   120  
   121  	runtimeContext, err := r.runtimeContextService.GetByID(ctx, id)
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  
   126  	if err = r.runtimeContextService.Delete(ctx, id); err != nil {
   127  		return nil, err
   128  	}
   129  
   130  	if err = tx.Commit(); err != nil {
   131  		return nil, err
   132  	}
   133  
   134  	deletedRuntimeContext := r.converter.ToGraphQL(runtimeContext)
   135  
   136  	return deletedRuntimeContext, nil
   137  }
   138  
   139  // Labels lists Labels with key `key` for RuntimeContext `obj`
   140  func (r *Resolver) Labels(ctx context.Context, obj *graphql.RuntimeContext, key *string) (graphql.Labels, error) {
   141  	if obj == nil {
   142  		return nil, apperrors.NewInternalError("Runtime Context cannot be empty")
   143  	}
   144  
   145  	tx, err := r.transact.Begin()
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  	defer r.transact.RollbackUnlessCommitted(ctx, tx)
   150  
   151  	ctx = persistence.SaveToContext(ctx, tx)
   152  
   153  	itemMap, err := r.runtimeContextService.ListLabels(ctx, obj.ID)
   154  	if err != nil {
   155  		if strings.Contains(err.Error(), "doesn't exist") {
   156  			return nil, tx.Commit()
   157  		}
   158  		return nil, err
   159  	}
   160  
   161  	if err = tx.Commit(); err != nil {
   162  		return nil, err
   163  	}
   164  
   165  	resultLabels := make(map[string]interface{})
   166  
   167  	for _, label := range itemMap {
   168  		if key == nil || label.Key == *key {
   169  			resultLabels[label.Key] = label.Value
   170  		}
   171  	}
   172  
   173  	return resultLabels, nil
   174  }