Skip to content

Commit

Permalink
If not specified plan should be free as default (#548)
Browse files Browse the repository at this point in the history
* default for plan.free should be true
  • Loading branch information
kerenlahav authored Aug 23, 2020
1 parent 4a19d22 commit acb50e7
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 10 deletions.
6 changes: 4 additions & 2 deletions pkg/types/service_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type ServicePlan struct {

CatalogID string `json:"catalog_id"`
CatalogName string `json:"catalog_name"`
Free bool `json:"free"`
Free *bool `json:"free,omitempty"`
Bindable *bool `json:"bindable,omitempty"`
PlanUpdatable *bool `json:"plan_updateable,omitempty"`

Expand All @@ -54,7 +54,9 @@ func (e *ServicePlan) Equals(obj Object) bool {
plan := obj.(*ServicePlan)
if e.Name != plan.Name ||
e.ServiceOfferingID != plan.ServiceOfferingID ||
e.Free != plan.Free ||
(e.Free == nil && plan.Free != nil) ||
(e.Free != nil && plan.Free == nil) ||
(e.Free != nil && plan.Free != nil && *e.Free != *plan.Free) ||
(e.Bindable == nil && plan.Bindable != nil) ||
(e.Bindable != nil && plan.Bindable == nil) ||
(e.Bindable != nil && plan.Bindable != nil && *e.Bindable != *plan.Bindable) ||
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func createServicePlan(now time.Time) Object {
Description: "description",
CatalogID: "1",
CatalogName: "catname",
Free: true,
Free: &trueVar,
Bindable: &trueVar,
PlanUpdatable: &trueVar,
Metadata: []byte("metadata"),
Expand Down
14 changes: 12 additions & 2 deletions storage/postgres/service_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (sp *ServicePlan) ToObject() (types.Object, error) {
Description: sp.Description,
CatalogID: sp.CatalogID,
CatalogName: sp.CatalogName,
Free: sp.Free,
Free: &sp.Free,
Bindable: toBoolPointer(sp.Bindable),
PlanUpdatable: toBoolPointer(sp.PlanUpdatable),
Metadata: getJSONRawMessage(sp.Metadata),
Expand All @@ -74,6 +74,16 @@ func (sp *ServicePlan) FromObject(object types.Object) (storage.Entity, error) {
if !ok {
return nil, fmt.Errorf("object is not of type ServicePlan")
}

isFree := func() bool {
if plan.Free == nil {
//If not specified, plan should be free as default
return true
} else {
return *plan.Free
}
}

return &ServicePlan{
BaseEntity: BaseEntity{
ID: plan.ID,
Expand All @@ -84,7 +94,7 @@ func (sp *ServicePlan) FromObject(object types.Object) (storage.Entity, error) {
},
Name: plan.Name,
Description: plan.Description,
Free: plan.Free,
Free: isFree(),
Bindable: toNullBool(plan.Bindable),
PlanUpdatable: toNullBool(plan.PlanUpdatable),
CatalogID: plan.CatalogID,
Expand Down
6 changes: 3 additions & 3 deletions test/notification_test/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ var _ = Describe("Notifications Suite", func() {

for _, serviceOffering := range serviceOfferings.ServiceOfferings {
for _, servicePlan := range serviceOffering.Plans {
if servicePlan.Free {
if *servicePlan.Free {
found := false
for _, notification := range notificationsAfterOp.Notifications {
if notification.Resource == types.VisibilityType && notification.Type == types.CREATED {
Expand Down Expand Up @@ -349,7 +349,7 @@ var _ = Describe("Notifications Suite", func() {
ctx = common.NewTestContextBuilderWithSecurity().WithSMExtensions(func(ctx context.Context, smb *sm.ServiceManagerBuilder, e env.Environment) error {
smb.WithCreateInterceptorProvider(types.ServiceBrokerType, &interceptors.PublicPlanCreateInterceptorProvider{
IsCatalogPlanPublicFunc: func(broker *types.ServiceBroker, catalogService *types.ServiceOffering, catalogPlan *types.ServicePlan) (b bool, e error) {
return catalogPlan.Free, nil
return *catalogPlan.Free, nil
},
SupportedPlatforms: func(ctx context.Context, plan *types.ServicePlan, repository storage.Repository) ([]string, error) {
return service_plans.ResolveSupportedPlatformIDsForPlans(ctx, []*types.ServicePlan{plan}, repository)
Expand All @@ -358,7 +358,7 @@ var _ = Describe("Notifications Suite", func() {

smb.WithUpdateInterceptorProvider(types.ServiceBrokerType, &interceptors.PublicPlanUpdateInterceptorProvider{
IsCatalogPlanPublicFunc: func(broker *types.ServiceBroker, catalogService *types.ServiceOffering, catalogPlan *types.ServicePlan) (b bool, e error) {
return catalogPlan.Free, nil
return *catalogPlan.Free, nil
},
SupportedPlatforms: func(ctx context.Context, plan *types.ServicePlan, repository storage.Repository) ([]string, error) {
return service_plans.ResolveSupportedPlatformIDsForPlans(ctx, []*types.ServicePlan{plan}, repository)
Expand Down
4 changes: 2 additions & 2 deletions test/public_plans_interceptor_test/public_plans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ var _ = Describe("Service Manager Public Plans Interceptor", func() {
ctx = common.NewTestContextBuilderWithSecurity().WithSMExtensions(func(ctx context.Context, smb *sm.ServiceManagerBuilder, e env.Environment) error {
smb.WithCreateInterceptorProvider(types.ServiceBrokerType, &interceptors.PublicPlanCreateInterceptorProvider{
IsCatalogPlanPublicFunc: func(broker *types.ServiceBroker, catalogService *types.ServiceOffering, catalogPlan *types.ServicePlan) (b bool, e error) {
return catalogPlan.Free, nil
return *catalogPlan.Free, nil
},
SupportedPlatforms: func(ctx context.Context, plan *types.ServicePlan, repository storage.Repository) ([]string, error) {
return service_plans.ResolveSupportedPlatformIDsForPlans(ctx, []*types.ServicePlan{plan}, repository)
Expand All @@ -123,7 +123,7 @@ var _ = Describe("Service Manager Public Plans Interceptor", func() {

smb.WithUpdateInterceptorProvider(types.ServiceBrokerType, &interceptors.PublicPlanUpdateInterceptorProvider{
IsCatalogPlanPublicFunc: func(broker *types.ServiceBroker, catalogService *types.ServiceOffering, catalogPlan *types.ServicePlan) (b bool, e error) {
return catalogPlan.Free, nil
return *catalogPlan.Free, nil
},
SupportedPlatforms: func(ctx context.Context, plan *types.ServicePlan, repository storage.Repository) ([]string, error) {
return service_plans.ResolveSupportedPlatformIDsForPlans(ctx, []*types.ServicePlan{plan}, repository)
Expand Down
34 changes: 34 additions & 0 deletions test/storage_test/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package storage_test

import (
"context"
"fmt"
"github.com/gofrs/uuid"
"net/http"
"testing"

Expand Down Expand Up @@ -291,6 +293,38 @@ var _ = Describe("Test", func() {
Status(http.StatusOK).JSON().Object().Value("id").String().Equal(visibilityID)
})
})

Context("when plan has no 'free' property", func() {
var testPlanWithoutFree = `
{
"name": "another-paid-plan-name-%[1]s",
"id": "%[1]s",
"description": "test-description",
"bindable": true
}
`
var catalogID string

BeforeEach(func() {
UUID, err := uuid.NewV4()
Expect(err).ToNot(HaveOccurred())
catalogID = UUID.String()
plan1 := common.GenerateTestPlanFromTemplate(catalogID, testPlanWithoutFree)
UUID, err = uuid.NewV4()
Expect(err).ToNot(HaveOccurred())
serviceID := UUID.String()
service1 := common.GenerateTestServiceWithPlansWithID(serviceID, plan1)
catalog := common.NewEmptySBCatalog()
catalog.AddService(service1)
ctx.RegisterBrokerWithCatalog(catalog)
})

It("stored with free = true", func() {
plans := ctx.SMWithBasic.ListWithQuery(web.ServicePlansURL, fmt.Sprintf("fieldQuery=catalog_id eq '%s'", catalogID))
planIsFree := plans.First().Object().Value("free").Raw()
Expect(planIsFree).To(Equal(true))
})
})
})

func compareLabels(actual, expected types.Labels) {
Expand Down

0 comments on commit acb50e7

Please sign in to comment.