github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/storage/driver/memory/runtime_state.go (about) 1 package memory 2 3 import ( 4 "sort" 5 "sync" 6 7 "github.com/kyma-project/kyma-environment-broker/internal/storage/dberr" 8 9 "github.com/kyma-project/kyma-environment-broker/internal" 10 ) 11 12 type runtimeState struct { 13 mu sync.Mutex 14 15 runtimeStates map[string]internal.RuntimeState 16 } 17 18 func NewRuntimeStates() *runtimeState { 19 return &runtimeState{ 20 runtimeStates: make(map[string]internal.RuntimeState, 0), 21 } 22 } 23 24 func (s *runtimeState) Insert(runtimeState internal.RuntimeState) error { 25 s.mu.Lock() 26 defer s.mu.Unlock() 27 s.runtimeStates[runtimeState.ID] = runtimeState 28 29 return nil 30 } 31 32 func (s *runtimeState) ListByRuntimeID(runtimeID string) ([]internal.RuntimeState, error) { 33 s.mu.Lock() 34 defer s.mu.Unlock() 35 36 result := make([]internal.RuntimeState, 0) 37 38 for _, state := range s.runtimeStates { 39 if state.RuntimeID == runtimeID { 40 result = append(result, state) 41 } 42 } 43 44 sort.Slice(result, func(i, j int) bool { 45 return result[i].CreatedAt.After(result[j].CreatedAt) 46 }) 47 return result, nil 48 } 49 50 func (s *runtimeState) GetByOperationID(operationID string) (internal.RuntimeState, error) { 51 s.mu.Lock() 52 defer s.mu.Unlock() 53 54 for _, rs := range s.runtimeStates { 55 if rs.OperationID == operationID { 56 return rs, nil 57 } 58 } 59 60 return internal.RuntimeState{}, dberr.NotFound("runtime state with operation ID %s not found", operationID) 61 } 62 63 func (s *runtimeState) GetLatestByRuntimeID(runtimeID string) (internal.RuntimeState, error) { 64 states, err := s.getRuntimeStatesByRuntimeID(runtimeID) 65 if err != nil { 66 return internal.RuntimeState{}, err 67 } 68 69 return states[0], nil 70 } 71 72 func (s *runtimeState) GetLatestWithKymaVersionByRuntimeID(runtimeID string) (internal.RuntimeState, error) { 73 states, err := s.getRuntimeStatesByRuntimeID(runtimeID) 74 if err != nil { 75 return internal.RuntimeState{}, err 76 } 77 78 for _, state := range states { 79 if state.KymaVersion != "" { 80 return state, nil 81 } 82 if state.ClusterSetup != nil && state.ClusterSetup.KymaConfig.Version != "" { 83 return state, nil 84 } 85 if state.KymaConfig.Version != "" { 86 return state, nil 87 } 88 } 89 90 return internal.RuntimeState{}, dberr.NotFound("runtime state with Reconciler input for runtime with ID: %s not found", runtimeID) 91 } 92 93 func (s *runtimeState) GetLatestWithReconcilerInputByRuntimeID(runtimeID string) (internal.RuntimeState, error) { 94 states, err := s.getRuntimeStatesByRuntimeID(runtimeID) 95 if err != nil { 96 return internal.RuntimeState{}, err 97 } 98 99 for _, state := range states { 100 if state.ClusterSetup != nil { 101 return state, nil 102 } 103 } 104 105 return internal.RuntimeState{}, dberr.NotFound("runtime state with Reconciler input for runtime with ID: %s not found", runtimeID) 106 } 107 108 func (s *runtimeState) GetLatestWithOIDCConfigByRuntimeID(runtimeID string) (internal.RuntimeState, error) { 109 states, err := s.getRuntimeStatesByRuntimeID(runtimeID) 110 if err != nil { 111 return internal.RuntimeState{}, err 112 } 113 114 for _, state := range states { 115 if state.ClusterConfig.OidcConfig != nil { 116 return state, nil 117 } 118 } 119 120 return internal.RuntimeState{}, dberr.NotFound("runtime state with OIDC config for runtime with ID: %s not found", runtimeID) 121 } 122 123 func (s *runtimeState) getRuntimeStatesByRuntimeID(runtimeID string) ([]internal.RuntimeState, error) { 124 states, err := s.ListByRuntimeID(runtimeID) 125 if err != nil { 126 return nil, err 127 } 128 if len(states) == 0 { 129 return nil, dberr.NotFound("runtime state for runtime with ID: %s not found", runtimeID) 130 } 131 return states, nil 132 }