forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan_service.go
99 lines (79 loc) · 2.65 KB
/
plan_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
package servicecatalog
import (
"errors"
"fmt"
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"
"k8s.io/client-go/tools/cache"
)
type planService struct {
informer cache.SharedIndexInformer
}
func newPlanService(informer cache.SharedIndexInformer) *planService {
informer.AddIndexers(cache.Indexers{
"relatedServiceClassName": func(obj interface{}) ([]string, error) {
servicePlan, ok := obj.(*v1beta1.ClusterServicePlan)
if !ok {
return nil, errors.New("Cannot convert item")
}
return []string{servicePlan.Spec.ClusterServiceClassRef.Name}, nil
},
"classNameAndPlanExternalName": func(obj interface{}) ([]string, error) {
servicePlan, ok := obj.(*v1beta1.ClusterServicePlan)
if !ok {
return nil, errors.New("Cannot convert item")
}
str := planIndexKey(servicePlan.Spec.ClusterServiceClassRef.Name, servicePlan.Spec.ExternalName)
return []string{str}, nil
},
})
return &planService{
informer: informer,
}
}
func (svc *planService) Find(name string) (*v1beta1.ClusterServicePlan, error) {
item, exists, err := svc.informer.GetStore().GetByKey(name)
if err != nil || !exists {
return nil, err
}
servicePlan, ok := item.(*v1beta1.ClusterServicePlan)
if !ok {
return nil, fmt.Errorf("Incorrect item type: %T, should be: *ClusterServicePlan", item)
}
return servicePlan, nil
}
func (svc *planService) FindByExternalNameForClass(planExternalName, className string) (*v1beta1.ClusterServicePlan, error) {
items, err := svc.informer.GetIndexer().ByIndex("classNameAndPlanExternalName", planIndexKey(className, planExternalName))
if err != nil {
return nil, err
}
if len(items) == 0 {
return nil, nil
}
if len(items) > 1 {
return nil, fmt.Errorf("Multiple ServicePlan resources with the same externalName %s", planExternalName)
}
item := items[0]
servicePlan, ok := item.(*v1beta1.ClusterServicePlan)
if !ok {
return nil, fmt.Errorf("Incorrect item type: %T, should be: *ClusterServicePlan", item)
}
return servicePlan, nil
}
func (svc *planService) ListForClass(name string) ([]*v1beta1.ClusterServicePlan, error) {
plans, err := svc.informer.GetIndexer().ByIndex("relatedServiceClassName", name)
if err != nil {
return nil, err
}
var servicePlans []*v1beta1.ClusterServicePlan
for _, item := range plans {
servicePlan, ok := item.(*v1beta1.ClusterServicePlan)
if !ok {
return nil, fmt.Errorf("Incorrect item type: %T, should be: *ClusterServicePlan", item)
}
servicePlans = append(servicePlans, servicePlan)
}
return servicePlans, nil
}
func planIndexKey(planExternalName, className string) string {
return fmt.Sprintf("%s/%s", className, planExternalName)
}