-
Notifications
You must be signed in to change notification settings - Fork 9
/
catalog.go
140 lines (114 loc) · 3.69 KB
/
catalog.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package broker
import (
"errors"
"fmt"
"github.com/pivotal-cf/brokerapi/v10"
)
type Catalog interface {
Validate() error
FindService(serviceID string) (service Service, found bool)
FindServicePlan(planID string) (plan ServicePlan, found bool)
ListServicePlans() []ServicePlan
}
type BrokerCatalog struct {
Services []Service `yaml:"services,omitempty"`
}
type Service struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
Description string `yaml:"description"`
Bindable bool `yaml:"bindable"`
Tags []string `yaml:"tags,omitempty"`
PlanUpdatable bool `yaml:"plan_updateable"`
Plans []ServicePlan `yaml:"plans"`
Requires []brokerapi.RequiredPermission `yaml:"requires,omitempty"`
Metadata *brokerapi.ServiceMetadata `yaml:"metadata,omitempty"`
DashboardClient *brokerapi.ServiceDashboardClient `yaml:"dashboard_client,omitempty"`
}
type ServicePlan struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
Description string `yaml:"description"`
Free bool `yaml:"free"`
Metadata *brokerapi.ServicePlanMetadata `yaml:"metadata,omitempty"`
PlanDeletable bool `yaml:"deletable,omitempty"`
S3Properties S3Properties `yaml:"s3_properties,omitempty"`
}
type S3Properties struct {
IamPolicy string `yaml:"iam_policy,omitempty"`
BucketPolicy string `yaml:"bucket_policy,omitempty"`
Encryption string `yaml:"encryption,omitempty"`
}
func (c BrokerCatalog) Validate() error {
for _, service := range c.Services {
if err := service.Validate(); err != nil {
return fmt.Errorf("Validating Services configuration: %s", err)
}
}
return nil
}
func (c BrokerCatalog) FindService(serviceID string) (service Service, found bool) {
for _, service := range c.Services {
if service.ID == serviceID {
return service, true
}
}
return service, false
}
func (c BrokerCatalog) FindServicePlan(planID string) (plan ServicePlan, found bool) {
for _, service := range c.Services {
for _, plan := range service.Plans {
if plan.ID == planID {
return plan, true
}
}
}
return plan, false
}
func (c BrokerCatalog) ListServicePlans() []ServicePlan {
var plans []ServicePlan
for _, service := range c.Services {
for _, plan := range service.Plans {
plans = append(plans, plan)
}
}
return plans
}
func (s Service) Validate() error {
if s.ID == "" {
return fmt.Errorf("Must provide a non-empty ID (%+v)", s)
}
if s.Name == "" {
return fmt.Errorf("Must provide a non-empty Name (%+v)", s)
}
if s.Description == "" {
return fmt.Errorf("Must provide a non-empty Description (%+v)", s)
}
for _, servicePlan := range s.Plans {
if err := servicePlan.Validate(); err != nil {
return fmt.Errorf("Validating Plans configuration: %s", err)
}
}
return nil
}
func (sp ServicePlan) Validate() error {
if sp.ID == "" {
return fmt.Errorf("Must provide a non-empty ID (%+v)", sp)
}
if sp.Name == "" {
return fmt.Errorf("Must provide a non-empty Name (%+v)", sp)
}
if sp.Description == "" {
return fmt.Errorf("Must provide a non-empty Description (%+v)", sp)
}
if err := sp.S3Properties.Validate(); err != nil {
return fmt.Errorf("Validating S3 Properties configuration: %s", err)
}
return nil
}
func (eq S3Properties) Validate() error {
if len(eq.IamPolicy) == 0 {
return errors.New("Must provide a non-empty IAM Policy")
}
return nil
}