github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/storage/postsql/factory.go (about) 1 package postsql 2 3 import ( 4 "time" 5 6 dbr "github.com/gocraft/dbr" 7 "github.com/kyma-project/kyma-environment-broker/common/events" 8 "github.com/kyma-project/kyma-environment-broker/internal" 9 "github.com/kyma-project/kyma-environment-broker/internal/storage/dberr" 10 "github.com/kyma-project/kyma-environment-broker/internal/storage/dbmodel" 11 "github.com/kyma-project/kyma-environment-broker/internal/storage/predicate" 12 ) 13 14 //go:generate mockery --name=Factory 15 type Factory interface { 16 NewReadSession() ReadSession 17 NewWriteSession() WriteSession 18 NewSessionWithinTransaction() (WriteSessionWithinTransaction, dberr.Error) 19 } 20 21 //go:generate mockery --name=ReadSession 22 type ReadSession interface { 23 FindAllInstancesJoinedWithOperation(prct ...predicate.Predicate) ([]dbmodel.InstanceWithOperationDTO, dberr.Error) 24 FindAllInstancesForRuntimes(runtimeIdList []string) ([]dbmodel.InstanceDTO, dberr.Error) 25 FindAllInstancesForSubAccounts(subAccountslist []string) ([]dbmodel.InstanceDTO, dberr.Error) 26 GetInstanceByID(instanceID string) (dbmodel.InstanceDTO, dberr.Error) 27 GetLastOperation(instanceID string) (dbmodel.OperationDTO, dberr.Error) 28 GetOperationByID(opID string) (dbmodel.OperationDTO, dberr.Error) 29 GetNotFinishedOperationsByType(operationType internal.OperationType) ([]dbmodel.OperationDTO, dberr.Error) 30 CountNotFinishedOperationsByInstanceID(instanceID string) (int, dberr.Error) 31 GetOperationByTypeAndInstanceID(inID string, opType internal.OperationType) (dbmodel.OperationDTO, dberr.Error) 32 GetOperationByInstanceID(inID string) (dbmodel.OperationDTO, dberr.Error) 33 GetOperationsByTypeAndInstanceID(inID string, opType internal.OperationType) ([]dbmodel.OperationDTO, dberr.Error) 34 GetOperationsByInstanceID(inID string) ([]dbmodel.OperationDTO, dberr.Error) 35 GetOperationsForIDs(opIdList []string) ([]dbmodel.OperationDTO, dberr.Error) 36 ListOperations(filter dbmodel.OperationFilter) ([]dbmodel.OperationDTO, int, int, error) 37 ListOperationsByType(operationType internal.OperationType) ([]dbmodel.OperationDTO, dberr.Error) 38 GetOperationStats() ([]dbmodel.OperationStatEntry, error) 39 GetInstanceStats() ([]dbmodel.InstanceByGlobalAccountIDStatEntry, error) 40 GetERSContextStats() ([]dbmodel.InstanceERSContextStatsEntry, error) 41 GetNumberOfInstancesForGlobalAccountID(globalAccountID string) (int, error) 42 GetRuntimeStateByOperationID(operationID string) (dbmodel.RuntimeStateDTO, dberr.Error) 43 ListRuntimeStateByRuntimeID(runtimeID string) ([]dbmodel.RuntimeStateDTO, dberr.Error) 44 GetOrchestrationByID(oID string) (dbmodel.OrchestrationDTO, dberr.Error) 45 ListOrchestrations(filter dbmodel.OrchestrationFilter) ([]dbmodel.OrchestrationDTO, int, int, error) 46 ListInstances(filter dbmodel.InstanceFilter) ([]dbmodel.InstanceDTO, int, int, error) 47 ListOperationsByOrchestrationID(orchestrationID string, filter dbmodel.OperationFilter) ([]dbmodel.OperationDTO, int, int, error) 48 ListOperationsInTimeRange(from, to time.Time) ([]dbmodel.OperationDTO, error) 49 GetOperationStatsForOrchestration(orchestrationID string) ([]dbmodel.OperationStatEntry, error) 50 GetLatestRuntimeStateByRuntimeID(runtimeID string) (dbmodel.RuntimeStateDTO, dberr.Error) 51 GetLatestRuntimeStateWithReconcilerInputByRuntimeID(runtimeID string) (dbmodel.RuntimeStateDTO, dberr.Error) 52 GetLatestRuntimeStateWithKymaVersionByRuntimeID(runtimeID string) (dbmodel.RuntimeStateDTO, dberr.Error) 53 GetLatestRuntimeStateWithOIDCConfigByRuntimeID(runtimeID string) (dbmodel.RuntimeStateDTO, dberr.Error) 54 ListEvents(filter events.EventFilter) ([]events.EventDTO, error) 55 } 56 57 //go:generate mockery --name=WriteSession 58 type WriteSession interface { 59 InsertInstance(instance dbmodel.InstanceDTO) dberr.Error 60 UpdateInstance(instance dbmodel.InstanceDTO) dberr.Error 61 DeleteInstance(instanceID string) dberr.Error 62 InsertOperation(dto dbmodel.OperationDTO) dberr.Error 63 UpdateOperation(dto dbmodel.OperationDTO) dberr.Error 64 InsertOrchestration(o dbmodel.OrchestrationDTO) dberr.Error 65 UpdateOrchestration(o dbmodel.OrchestrationDTO) dberr.Error 66 InsertRuntimeState(state dbmodel.RuntimeStateDTO) dberr.Error 67 InsertEvent(level events.EventLevel, message, instanceID, operationID string) dberr.Error 68 DeleteEvents(until time.Time) dberr.Error 69 } 70 71 type Transaction interface { 72 Commit() dberr.Error 73 RollbackUnlessCommitted() 74 } 75 76 //go:generate mockery --name=WriteSessionWithinTransaction 77 type WriteSessionWithinTransaction interface { 78 WriteSession 79 Transaction 80 } 81 82 type factory struct { 83 connection *dbr.Connection 84 } 85 86 func NewFactory(connection *dbr.Connection) Factory { 87 return &factory{ 88 connection: connection, 89 } 90 } 91 92 func (sf *factory) NewReadSession() ReadSession { 93 return readSession{ 94 session: sf.connection.NewSession(nil), 95 } 96 } 97 98 func (sf *factory) NewWriteSession() WriteSession { 99 return writeSession{ 100 session: sf.connection.NewSession(nil), 101 } 102 } 103 104 func (sf *factory) NewSessionWithinTransaction() (WriteSessionWithinTransaction, dberr.Error) { 105 dbSession := sf.connection.NewSession(nil) 106 dbTransaction, err := dbSession.Begin() 107 108 if err != nil { 109 return nil, dberr.Internal("Failed to start transaction: %s", err) 110 } 111 112 return writeSession{ 113 session: dbSession, 114 transaction: dbTransaction, 115 }, nil 116 }