-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathservice.go
More file actions
127 lines (112 loc) · 3.43 KB
/
service.go
File metadata and controls
127 lines (112 loc) · 3.43 KB
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
125
126
127
package cfenv
import (
"fmt"
"regexp"
"strings"
)
// Service describes a bound service. For bindable services Cloud Foundry will
// add connection details to the VCAP_SERVICES environment variable when you
// restart your application, after binding a service instance to your
// application.
//
// The results are returned as a JSON document that contains an object for each
// service for which one or more instances are bound to the application. The
// service object contains a child object for each service instance of that
// service that is bound to the application.
type Service struct {
Name string // name of the service
Label string // label of the service
Tags []string // tags for the service
Plan string // plan of the service
Credentials map[string]interface{} // credentials for the service
VolumeMounts []map[string]string `mapstructure:"volume_mounts"` // volume mount info as provided by the nfsbroker
}
func (s *Service) CredentialString(key string) (string, bool) {
credential, ok := s.Credentials[key].(string)
return credential, ok
}
// Services is an association of service labels to a slice of services with that
// label.
type Services map[string][]Service
// WithTag finds services with the specified tag.
func (s *Services) WithTag(tag string) ([]Service, error) {
result := []Service{}
for _, services := range *s {
for i := range services {
service := services[i]
for _, t := range service.Tags {
if strings.EqualFold(tag, t) {
result = append(result, service)
break
}
}
}
}
if len(result) > 0 {
return result, nil
}
return nil, fmt.Errorf("no services with tag %s", tag)
}
// WithTag finds services with a tag pattern.
func (s *Services) WithTagUsingPattern(tagPattern string) ([]Service, error) {
result := []Service{}
for _, services := range *s {
for i := range services {
service := services[i]
for _, t := range service.Tags {
if s.match(tagPattern, t) {
result = append(result, service)
break
}
}
}
}
if len(result) > 0 {
return result, nil
}
return nil, fmt.Errorf("no services with tag pattern %s", tagPattern)
}
// WithLabel finds the service with the specified label.
func (s *Services) WithLabel(label string) ([]Service, error) {
for l, services := range *s {
if strings.EqualFold(label, l) {
return services, nil
}
}
return nil, fmt.Errorf("no services with label %s", label)
}
func (s *Services) match(matcher, content string) bool {
regex, err := regexp.Compile("(?i)^" + matcher + "$")
if err != nil {
return false
}
return regex.MatchString(content)
}
// WithName finds the service with a name pattern.
func (s *Services) WithNameUsingPattern(namePattern string) ([]Service, error) {
result := []Service{}
for _, services := range *s {
for i := range services {
service := services[i]
if s.match(namePattern, service.Name) {
result = append(result, service)
}
}
}
if len(result) > 0 {
return result, nil
}
return nil, fmt.Errorf("no service with name pattern %s", namePattern)
}
// WithName finds the service with the specified name.
func (s *Services) WithName(name string) (*Service, error) {
for _, services := range *s {
for i := range services {
service := services[i]
if strings.EqualFold(name, service.Name) {
return &service, nil
}
}
}
return nil, fmt.Errorf("no service with name %s", name)
}