-
Notifications
You must be signed in to change notification settings - Fork 84
/
service_instance.go
108 lines (89 loc) · 3.35 KB
/
service_instance.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
package types
import (
"encoding/json"
"github.com/kyma-incubator/compass/components/instance-creator/internal/client/resources"
"github.com/pkg/errors"
"github.com/kyma-incubator/compass/components/instance-creator/internal/client/paths"
)
const (
// ServiceInstancesType represents the type of the ServiceInstances struct; used primarily for logging purposes
ServiceInstancesType = "service instances"
// ServiceInstanceType represents the type of the ServiceInstance struct; used primarily for logging purposes
ServiceInstanceType = "service instance"
)
// ServiceInstanceReqBody is the request body when a Service Instance is being created
type ServiceInstanceReqBody struct {
Name string `json:"name"`
ServicePlanID string `json:"service_plan_id"`
Parameters json.RawMessage `json:"parameters,omitempty"`
Labels map[string][]string `json:"labels,omitempty"`
}
// GetResourceName gets the ServiceInstance name from the request body
func (rb *ServiceInstanceReqBody) GetResourceName() string {
return rb.Name
}
// ServiceInstance represents a Service Instance
type ServiceInstance struct {
ID string `json:"id"`
Name string `json:"name"`
ServicePlanID string `json:"service_plan_id"`
PlatformID string `json:"platform_id"`
}
// GetResourceID gets the ServiceInstance ID
func (s *ServiceInstance) GetResourceID() string {
return s.ID
}
// GetResourceType gets the return type of the ServiceInstance
func (s *ServiceInstance) GetResourceType() string {
return ServiceInstanceType
}
// GetResourceURLPath gets the ServiceInstance URL Path
func (s *ServiceInstance) GetResourceURLPath() string {
return paths.ServiceInstancesPath
}
// GetResourceName gets the Service
func (s *ServiceInstance) GetResourceName() string {
return s.Name
}
// ServiceInstances represents a collection of Service Instance
type ServiceInstances struct {
NumItems int `json:"num_items"`
Items []*ServiceInstance `json:"items"`
}
// GetType gets the type of the ServiceInstances
func (si *ServiceInstances) GetType() string {
return ServiceInstancesType
}
// GetURLPath gets the URL Path of the ServiceInstance
func (si *ServiceInstances) GetURLPath() string {
return paths.ServiceInstancesPath
}
// GetIDs gets the IDs of all ServiceInstances
func (sis *ServiceInstances) GetIDs() []string {
ids := make([]string, 0, sis.NumItems)
for _, si := range sis.Items {
ids = append(ids, si.ID)
}
return ids
}
// ServiceInstanceMatchParameters holds all the necessary fields that are used when matching ServiceInstances
type ServiceInstanceMatchParameters struct {
ServiceInstanceName string
}
// Match matches a ServiceInstance based on some criteria
func (sip *ServiceInstanceMatchParameters) Match(resources resources.Resources) (string, error) {
serviceInstances, ok := resources.(*ServiceInstances)
if !ok {
return "", errors.New("while type asserting Resources to ServiceInstances")
}
for _, si := range serviceInstances.Items {
if si.Name == sip.ServiceInstanceName {
return si.ID, nil
}
}
return "", nil // for ServiceInstances we don't want to fail if nothing is found
}
// MatchMultiple matches several ServiceInstances based on some criteria
func (sip *ServiceInstanceMatchParameters) MatchMultiple(resources resources.Resources) ([]string, error) {
return nil, nil // implement me when needed
}