forked from pivotal-pez/admin-portal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
73 lines (63 loc) · 2.04 KB
/
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
package cfenv
import (
"fmt"
"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]string // credentials for the service
}
// 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, service.Tags[t]) {
result = append(result, service)
}
}
}
}
if len(result) > 0 {
return result, nil
}
return nil, fmt.Errorf("no services with tag %s", tag)
}
// 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)
}
// 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)
}