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

     1  package bundleinstanceauth
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
     7  
     8  	"github.com/kyma-incubator/compass/components/director/internal/model"
     9  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    10  	"github.com/kyma-incubator/compass/components/director/pkg/persistence"
    11  	"github.com/pkg/errors"
    12  
    13  	"github.com/kyma-incubator/compass/components/director/pkg/log"
    14  )
    15  
    16  // Service missing godoc
    17  //go:generate mockery --name=Service --output=automock --outpkg=automock --case=underscore --disable-version-string
    18  type Service interface {
    19  	RequestDeletion(ctx context.Context, instanceAuth *model.BundleInstanceAuth, defaultBundleInstanceAuth *model.Auth) (bool, error)
    20  	Create(ctx context.Context, bundleID string, in model.BundleInstanceAuthRequestInput, defaultAuth *model.Auth, requestInputSchema *string) (string, error)
    21  	CreateBundleInstanceAuth(ctx context.Context, bundleID string, in model.BundleInstanceAuthCreateInput, requestInputSchema *string) (string, error)
    22  	Update(ctx context.Context, instanceAuth *model.BundleInstanceAuth) error
    23  	Get(ctx context.Context, id string) (*model.BundleInstanceAuth, error)
    24  	SetAuth(ctx context.Context, id string, in model.BundleInstanceAuthSetInput) error
    25  	Delete(ctx context.Context, id string) error
    26  }
    27  
    28  // Converter missing godoc
    29  //go:generate mockery --name=Converter --output=automock --outpkg=automock --case=underscore --disable-version-string
    30  type Converter interface {
    31  	ToGraphQL(in *model.BundleInstanceAuth) (*graphql.BundleInstanceAuth, error)
    32  	RequestInputFromGraphQL(in graphql.BundleInstanceAuthRequestInput) model.BundleInstanceAuthRequestInput
    33  	SetInputFromGraphQL(in graphql.BundleInstanceAuthSetInput) (model.BundleInstanceAuthSetInput, error)
    34  	CreateInputFromGraphQL(in graphql.BundleInstanceAuthCreateInput) (model.BundleInstanceAuthCreateInput, error)
    35  	UpdateInputFromGraphQL(in graphql.BundleInstanceAuthUpdateInput) (model.BundleInstanceAuthUpdateInput, error)
    36  }
    37  
    38  // BundleService missing godoc
    39  //go:generate mockery --name=BundleService --output=automock --outpkg=automock --case=underscore --disable-version-string
    40  type BundleService interface {
    41  	Get(ctx context.Context, id string) (*model.Bundle, error)
    42  }
    43  
    44  // BundleConverter missing godoc
    45  //go:generate mockery --name=BundleConverter --output=automock --outpkg=automock --case=underscore --disable-version-string
    46  type BundleConverter interface {
    47  	ToGraphQL(in *model.Bundle) (*graphql.Bundle, error)
    48  }
    49  
    50  // Resolver missing godoc
    51  type Resolver struct {
    52  	transact persistence.Transactioner
    53  	svc      Service
    54  	bndlSvc  BundleService
    55  	conv     Converter
    56  	bndlConv BundleConverter
    57  }
    58  
    59  // NewResolver missing godoc
    60  func NewResolver(transact persistence.Transactioner, svc Service, bndlSvc BundleService, conv Converter, bndlConv BundleConverter) *Resolver {
    61  	return &Resolver{
    62  		transact: transact,
    63  		svc:      svc,
    64  		bndlSvc:  bndlSvc,
    65  		conv:     conv,
    66  		bndlConv: bndlConv,
    67  	}
    68  }
    69  
    70  // BundleByInstanceAuth missing godoc
    71  func (r *Resolver) BundleByInstanceAuth(ctx context.Context, authID string) (*graphql.Bundle, error) {
    72  	tx, err := r.transact.Begin()
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	defer r.transact.RollbackUnlessCommitted(ctx, tx)
    77  
    78  	ctx = persistence.SaveToContext(ctx, tx)
    79  
    80  	bndlInstanceAuth, err := r.svc.Get(ctx, authID)
    81  	if err != nil {
    82  		if apperrors.IsNotFoundError(err) {
    83  			return nil, tx.Commit()
    84  		}
    85  		return nil, err
    86  	}
    87  
    88  	pkg, err := r.bndlSvc.Get(ctx, bndlInstanceAuth.BundleID)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  
    93  	err = tx.Commit()
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	return r.bndlConv.ToGraphQL(pkg)
    99  }
   100  
   101  // BundleInstanceAuth missing godoc
   102  func (r *Resolver) BundleInstanceAuth(ctx context.Context, id string) (*graphql.BundleInstanceAuth, error) {
   103  	tx, err := r.transact.Begin()
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  	defer r.transact.RollbackUnlessCommitted(ctx, tx)
   108  
   109  	ctx = persistence.SaveToContext(ctx, tx)
   110  
   111  	bndlInstanceAuth, err := r.svc.Get(ctx, id)
   112  	if err != nil {
   113  		if apperrors.IsNotFoundError(err) {
   114  			return nil, tx.Commit()
   115  		}
   116  		return nil, err
   117  	}
   118  
   119  	err = tx.Commit()
   120  	if err != nil {
   121  		return nil, err
   122  	}
   123  
   124  	return r.conv.ToGraphQL(bndlInstanceAuth)
   125  }
   126  
   127  // DeleteBundleInstanceAuth missing godoc
   128  func (r *Resolver) DeleteBundleInstanceAuth(ctx context.Context, authID string) (*graphql.BundleInstanceAuth, error) {
   129  	tx, err := r.transact.Begin()
   130  	if err != nil {
   131  		return nil, err
   132  	}
   133  
   134  	defer r.transact.RollbackUnlessCommitted(ctx, tx)
   135  	ctx = persistence.SaveToContext(ctx, tx)
   136  
   137  	instanceAuth, err := r.svc.Get(ctx, authID)
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  
   142  	err = r.svc.Delete(ctx, authID)
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  
   147  	log.C(ctx).Infof("BundleInstanceAuth with id %s successfully deleted", authID)
   148  
   149  	err = tx.Commit()
   150  	if err != nil {
   151  		return nil, err
   152  	}
   153  
   154  	return r.conv.ToGraphQL(instanceAuth)
   155  }
   156  
   157  // SetBundleInstanceAuth missing godoc
   158  func (r *Resolver) SetBundleInstanceAuth(ctx context.Context, authID string, in graphql.BundleInstanceAuthSetInput) (*graphql.BundleInstanceAuth, error) {
   159  	tx, err := r.transact.Begin()
   160  	if err != nil {
   161  		return nil, err
   162  	}
   163  
   164  	defer r.transact.RollbackUnlessCommitted(ctx, tx)
   165  	ctx = persistence.SaveToContext(ctx, tx)
   166  
   167  	log.C(ctx).Infof("Setting credentials for BundleInstanceAuth with id %s", authID)
   168  
   169  	convertedIn, err := r.conv.SetInputFromGraphQL(in)
   170  	if err != nil {
   171  		return nil, errors.Wrapf(err, "while converting BundleInstanceAuth with id %s from GraphQL", authID)
   172  	}
   173  
   174  	err = r.svc.SetAuth(ctx, authID, convertedIn)
   175  	if err != nil {
   176  		return nil, err
   177  	}
   178  
   179  	instanceAuth, err := r.svc.Get(ctx, authID)
   180  	if err != nil {
   181  		return nil, err
   182  	}
   183  
   184  	log.C(ctx).Infof("Credentials successfully set for BundleInstanceAuth with id %s", authID)
   185  
   186  	err = tx.Commit()
   187  	if err != nil {
   188  		return nil, err
   189  	}
   190  
   191  	return r.conv.ToGraphQL(instanceAuth)
   192  }
   193  
   194  // RequestBundleInstanceAuthCreation missing godoc
   195  func (r *Resolver) RequestBundleInstanceAuthCreation(ctx context.Context, bundleID string, in graphql.BundleInstanceAuthRequestInput) (*graphql.BundleInstanceAuth, error) {
   196  	tx, err := r.transact.Begin()
   197  	if err != nil {
   198  		return nil, err
   199  	}
   200  
   201  	defer r.transact.RollbackUnlessCommitted(ctx, tx)
   202  	ctx = persistence.SaveToContext(ctx, tx)
   203  
   204  	log.C(ctx).Infof("Requesting BundleInstanceAuth creation for Bundle with id %s", bundleID)
   205  
   206  	bndl, err := r.bndlSvc.Get(ctx, bundleID)
   207  	if err != nil {
   208  		return nil, err
   209  	}
   210  
   211  	convertedIn := r.conv.RequestInputFromGraphQL(in)
   212  
   213  	instanceAuthID, err := r.svc.Create(ctx, bundleID, convertedIn, bndl.DefaultInstanceAuth, bndl.InstanceAuthRequestInputSchema)
   214  	if err != nil {
   215  		return nil, err
   216  	}
   217  
   218  	instanceAuth, err := r.svc.Get(ctx, instanceAuthID)
   219  	if err != nil {
   220  		return nil, err
   221  	}
   222  	log.C(ctx).Infof("Successfully created BundleInstanceAuth with id %s for Bundle with id %s", instanceAuthID, bundleID)
   223  
   224  	err = tx.Commit()
   225  	if err != nil {
   226  		return nil, err
   227  	}
   228  
   229  	return r.conv.ToGraphQL(instanceAuth)
   230  }
   231  
   232  // RequestBundleInstanceAuthDeletion missing godoc
   233  func (r *Resolver) RequestBundleInstanceAuthDeletion(ctx context.Context, authID string) (*graphql.BundleInstanceAuth, error) {
   234  	tx, err := r.transact.Begin()
   235  	if err != nil {
   236  		return nil, err
   237  	}
   238  
   239  	defer r.transact.RollbackUnlessCommitted(ctx, tx)
   240  	ctx = persistence.SaveToContext(ctx, tx)
   241  
   242  	log.C(ctx).Infof("Requesting BundleInstanceAuth deletion for BundleInstanceAuth with id %s", authID)
   243  
   244  	instanceAuth, err := r.svc.Get(ctx, authID)
   245  	if err != nil {
   246  		return nil, err
   247  	}
   248  
   249  	bndl, err := r.bndlSvc.Get(ctx, instanceAuth.BundleID)
   250  	if err != nil {
   251  		return nil, err
   252  	}
   253  
   254  	deleted, err := r.svc.RequestDeletion(ctx, instanceAuth, bndl.DefaultInstanceAuth)
   255  	if err != nil {
   256  		return nil, err
   257  	}
   258  
   259  	if !deleted {
   260  		instanceAuth, err = r.svc.Get(ctx, authID) // get InstanceAuth once again for new status
   261  		if err != nil {
   262  			return nil, err
   263  		}
   264  	}
   265  
   266  	log.C(ctx).Infof("BundleInstanceAuth with id %s successfully deleted.", authID)
   267  
   268  	err = tx.Commit()
   269  	if err != nil {
   270  		return nil, err
   271  	}
   272  
   273  	return r.conv.ToGraphQL(instanceAuth)
   274  }
   275  
   276  // CreateBundleInstanceAuth creates a BundleInstanceAuth for a Bundle with ID - bundleID from a given BundleInstanceAuthCreateInput
   277  func (r *Resolver) CreateBundleInstanceAuth(ctx context.Context, bundleID string, in graphql.BundleInstanceAuthCreateInput) (*graphql.BundleInstanceAuth, error) {
   278  	tx, err := r.transact.Begin()
   279  	if err != nil {
   280  		return nil, err
   281  	}
   282  
   283  	defer r.transact.RollbackUnlessCommitted(ctx, tx)
   284  	ctx = persistence.SaveToContext(ctx, tx)
   285  
   286  	log.C(ctx).Infof("Creating BundleInstanceAuth for Bundle with id %q", bundleID)
   287  
   288  	bndl, err := r.bndlSvc.Get(ctx, bundleID)
   289  	if err != nil {
   290  		return nil, err
   291  	}
   292  
   293  	convertedIn, err := r.conv.CreateInputFromGraphQL(in)
   294  	if err != nil {
   295  		return nil, err
   296  	}
   297  
   298  	instanceAuthID, err := r.svc.CreateBundleInstanceAuth(ctx, bundleID, convertedIn, bndl.InstanceAuthRequestInputSchema)
   299  	if err != nil {
   300  		return nil, err
   301  	}
   302  
   303  	instanceAuth, err := r.svc.Get(ctx, instanceAuthID)
   304  	if err != nil {
   305  		return nil, err
   306  	}
   307  
   308  	if err = tx.Commit(); err != nil {
   309  		return nil, err
   310  	}
   311  
   312  	log.C(ctx).Infof("Successfully created BundleInstanceAuth with id %q for Bundle with id %q", instanceAuthID, bundleID)
   313  
   314  	return r.conv.ToGraphQL(instanceAuth)
   315  }
   316  
   317  // UpdateBundleInstanceAuth updates a BundleInstanceAuth with id for a Bundle with ID - bundleID from a given BundleInstanceAuthUpdateInput
   318  func (r *Resolver) UpdateBundleInstanceAuth(ctx context.Context, id string, bundleID string, in graphql.BundleInstanceAuthUpdateInput) (*graphql.BundleInstanceAuth, error) {
   319  	tx, err := r.transact.Begin()
   320  	if err != nil {
   321  		return nil, err
   322  	}
   323  
   324  	defer r.transact.RollbackUnlessCommitted(ctx, tx)
   325  	ctx = persistence.SaveToContext(ctx, tx)
   326  
   327  	log.C(ctx).Infof("Creating BundleInstanceAuth for Bundle with id %q", bundleID)
   328  
   329  	bndl, err := r.bndlSvc.Get(ctx, bundleID)
   330  	if err != nil {
   331  		return nil, err
   332  	}
   333  
   334  	convertedIn, err := r.conv.UpdateInputFromGraphQL(in)
   335  	if err != nil {
   336  		return nil, err
   337  	}
   338  
   339  	bndlInstAuth, err := r.svc.Get(ctx, id)
   340  	if err != nil {
   341  		return nil, err
   342  	}
   343  
   344  	bndlInstAuth.SetFromUpdateInput(convertedIn)
   345  
   346  	if in.InputParams != nil {
   347  		log.C(ctx).Debugf("Validating BundleInstanceAuth request input for Bundle with id %q", bundleID)
   348  		if err = validateInputParamsAgainstSchema(convertedIn.InputParams, bndl.InstanceAuthRequestInputSchema); err != nil {
   349  			return nil, errors.Wrapf(err, "while validating BundleInstanceAuth request input for Bundle with id %q", bundleID)
   350  		}
   351  	}
   352  
   353  	if err = r.svc.Update(ctx, bndlInstAuth); err != nil {
   354  		return nil, errors.Wrapf(err, "while updating BundleInstanceAuth with id %q for Bundle with id %q", id, bundleID)
   355  	}
   356  
   357  	instanceAuth, err := r.svc.Get(ctx, id)
   358  	if err != nil {
   359  		return nil, err
   360  	}
   361  
   362  	if err = tx.Commit(); err != nil {
   363  		return nil, err
   364  	}
   365  
   366  	log.C(ctx).Infof("Successfully updated BundleInstanceAuth with id %q for Bundle with id %q", id, bundleID)
   367  
   368  	return r.conv.ToGraphQL(instanceAuth)
   369  }