-
Notifications
You must be signed in to change notification settings - Fork 404
/
asset_service.go
128 lines (107 loc) · 3.99 KB
/
asset_service.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package rafter
import (
"fmt"
"github.com/kyma-project/kyma/components/console-backend-service/internal/domain/rafter/extractor"
"github.com/kyma-project/kyma/components/console-backend-service/internal/domain/rafter/pretty"
"github.com/kyma-project/kyma/components/console-backend-service/internal/resource"
notifierResource "github.com/kyma-project/kyma/components/console-backend-service/pkg/resource"
"github.com/kyma-project/rafter/pkg/apis/rafter/v1beta1"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/cache"
)
const (
Namespace_AssetGroupName_Index = "namespace/assetGroupName"
Namespace_AssetGroupName_Type_Index = "namespace/assetGroupName/type"
)
//go:generate mockery -name=assetSvc -output=automock -outpkg=automock -case=underscore
//go:generate failery -name=assetSvc -case=underscore -output disabled -outpkg disabled
type assetSvc interface {
Find(namespace, name string) (*v1beta1.Asset, error)
ListForAssetGroupByType(namespace, assetGroupName string, types []string) ([]*v1beta1.Asset, error)
Subscribe(listener notifierResource.Listener)
Unsubscribe(listener notifierResource.Listener)
}
type assetService struct {
*resource.Service
notifier notifierResource.Notifier
extractor extractor.AssetUnstructuredExtractor
}
func newAssetService(serviceFactory *resource.ServiceFactory) (*assetService, error) {
svc := &assetService{
Service: serviceFactory.ForResource(schema.GroupVersionResource{
Version: v1beta1.GroupVersion.Version,
Group: v1beta1.GroupVersion.Group,
Resource: "assets",
}),
extractor: extractor.AssetUnstructuredExtractor{},
}
err := svc.AddIndexers(cache.Indexers{
Namespace_AssetGroupName_Index: func(obj interface{}) ([]string, error) {
entity, err := svc.extractor.Do(obj)
if err != nil {
return nil, errors.New("Cannot convert item")
}
return []string{fmt.Sprintf("%s/%s", entity.Namespace, entity.Labels[AssetGroupLabel])}, nil
},
Namespace_AssetGroupName_Type_Index: func(obj interface{}) ([]string, error) {
entity, err := svc.extractor.Do(obj)
if err != nil {
return nil, errors.New("Cannot convert item")
}
return []string{fmt.Sprintf("%s/%s/%s", entity.Namespace, entity.Labels[AssetGroupLabel], entity.Labels[TypeLabel])}, nil
},
})
if err != nil {
return nil, errors.Wrap(err, "while adding indexers")
}
notifier := notifierResource.NewNotifier()
svc.Informer.AddEventHandler(notifier)
svc.notifier = notifier
return svc, nil
}
func (svc *assetService) Find(namespace, name string) (*v1beta1.Asset, error) {
key := fmt.Sprintf("%s/%s", namespace, name)
item, exists, err := svc.Informer.GetStore().GetByKey(key)
if err != nil || !exists {
return nil, err
}
asset, err := svc.extractor.Do(item)
if err != nil {
return nil, errors.Wrapf(err, "Incorrect item type: %T, should be: *%s", item, pretty.AssetType)
}
return asset, nil
}
func (svc *assetService) ListForAssetGroupByType(namespace, assetGroupName string, types []string) ([]*v1beta1.Asset, error) {
var items []interface{}
var err error
if len(types) == 0 {
items, err = svc.Informer.GetIndexer().ByIndex(Namespace_AssetGroupName_Index, fmt.Sprintf("%s/%s", namespace, assetGroupName))
} else {
for _, typeArg := range types {
its, err := svc.Informer.GetIndexer().ByIndex(Namespace_AssetGroupName_Type_Index, fmt.Sprintf("%s/%s/%s", namespace, assetGroupName, typeArg))
if err != nil {
return nil, err
}
items = append(items, its...)
}
}
if err != nil {
return nil, err
}
var assets []*v1beta1.Asset
for _, item := range items {
asset, err := svc.extractor.Do(item)
if err != nil {
return nil, errors.Wrapf(err, "Incorrect item type: %T, should be: *%s", item, pretty.AssetType)
}
assets = append(assets, asset)
}
return assets, nil
}
func (svc *assetService) Subscribe(listener notifierResource.Listener) {
svc.notifier.AddListener(listener)
}
func (svc *assetService) Unsubscribe(listener notifierResource.Listener) {
svc.notifier.DeleteListener(listener)
}