forked from kyma-project/kyma
/
asset_service.go
90 lines (75 loc) · 2.91 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
package docstopic
import (
"context"
"github.com/kyma-project/kyma/components/asset-store-controller-manager/pkg/apis/assetstore/v1alpha2"
"github.com/kyma-project/kyma/components/cms-controller-manager/pkg/handler/docstopic"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
type assetService struct {
client client.Client
scheme *runtime.Scheme
}
func newAssetService(client client.Client, scheme *runtime.Scheme) *assetService {
return &assetService{
client: client,
scheme: scheme,
}
}
func (s *assetService) List(ctx context.Context, namespace string, labels map[string]string) ([]docstopic.CommonAsset, error) {
instances := &v1alpha2.AssetList{}
err := s.client.List(ctx, client.MatchingLabels(labels), instances)
if err != nil {
return nil, errors.Wrapf(err, "while listing Assets in namespace %s", namespace)
}
commons := make([]docstopic.CommonAsset, 0, len(instances.Items))
for _, instance := range instances.Items {
if instance.Namespace != namespace {
continue
}
common := s.assetToCommon(instance)
commons = append(commons, common)
}
return commons, nil
}
func (s *assetService) Create(ctx context.Context, docsTopic v1.Object, commonAsset docstopic.CommonAsset) error {
instance := &v1alpha2.Asset{
ObjectMeta: commonAsset.ObjectMeta,
Spec: v1alpha2.AssetSpec{
CommonAssetSpec: commonAsset.Spec,
},
}
if err := controllerutil.SetControllerReference(docsTopic, instance, s.scheme); err != nil {
return errors.Wrapf(err, "while creating Asset %s in namespace %s", commonAsset.Name, commonAsset.Namespace)
}
return s.client.Create(ctx, instance)
}
func (s *assetService) Update(ctx context.Context, commonAsset docstopic.CommonAsset) error {
instance := &v1alpha2.Asset{}
err := s.client.Get(ctx, types.NamespacedName{Name: commonAsset.Name, Namespace: commonAsset.Namespace}, instance)
if err != nil {
return errors.Wrapf(err, "while updating Asset %s in namespace %s", commonAsset.Name, commonAsset.Namespace)
}
updated := instance.DeepCopy()
updated.Spec.CommonAssetSpec = commonAsset.Spec
return s.client.Update(ctx, updated)
}
func (s *assetService) Delete(ctx context.Context, commonAsset docstopic.CommonAsset) error {
instance := &v1alpha2.Asset{}
err := s.client.Get(ctx, types.NamespacedName{Name: commonAsset.Name, Namespace: commonAsset.Namespace}, instance)
if err != nil {
return errors.Wrapf(err, "while deleting Asset %s in namespace %s", commonAsset.Name, commonAsset.Namespace)
}
return s.client.Delete(ctx, instance)
}
func (s *assetService) assetToCommon(instance v1alpha2.Asset) docstopic.CommonAsset {
return docstopic.CommonAsset{
ObjectMeta: instance.ObjectMeta,
Spec: instance.Spec.CommonAssetSpec,
Status: instance.Status.CommonAssetStatus,
}
}