github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/storage/driver/memory/orchestration.go (about)

     1  package memory
     2  
     3  import (
     4  	"sort"
     5  	"sync"
     6  
     7  	"github.com/kyma-project/kyma-environment-broker/internal"
     8  
     9  	"github.com/kyma-project/kyma-environment-broker/common/pagination"
    10  
    11  	"github.com/kyma-project/kyma-environment-broker/internal/storage/dberr"
    12  	"github.com/kyma-project/kyma-environment-broker/internal/storage/dbmodel"
    13  )
    14  
    15  type orchestrations struct {
    16  	mu sync.Mutex
    17  
    18  	orchestrations map[string]internal.Orchestration
    19  }
    20  
    21  func NewOrchestrations() *orchestrations {
    22  	return &orchestrations{
    23  		orchestrations: make(map[string]internal.Orchestration, 0),
    24  	}
    25  }
    26  
    27  func (s *orchestrations) Insert(orchestration internal.Orchestration) error {
    28  	s.mu.Lock()
    29  	defer s.mu.Unlock()
    30  	s.orchestrations[orchestration.OrchestrationID] = orchestration
    31  
    32  	return nil
    33  }
    34  
    35  func (s *orchestrations) GetByID(orchestrationID string) (*internal.Orchestration, error) {
    36  	s.mu.Lock()
    37  	defer s.mu.Unlock()
    38  
    39  	inst, ok := s.orchestrations[orchestrationID]
    40  	if !ok {
    41  		return nil, dberr.NotFound("orchestration with id %s not exist", orchestrationID)
    42  	}
    43  
    44  	return &inst, nil
    45  }
    46  
    47  func (s *orchestrations) List(filter dbmodel.OrchestrationFilter) ([]internal.Orchestration, int, int, error) {
    48  	s.mu.Lock()
    49  	defer s.mu.Unlock()
    50  
    51  	result := make([]internal.Orchestration, 0)
    52  	offset := pagination.ConvertPageAndPageSizeToOffset(filter.PageSize, filter.Page)
    53  
    54  	orchestrations := s.filter(filter)
    55  	s.sortByCreatedAt(orchestrations)
    56  
    57  	for i := offset; (filter.PageSize < 1 || i < offset+filter.PageSize) && i < len(orchestrations); i++ {
    58  		result = append(result, s.orchestrations[orchestrations[i].OrchestrationID])
    59  	}
    60  
    61  	return result,
    62  		len(result),
    63  		len(orchestrations),
    64  		nil
    65  }
    66  
    67  func (s *orchestrations) Update(orchestration internal.Orchestration) error {
    68  	s.mu.Lock()
    69  	defer s.mu.Unlock()
    70  	if _, ok := s.orchestrations[orchestration.OrchestrationID]; !ok {
    71  		return dberr.NotFound("orchestration with id %s not exist", orchestration.OrchestrationID)
    72  
    73  	}
    74  	s.orchestrations[orchestration.OrchestrationID] = orchestration
    75  
    76  	return nil
    77  }
    78  
    79  func (s *orchestrations) sortByCreatedAt(orchestrations []internal.Orchestration) {
    80  	sort.Slice(orchestrations, func(i, j int) bool {
    81  		return orchestrations[i].CreatedAt.Before(orchestrations[j].CreatedAt)
    82  	})
    83  }
    84  
    85  func (s *orchestrations) filter(filter dbmodel.OrchestrationFilter) []internal.Orchestration {
    86  	orchestrations := make([]internal.Orchestration, 0, len(s.orchestrations))
    87  	equal := func(a, b string) bool { return a == b }
    88  	for _, v := range s.orchestrations {
    89  		if ok := matchFilter(string(v.Type), filter.Types, equal); !ok {
    90  			continue
    91  		}
    92  		if ok := matchFilter(v.State, filter.States, equal); !ok {
    93  			continue
    94  		}
    95  
    96  		orchestrations = append(orchestrations, v)
    97  	}
    98  
    99  	return orchestrations
   100  }