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

     1  package subscription
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/pkg/log"
     7  	"github.com/kyma-incubator/compass/components/director/pkg/persistence"
     8  	"github.com/kyma-incubator/compass/components/director/pkg/resource"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  // SubscriptionService responsible for service-layer Subscription operations
    13  //
    14  //go:generate mockery --name=SubscriptionService --output=automock --outpkg=automock --case=underscore --disable-version-string
    15  type SubscriptionService interface {
    16  	SubscribeTenantToRuntime(ctx context.Context, providerID, subaccountTenantID, providerSubaccountID, consumerTenantID, region, subscriptionAppName string) (bool, error)
    17  	UnsubscribeTenantFromRuntime(ctx context.Context, providerID, subaccountTenantID, providerSubaccountID, consumerTenantID, region string) (bool, error)
    18  	SubscribeTenantToApplication(ctx context.Context, providerID, subaccountTenantID, consumerTenantID, region, subscribedAppName string, subscriptionPayload string) (bool, error)
    19  	UnsubscribeTenantFromApplication(ctx context.Context, providerID, subaccountTenantID, region string) (bool, error)
    20  	DetermineSubscriptionFlow(ctx context.Context, providerID, region string) (resource.Type, error)
    21  }
    22  
    23  // Resolver is an object responsible for resolver-layer Subscription operations.
    24  type Resolver struct {
    25  	transact        persistence.Transactioner
    26  	subscriptionSvc SubscriptionService
    27  }
    28  
    29  // NewResolver returns a new object responsible for resolver-layer Subscription operations.
    30  func NewResolver(transact persistence.Transactioner, subscriptionSvc SubscriptionService) *Resolver {
    31  	return &Resolver{
    32  		transact:        transact,
    33  		subscriptionSvc: subscriptionSvc,
    34  	}
    35  }
    36  
    37  // SubscribeTenant subscribes tenant to runtime labeled with `providerID` and `region`
    38  func (r *Resolver) SubscribeTenant(ctx context.Context, providerID, subaccountTenantID, providerSubaccountID, consumerTenantID, region, subscriptionAppName string, subscriptionPayload string) (bool, error) {
    39  	tx, err := r.transact.Begin()
    40  	if err != nil {
    41  		return false, err
    42  	}
    43  	defer r.transact.RollbackUnlessCommitted(ctx, tx)
    44  
    45  	ctx = persistence.SaveToContext(ctx, tx)
    46  
    47  	flowType, err := r.subscriptionSvc.DetermineSubscriptionFlow(ctx, providerID, region)
    48  	if err != nil {
    49  		return false, errors.Wrap(err, "while determining subscription flow")
    50  	}
    51  
    52  	var success bool
    53  
    54  	switch flowType {
    55  	case resource.ApplicationTemplate:
    56  		log.C(ctx).Infof("Entering application subscription flow")
    57  		success, err = r.subscriptionSvc.SubscribeTenantToApplication(ctx, providerID, subaccountTenantID, consumerTenantID, region, subscriptionAppName, subscriptionPayload)
    58  		if err != nil {
    59  			return false, err
    60  		}
    61  	case resource.Runtime:
    62  		log.C(ctx).Infof("Entering runtime subscription flow")
    63  		success, err = r.subscriptionSvc.SubscribeTenantToRuntime(ctx, providerID, subaccountTenantID, providerSubaccountID, consumerTenantID, region, subscriptionAppName)
    64  		if err != nil {
    65  			return false, err
    66  		}
    67  	default:
    68  		log.C(ctx).Infof("Nothing to subscribe to provider (%q) in region (%q)", providerID, region)
    69  	}
    70  
    71  	if err = tx.Commit(); err != nil {
    72  		return false, err
    73  	}
    74  
    75  	return success, nil
    76  }
    77  
    78  // UnsubscribeTenant unsubscribes tenant to runtime labeled with `providerID` and `region`
    79  func (r *Resolver) UnsubscribeTenant(ctx context.Context, providerID, subaccountTenantID, providerSubaccountID, consumerTenantID, region string) (bool, error) {
    80  	tx, err := r.transact.Begin()
    81  	if err != nil {
    82  		return false, err
    83  	}
    84  	defer r.transact.RollbackUnlessCommitted(ctx, tx)
    85  
    86  	ctx = persistence.SaveToContext(ctx, tx)
    87  
    88  	flowType, err := r.subscriptionSvc.DetermineSubscriptionFlow(ctx, providerID, region)
    89  	if err != nil {
    90  		return false, errors.Wrap(err, "while determining subscription flow")
    91  	}
    92  
    93  	var success bool
    94  
    95  	switch flowType {
    96  	case resource.ApplicationTemplate:
    97  		log.C(ctx).Infof("Entering application subscription flow")
    98  		success, err = r.subscriptionSvc.UnsubscribeTenantFromApplication(ctx, providerID, subaccountTenantID, region)
    99  		if err != nil {
   100  			return false, err
   101  		}
   102  	case resource.Runtime:
   103  		log.C(ctx).Infof("Entering runtime subscription flow")
   104  		success, err = r.subscriptionSvc.UnsubscribeTenantFromRuntime(ctx, providerID, subaccountTenantID, providerSubaccountID, consumerTenantID, region)
   105  		if err != nil {
   106  			return false, err
   107  		}
   108  	default:
   109  		log.C(ctx).Infof("Nothing to unsubscribe from provider(%q) with subaccount: %q in region (%q)", providerID, providerSubaccountID, region)
   110  	}
   111  
   112  	if err = tx.Commit(); err != nil {
   113  		return false, err
   114  	}
   115  
   116  	return success, nil
   117  }