forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
broker_converter.go
102 lines (83 loc) · 2.56 KB
/
broker_converter.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
package servicecatalog
import (
"sort"
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"
"github.com/kyma-project/kyma/components/ui-api-layer/internal/gqlschema"
"github.com/pkg/errors"
)
type brokerConverter struct{}
func (c *brokerConverter) ToGQL(item *v1beta1.ClusterServiceBroker) (*gqlschema.ServiceBroker, error) {
if item == nil {
return nil, nil
}
conditions := item.Status.Conditions
c.sortConditions(conditions)
returnStatus := c.conditionToBrokerStatus(conditions)
labels := new(gqlschema.JSON)
err := labels.UnmarshalGQL(c.mapStringMapToJson(item.Labels))
if err != nil {
return nil, errors.Wrap(err, "While unmarshalling labels")
}
broker := gqlschema.ServiceBroker{
Name: item.Name,
Status: returnStatus,
CreationTimestamp: item.CreationTimestamp.Time,
Labels: *labels,
Url: item.Spec.URL,
}
return &broker, nil
}
func (c *brokerConverter) ToGQLs(in []*v1beta1.ClusterServiceBroker) ([]gqlschema.ServiceBroker, error) {
var result []gqlschema.ServiceBroker
for _, u := range in {
converted, err := c.ToGQL(u)
if err != nil {
return nil, err
}
if converted != nil {
result = append(result, *converted)
}
}
return result, nil
}
func (c *brokerConverter) sortConditions(conditions []v1beta1.ServiceBrokerCondition) {
sort.SliceStable(conditions, func(i, j int) bool {
return conditions[j].LastTransitionTime.Before(&conditions[i].LastTransitionTime)
})
}
func (c *brokerConverter) conditionToBrokerStatus(conditions []v1beta1.ServiceBrokerCondition) gqlschema.ServiceBrokerStatus {
readyStatus, exists := c.findReadyCondition(conditions)
if exists {
return gqlschema.ServiceBrokerStatus{
Ready: readyStatus.Status == v1beta1.ConditionTrue,
Reason: readyStatus.Reason,
Message: readyStatus.Message,
}
}
var reason, message string
if len(conditions) > 0 {
condition := conditions[0]
reason = condition.Reason
message = condition.Message
}
return gqlschema.ServiceBrokerStatus{
Ready: false,
Reason: reason,
Message: message,
}
}
func (c *brokerConverter) findReadyCondition(conditions []v1beta1.ServiceBrokerCondition) (v1beta1.ServiceBrokerCondition, bool) {
for _, condition := range conditions {
if condition.Type == "Ready" {
return condition, true
}
}
return v1beta1.ServiceBrokerCondition{}, false
}
func (c *brokerConverter) mapStringMapToJson(labels map[string]string) map[string]interface{} {
result := make(map[string]interface{})
for k, v := range labels {
result[k] = v
}
return result
}