github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/orchestration/runtime_lister.go (about) 1 package orchestration 2 3 import ( 4 "fmt" 5 6 "github.com/kyma-project/kyma-environment-broker/common/runtime" 7 runtimeInt "github.com/kyma-project/kyma-environment-broker/internal/runtime" 8 "github.com/kyma-project/kyma-environment-broker/internal/storage" 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/sirupsen/logrus" 12 ) 13 14 type RuntimeLister struct { 15 instancesDb storage.Instances 16 operationsDb storage.Operations 17 converter runtimeInt.Converter 18 log logrus.FieldLogger 19 } 20 21 func NewRuntimeLister(instancesDb storage.Instances, operationsDb storage.Operations, converter runtimeInt.Converter, log logrus.FieldLogger) *RuntimeLister { 22 return &RuntimeLister{ 23 instancesDb: instancesDb, 24 operationsDb: operationsDb, 25 converter: converter, 26 log: log, 27 } 28 } 29 30 func (rl RuntimeLister) ListAllRuntimes() ([]runtime.RuntimeDTO, error) { 31 instances, _, _, err := rl.instancesDb.List(dbmodel.InstanceFilter{}) 32 if err != nil { 33 return nil, fmt.Errorf("while listing instances from DB: %w", err) 34 } 35 36 runtimes := make([]runtime.RuntimeDTO, 0, len(instances)) 37 for _, inst := range instances { 38 dto, err := rl.converter.NewDTO(inst) 39 if err != nil { 40 rl.log.Errorf("cannot convert instance to DTO: %s", err.Error()) 41 continue 42 } 43 44 pOprs, err := rl.operationsDb.ListProvisioningOperationsByInstanceID(inst.InstanceID) 45 if err != nil { 46 rl.log.Errorf("while getting provision operation for instance %s: %s", inst.InstanceID, err.Error()) 47 continue 48 } 49 if len(pOprs) > 0 { 50 rl.converter.ApplyProvisioningOperation(&dto, &pOprs[len(pOprs)-1]) 51 } 52 if len(pOprs) > 1 { 53 rl.converter.ApplyUnsuspensionOperations(&dto, pOprs[:len(pOprs)-1]) 54 } 55 56 dOprs, err := rl.operationsDb.ListDeprovisioningOperationsByInstanceID(inst.InstanceID) 57 if err != nil && !dberr.IsNotFound(err) { 58 rl.log.Errorf("while getting deprovision operation for instance %s: %s", inst.InstanceID, err.Error()) 59 continue 60 } 61 if len(dOprs) > 0 { 62 rl.converter.ApplyDeprovisioningOperation(&dto, &dOprs[0]) 63 } 64 65 rl.converter.ApplySuspensionOperations(&dto, dOprs) 66 67 runtimes = append(runtimes, dto) 68 } 69 70 return runtimes, nil 71 }