forked from kyma-project/kyma
/
replicaset_resolver.go
124 lines (103 loc) · 5.09 KB
/
replicaset_resolver.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
package k8s
import (
"context"
"github.com/golang/glog"
"github.com/kyma-project/kyma/components/ui-api-layer/internal/domain/k8s/pretty"
"github.com/kyma-project/kyma/components/ui-api-layer/internal/gqlerror"
"github.com/kyma-project/kyma/components/ui-api-layer/internal/gqlschema"
"github.com/kyma-project/kyma/components/ui-api-layer/internal/pager"
"github.com/pkg/errors"
api "k8s.io/api/apps/v1"
)
//go:generate mockery -name=replicaSetSvc -output=automock -outpkg=automock -case=underscore
type replicaSetSvc interface {
Find(name, namespace string) (*api.ReplicaSet, error)
List(namespace string, pagingParams pager.PagingParams) ([]*api.ReplicaSet, error)
Update(name, namespace string, update api.ReplicaSet) (*api.ReplicaSet, error)
Delete(name, namespace string) error
}
//go:generate mockery -name=gqlReplicaSetConverter -output=automock -outpkg=automock -case=underscore
type gqlReplicaSetConverter interface {
ToGQL(in *api.ReplicaSet) (*gqlschema.ReplicaSet, error)
ToGQLs(in []*api.ReplicaSet) ([]gqlschema.ReplicaSet, error)
GQLJSONToReplicaSet(in gqlschema.JSON) (api.ReplicaSet, error)
}
type replicaSetResolver struct {
replicaSetSvc replicaSetSvc
replicaSetConverter gqlReplicaSetConverter
}
func newReplicaSetResolver(replicaSetLister replicaSetSvc) *replicaSetResolver {
return &replicaSetResolver{
replicaSetSvc: replicaSetLister,
replicaSetConverter: &replicaSetConverter{},
}
}
func (r *replicaSetResolver) ReplicaSetQuery(ctx context.Context, name, namespace string) (*gqlschema.ReplicaSet, error) {
replicaSet, err := r.replicaSetSvc.Find(name, namespace)
if err != nil {
glog.Error(errors.Wrapf(err, "while getting %s with name %s from namespace %s", pretty.ReplicaSet, name, namespace))
return nil, gqlerror.New(err, pretty.ReplicaSet, gqlerror.WithName(name), gqlerror.WithNamespace(namespace))
}
if replicaSet == nil {
return nil, nil
}
converted, err := r.replicaSetConverter.ToGQL(replicaSet)
if err != nil {
glog.Error(errors.Wrapf(err, "while converting %s from namespace %s", pretty.ReplicaSet, namespace))
return nil, gqlerror.New(err, pretty.ReplicaSet, gqlerror.WithName(name), gqlerror.WithNamespace(namespace))
}
return converted, nil
}
func (r *replicaSetResolver) ReplicaSetsQuery(ctx context.Context, namespace string, first *int, offset *int) ([]gqlschema.ReplicaSet, error) {
replicaSets, err := r.replicaSetSvc.List(namespace, pager.PagingParams{
First: first,
Offset: offset,
})
if err != nil {
glog.Error(errors.Wrapf(err, "while listing %s from namespace %s", pretty.ReplicaSets, namespace))
return nil, gqlerror.New(err, pretty.ReplicaSets, gqlerror.WithNamespace(namespace))
}
converted, err := r.replicaSetConverter.ToGQLs(replicaSets)
if err != nil {
glog.Error(errors.Wrapf(err, "while converting %s from namespace %s", pretty.ReplicaSets, namespace))
return nil, gqlerror.New(err, pretty.ReplicaSets, gqlerror.WithNamespace(namespace))
}
return converted, nil
}
func (r *replicaSetResolver) UpdateReplicaSetMutation(ctx context.Context, name string, namespace string, update gqlschema.JSON) (*gqlschema.ReplicaSet, error) {
replicaSet, err := r.replicaSetConverter.GQLJSONToReplicaSet(update)
if err != nil {
glog.Error(errors.Wrapf(err, "while updating %s `%s` from namespace `%s`", pretty.ReplicaSet, name, namespace))
return nil, gqlerror.New(err, pretty.ReplicaSet, gqlerror.WithName(name), gqlerror.WithNamespace(namespace))
}
updated, err := r.replicaSetSvc.Update(name, namespace, replicaSet)
if err != nil {
glog.Error(errors.Wrapf(err, "while updating %s `%s` from namespace %s", pretty.ReplicaSet, name, namespace))
return nil, gqlerror.New(err, pretty.ReplicaSet, gqlerror.WithName(name), gqlerror.WithNamespace(namespace))
}
updatedGql, err := r.replicaSetConverter.ToGQL(updated)
if err != nil {
glog.Error(errors.Wrapf(err, "while converting %s `%s` from namespace %s", pretty.ReplicaSet, name, namespace))
return nil, gqlerror.New(err, pretty.ReplicaSet, gqlerror.WithName(name), gqlerror.WithNamespace(namespace))
}
return updatedGql, nil
}
func (r *replicaSetResolver) DeleteReplicaSetMutation(ctx context.Context, name string, namespace string) (*gqlschema.ReplicaSet, error) {
replicaSet, err := r.replicaSetSvc.Find(name, namespace)
if err != nil {
glog.Error(errors.Wrapf(err, "while finding %s `%s` in namespace `%s`", pretty.ReplicaSet, name, namespace))
return nil, gqlerror.New(err, pretty.ReplicaSet, gqlerror.WithName(name), gqlerror.WithNamespace(namespace))
}
replicaSetCopy := replicaSet.DeepCopy()
deletedReplicaSet, err := r.replicaSetConverter.ToGQL(replicaSetCopy)
if err != nil {
glog.Error(errors.Wrapf(err, "while converting %s", pretty.ReplicaSet))
return nil, gqlerror.New(err, pretty.ReplicaSet, gqlerror.WithName(name), gqlerror.WithNamespace(namespace))
}
err = r.replicaSetSvc.Delete(name, namespace)
if err != nil {
glog.Error(errors.Wrapf(err, "while deleting %s `%s` from namespace `%s`", pretty.ReplicaSet, name, namespace))
return nil, gqlerror.New(err, pretty.ReplicaSet, gqlerror.WithName(name), gqlerror.WithNamespace(namespace))
}
return deletedReplicaSet, nil
}