github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/storage/driver/postsql/orchestration.go (about) 1 package postsql 2 3 import ( 4 "fmt" 5 6 "github.com/kyma-project/kyma-environment-broker/internal" 7 "github.com/kyma-project/kyma-environment-broker/internal/storage/dberr" 8 "github.com/kyma-project/kyma-environment-broker/internal/storage/dbmodel" 9 "github.com/kyma-project/kyma-environment-broker/internal/storage/postsql" 10 log "github.com/sirupsen/logrus" 11 "k8s.io/apimachinery/pkg/util/wait" 12 ) 13 14 type orchestrations struct { 15 postsql.Factory 16 } 17 18 func NewOrchestrations(sess postsql.Factory) *orchestrations { 19 return &orchestrations{ 20 Factory: sess, 21 } 22 } 23 24 func (s *orchestrations) Insert(orchestration internal.Orchestration) error { 25 _, err := s.GetByID(orchestration.OrchestrationID) 26 if err == nil { 27 return dberr.AlreadyExists("orchestration with id %s already exist", orchestration.OrchestrationID) 28 } 29 30 dto, err := dbmodel.NewOrchestrationDTO(orchestration) 31 if err != nil { 32 return fmt.Errorf("while converting Orchestration to DTO: %w", err) 33 } 34 35 sess := s.NewWriteSession() 36 return wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) { 37 err := sess.InsertOrchestration(dto) 38 if err != nil { 39 log.Errorf("while saving orchestration ID %s: %v", orchestration.OrchestrationID, err) 40 return false, nil 41 } 42 return true, nil 43 }) 44 } 45 46 func (s *orchestrations) GetByID(orchestrationID string) (*internal.Orchestration, error) { 47 sess := s.NewReadSession() 48 orchestration := internal.Orchestration{} 49 var lastErr error 50 err := wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) { 51 var dto dbmodel.OrchestrationDTO 52 dto, lastErr = sess.GetOrchestrationByID(orchestrationID) 53 if lastErr != nil { 54 if dberr.IsNotFound(lastErr) { 55 return false, dberr.NotFound("Orchestration with id %s not exist", orchestrationID) 56 } 57 log.Errorf("while getting orchestration by ID %s: %v", orchestrationID, lastErr) 58 return false, nil 59 } 60 orchestration, lastErr = dto.ToOrchestration() 61 return true, nil 62 }) 63 if err != nil { 64 return nil, lastErr 65 } 66 return &orchestration, nil 67 } 68 69 func (s *orchestrations) List(filter dbmodel.OrchestrationFilter) ([]internal.Orchestration, int, int, error) { 70 sess := s.NewReadSession() 71 var ( 72 orchestrations = make([]internal.Orchestration, 0) 73 lastErr error 74 count, totalCount int 75 ) 76 err := wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) { 77 var dtos []dbmodel.OrchestrationDTO 78 dtos, count, totalCount, lastErr = sess.ListOrchestrations(filter) 79 if lastErr != nil { 80 if dberr.IsNotFound(lastErr) { 81 return false, dberr.NotFound("Orchestrations not exist") 82 } 83 log.Errorf("while getting orchestration: %v", lastErr) 84 return false, nil 85 } 86 for _, dto := range dtos { 87 var o internal.Orchestration 88 o, lastErr = dto.ToOrchestration() 89 if lastErr != nil { 90 return false, lastErr 91 } 92 orchestrations = append(orchestrations, o) 93 } 94 return true, nil 95 }) 96 if err != nil { 97 return nil, -1, -1, lastErr 98 } 99 return orchestrations, count, totalCount, nil 100 } 101 102 func (s *orchestrations) Update(orchestration internal.Orchestration) error { 103 dto, err := dbmodel.NewOrchestrationDTO(orchestration) 104 if err != nil { 105 return fmt.Errorf("while converting Orchestration to DTO: %w", err) 106 } 107 108 sess := s.NewWriteSession() 109 var lastErr dberr.Error 110 err = wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) { 111 lastErr = sess.UpdateOrchestration(dto) 112 if lastErr != nil { 113 if dberr.IsNotFound(lastErr) { 114 return false, dberr.NotFound("Orchestration with id %s not exist", orchestration.OrchestrationID) 115 } 116 log.Errorf("while updating orchestration ID %s: %v", orchestration.OrchestrationID, lastErr) 117 return false, nil 118 } 119 return true, nil 120 }) 121 if err != nil { 122 return lastErr 123 } 124 return nil 125 }