forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_plan.go
84 lines (71 loc) · 2.55 KB
/
service_plan.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
package api
import (
"fmt"
"net/url"
"strings"
"code.cloudfoundry.org/cli/cf/api/resources"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/net"
)
//go:generate counterfeiter . ServicePlanRepository
type ServicePlanRepository interface {
Search(searchParameters map[string]string) ([]models.ServicePlanFields, error)
Update(models.ServicePlanFields, string, bool) error
ListPlansFromManyServices(serviceGUIDs []string) ([]models.ServicePlanFields, error)
}
type CloudControllerServicePlanRepository struct {
config coreconfig.Reader
gateway net.Gateway
}
func NewCloudControllerServicePlanRepository(config coreconfig.Reader, gateway net.Gateway) CloudControllerServicePlanRepository {
return CloudControllerServicePlanRepository{
config: config,
gateway: gateway,
}
}
func (repo CloudControllerServicePlanRepository) Update(servicePlan models.ServicePlanFields, serviceGUID string, public bool) error {
return repo.gateway.UpdateResource(
repo.config.APIEndpoint(),
fmt.Sprintf("/v2/service_plans/%s", servicePlan.GUID),
strings.NewReader(fmt.Sprintf(`{"public":%t}`, public)),
)
}
func (repo CloudControllerServicePlanRepository) ListPlansFromManyServices(serviceGUIDs []string) ([]models.ServicePlanFields, error) {
serviceGUIDsString := strings.Join(serviceGUIDs, ",")
plans := []models.ServicePlanFields{}
err := repo.gateway.ListPaginatedResources(
repo.config.APIEndpoint(),
fmt.Sprintf("/v2/service_plans?q=%s", url.QueryEscape("service_guid IN "+serviceGUIDsString)),
resources.ServicePlanResource{},
func(resource interface{}) bool {
if plan, ok := resource.(resources.ServicePlanResource); ok {
plans = append(plans, plan.ToFields())
}
return true
})
return plans, err
}
func (repo CloudControllerServicePlanRepository) Search(queryParams map[string]string) (plans []models.ServicePlanFields, err error) {
err = repo.gateway.ListPaginatedResources(
repo.config.APIEndpoint(),
combineQueryParametersWithURI("/v2/service_plans", queryParams),
resources.ServicePlanResource{},
func(resource interface{}) bool {
if sp, ok := resource.(resources.ServicePlanResource); ok {
plans = append(plans, sp.ToFields())
}
return true
})
return
}
func combineQueryParametersWithURI(uri string, queryParams map[string]string) string {
if len(queryParams) == 0 {
return uri
}
params := []string{}
for key, value := range queryParams {
params = append(params, url.QueryEscape(key+":"+value))
}
return uri + "?q=" + strings.Join(params, "%3B")
}