github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/bundleinstanceauth/repository.go (about) 1 package bundleinstanceauth 2 3 import ( 4 "context" 5 6 "github.com/kyma-incubator/compass/components/director/pkg/log" 7 8 "github.com/kyma-incubator/compass/components/director/pkg/resource" 9 10 "github.com/pkg/errors" 11 12 "github.com/kyma-incubator/compass/components/director/internal/model" 13 "github.com/kyma-incubator/compass/components/director/internal/repo" 14 "github.com/kyma-incubator/compass/components/director/pkg/apperrors" 15 ) 16 17 const tableName string = `public.bundle_instance_auths` 18 19 var ( 20 idColumns = []string{"id"} 21 updatableColumns = []string{"context", "input_params", "auth_value", "status_condition", "status_timestamp", "status_message", "status_reason"} 22 tableColumns = []string{"id", "owner_id", "bundle_id", "context", "input_params", "auth_value", "status_condition", "status_timestamp", "status_message", "status_reason", "runtime_id", "runtime_context_id"} 23 ) 24 25 // EntityConverter missing godoc 26 //go:generate mockery --name=EntityConverter --output=automock --outpkg=automock --case=underscore --disable-version-string 27 type EntityConverter interface { 28 ToEntity(in *model.BundleInstanceAuth) (*Entity, error) 29 FromEntity(entity *Entity) (*model.BundleInstanceAuth, error) 30 } 31 32 type repository struct { 33 creator repo.CreatorGlobal 34 singleGetter repo.SingleGetter 35 lister repo.Lister 36 updater repo.Updater 37 deleter repo.Deleter 38 conv EntityConverter 39 } 40 41 // NewRepository missing godoc 42 func NewRepository(conv EntityConverter) *repository { 43 return &repository{ 44 // TODO: We will use the default creator which does not ensures parent access. This is because in order to ensure that the caller 45 // tenant has access to the parent for BIAs we need to check for either owner or non-owner access. The straight-forward 46 // scenario will be to have a non-owner access to the bundle once a formation is created. Then we check for whatever access 47 // the caller has to the parent and allow it. 48 // However, this cannot be done before formations redesign and due to this the formation check will still take place 49 // in the pkg/scenario/directive.go. Once formation redesign in in place we can remove this directive and here we can use non-global creator. 50 creator: repo.NewCreatorGlobal(resource.BundleInstanceAuth, tableName, tableColumns), 51 singleGetter: repo.NewSingleGetter(tableName, tableColumns), 52 lister: repo.NewLister(tableName, tableColumns), 53 deleter: repo.NewDeleter(tableName), 54 updater: repo.NewUpdater(tableName, updatableColumns, idColumns), 55 conv: conv, 56 } 57 } 58 59 // Create missing godoc 60 func (r *repository) Create(ctx context.Context, item *model.BundleInstanceAuth) error { 61 if item == nil { 62 return apperrors.NewInternalError("item cannot be nil") 63 } 64 65 entity, err := r.conv.ToEntity(item) 66 if err != nil { 67 return errors.Wrap(err, "while converting BundleInstanceAuth model to entity") 68 } 69 70 log.C(ctx).Debugf("Persisting BundleInstanceAuth entity with id %s to db", item.ID) 71 err = r.creator.Create(ctx, entity) 72 if err != nil { 73 return errors.Wrapf(err, "while saving entity with id %s to db", item.ID) 74 } 75 76 return nil 77 } 78 79 // GetByID missing godoc 80 func (r *repository) GetByID(ctx context.Context, tenantID string, id string) (*model.BundleInstanceAuth, error) { 81 var entity Entity 82 if err := r.singleGetter.Get(ctx, resource.BundleInstanceAuth, tenantID, repo.Conditions{repo.NewEqualCondition("id", id)}, repo.NoOrderBy, &entity); err != nil { 83 return nil, err 84 } 85 86 itemModel, err := r.conv.FromEntity(&entity) 87 if err != nil { 88 return nil, errors.Wrap(err, "while converting BundleInstanceAuth entity to model") 89 } 90 91 return itemModel, nil 92 } 93 94 // GetForBundle missing godoc 95 func (r *repository) GetForBundle(ctx context.Context, tenant string, id string, bundleID string) (*model.BundleInstanceAuth, error) { 96 var ent Entity 97 98 conditions := repo.Conditions{ 99 repo.NewEqualCondition("id", id), 100 repo.NewEqualCondition("bundle_id", bundleID), 101 } 102 if err := r.singleGetter.Get(ctx, resource.BundleInstanceAuth, tenant, conditions, repo.NoOrderBy, &ent); err != nil { 103 return nil, err 104 } 105 106 bndlModel, err := r.conv.FromEntity(&ent) 107 if err != nil { 108 return nil, errors.Wrap(err, "while creating Bundle model from entity") 109 } 110 111 return bndlModel, nil 112 } 113 114 // ListByBundleID missing godoc 115 func (r *repository) ListByBundleID(ctx context.Context, tenantID string, bundleID string) ([]*model.BundleInstanceAuth, error) { 116 var entities Collection 117 118 conditions := repo.Conditions{ 119 repo.NewEqualCondition("bundle_id", bundleID), 120 } 121 122 err := r.lister.List(ctx, resource.BundleInstanceAuth, tenantID, &entities, conditions...) 123 124 if err != nil { 125 return nil, err 126 } 127 128 return r.multipleFromEntities(entities) 129 } 130 131 // ListByRuntimeID missing godoc 132 func (r *repository) ListByRuntimeID(ctx context.Context, tenantID string, runtimeID string) ([]*model.BundleInstanceAuth, error) { 133 var entities Collection 134 135 conditions := repo.Conditions{ 136 repo.NewEqualCondition("runtime_id", runtimeID), 137 } 138 139 err := r.lister.List(ctx, resource.BundleInstanceAuth, tenantID, &entities, conditions...) 140 141 if err != nil { 142 return nil, err 143 } 144 145 return r.multipleFromEntities(entities) 146 } 147 148 // Update missing godoc 149 func (r *repository) Update(ctx context.Context, tenant string, item *model.BundleInstanceAuth) error { 150 if item == nil { 151 return apperrors.NewInternalError("item cannot be nil") 152 } 153 154 entity, err := r.conv.ToEntity(item) 155 if err != nil { 156 return errors.Wrap(err, "while converting model to entity") 157 } 158 159 log.C(ctx).Debugf("Updating BundleInstanceAuth entity with id %s in db", item.ID) 160 return r.updater.UpdateSingle(ctx, resource.BundleInstanceAuth, tenant, entity) 161 } 162 163 // Delete missing godoc 164 func (r *repository) Delete(ctx context.Context, tenantID string, id string) error { 165 return r.deleter.DeleteOne(ctx, resource.BundleInstanceAuth, tenantID, repo.Conditions{repo.NewEqualCondition("id", id)}) 166 } 167 168 func (r *repository) multipleFromEntities(entities Collection) ([]*model.BundleInstanceAuth, error) { 169 items := make([]*model.BundleInstanceAuth, 0, len(entities)) 170 for _, ent := range entities { 171 m, err := r.conv.FromEntity(&ent) 172 if err != nil { 173 return nil, errors.Wrap(err, "while creating BundleInstanceAuth model from entity") 174 } 175 items = append(items, m) 176 } 177 return items, nil 178 }