-
Notifications
You must be signed in to change notification settings - Fork 3
/
tenant_cluster_binding_repo.go
90 lines (78 loc) · 3.21 KB
/
tenant_cluster_binding_repo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2022 Monoskope Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package repositories
import (
"context"
"github.com/finleap-connect/monoskope/pkg/domain/errors"
"github.com/finleap-connect/monoskope/pkg/domain/projections"
es "github.com/finleap-connect/monoskope/pkg/eventsourcing"
"github.com/google/uuid"
)
type tenantClusterBindingRepository struct {
DomainRepository[*projections.TenantClusterBinding]
}
// TenantClusterBindingRepository is a repository for reading and writing tenantclusterbinding projections.
type TenantClusterBindingRepository interface {
DomainRepository[*projections.TenantClusterBinding]
GetByTenantId(ctx context.Context, tenantId uuid.UUID) ([]*projections.TenantClusterBinding, error)
GetByClusterId(ctx context.Context, tenantId uuid.UUID) ([]*projections.TenantClusterBinding, error)
GetByTenantAndClusterId(ctx context.Context, tenantId, clusterId uuid.UUID) (*projections.TenantClusterBinding, error)
}
// NewTenantClusterBindingRepository creates a repository for reading and writing tenantclusterbinding projections.
func NewTenantClusterBindingRepository(repository es.Repository[*projections.TenantClusterBinding]) TenantClusterBindingRepository {
return &tenantClusterBindingRepository{
NewDomainRepository(repository),
}
}
// GetByTenantId searches for the TenantClusterBinding projections by tenant id.
func (r *tenantClusterBindingRepository) GetByTenantId(ctx context.Context, tenantId uuid.UUID) ([]*projections.TenantClusterBinding, error) {
ps, err := r.AllWith(ctx, false)
if err != nil {
return nil, err
}
var bindings []*projections.TenantClusterBinding
for _, p := range ps {
if p.TenantId == tenantId.String() {
bindings = append(bindings, p)
}
}
return bindings, nil
}
// GetByClusterId searches for the TenantClusterBinding projections by cluster id.
func (r *tenantClusterBindingRepository) GetByClusterId(ctx context.Context, clusterId uuid.UUID) ([]*projections.TenantClusterBinding, error) {
ps, err := r.AllWith(ctx, false)
if err != nil {
return nil, err
}
var bindings []*projections.TenantClusterBinding
for _, p := range ps {
if p.ClusterId == clusterId.String() {
bindings = append(bindings, p)
}
}
return bindings, nil
}
// GetByTenantAndClusterId searches the TenantClusterBinding projection by tenant and cluster id.
func (r *tenantClusterBindingRepository) GetByTenantAndClusterId(ctx context.Context, tenantId, clusterId uuid.UUID) (*projections.TenantClusterBinding, error) {
ps, err := r.AllWith(ctx, false)
if err != nil {
return nil, err
}
for _, p := range ps {
if p.TenantId == tenantId.String() && p.ClusterId == clusterId.String() {
return p, nil
}
}
return nil, errors.ErrTenantClusterBindingNotFound
}