Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore space_guids param when listing service plans and allow include=space.organization #3431

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions api/payloads/params/include.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,13 @@ import (
"fmt"
"net/url"
"strings"

"code.cloudfoundry.org/korifi/api/payloads/validation"
jellidation "github.com/jellydator/validation"
)

type IncludeResourceRule struct {
RelationshipPath []string
Fields []string
}

func (r IncludeResourceRule) Validate() error {
return jellidation.ValidateStruct(&r,
jellidation.Field(&r.RelationshipPath, jellidation.Each(validation.OneOf("service_offering", "service_broker"))),
jellidation.Field(&r.Fields, jellidation.Each(validation.OneOf("guid", "name"))),
)
}

func ParseFields(values url.Values) []IncludeResourceRule {
includes := []IncludeResourceRule{}
fmt.Printf("values = %+v\n", values)
Expand Down
35 changes: 32 additions & 3 deletions api/payloads/service_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"
"slices"
"strconv"
"strings"

"code.cloudfoundry.org/korifi/api/payloads/params"
"code.cloudfoundry.org/korifi/api/payloads/parse"
Expand All @@ -28,10 +29,34 @@ type ServicePlanList struct {

func (l ServicePlanList) Validate() error {
return jellidation.ValidateStruct(&l,
jellidation.Field(&l.IncludeResourceRules),
jellidation.Field(&l.IncludeResourceRules, jellidation.Each(jellidation.By(func(value any) error {
rule, ok := value.(params.IncludeResourceRule)
if !ok {
return fmt.Errorf("%T is not supported, IncludeResourceRule is expected", value)
}

if len(rule.Fields) == 0 {
return validateInclude(rule)
}

return validateFields(rule)
}))),
)
}

func validateInclude(rule params.IncludeResourceRule) error {
return validation.OneOf("service_offering", "space.organization").
Validate(strings.Join(rule.RelationshipPath, "."))
}

func validateFields(rule params.IncludeResourceRule) error {
if strings.Join(rule.RelationshipPath, ".") != "service_offering.service_broker" {
return jellidation.NewError("invalid_fields_param", "must be fields[service_offering.service_broker]")
}

return jellidation.Each(validation.OneOf("guid", "name")).Validate(rule.Fields)
}

func (l *ServicePlanList) ToMessage() repositories.ListServicePlanMessage {
return repositories.ListServicePlanMessage{
ServiceOfferingGUIDs: parse.ArrayParam(l.ServiceOfferingGUIDs),
Expand All @@ -42,11 +67,15 @@ func (l *ServicePlanList) ToMessage() repositories.ListServicePlanMessage {
}

func (l *ServicePlanList) SupportedKeys() []string {
return []string{"service_offering_guids", "names", "available", "fields[service_offering.service_broker]", "service_broker_names", "page", "per_page", "include"}
return []string{"service_offering_guids", "names", "available", "fields[service_offering.service_broker]", "service_broker_names", "include"}
}

func (l *ServicePlanList) IgnoredKeys() []*regexp.Regexp {
return []*regexp.Regexp{regexp.MustCompile("space_guids")}
return []*regexp.Regexp{
regexp.MustCompile("space_guids"),
regexp.MustCompile("page"),
regexp.MustCompile("per_page"),
}
}

func (l *ServicePlanList) DecodeFromURLValues(values url.Values) error {
Expand Down
22 changes: 14 additions & 8 deletions api/payloads/service_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ var _ = Describe("ServicePlan", func() {
Entry("available", "available=true", payloads.ServicePlanList{Available: tools.PtrTo(true)}),
Entry("not available", "available=false", payloads.ServicePlanList{Available: tools.PtrTo(false)}),
Entry("broker names", "service_broker_names=b1,b2", payloads.ServicePlanList{BrokerNames: "b1,b2"}),
Entry("include service offering", "include=service_offering", payloads.ServicePlanList{
IncludeResourceRules: []params.IncludeResourceRule{{
RelationshipPath: []string{"service_offering"},
Fields: []string{},
}},
Entry("include", "include=service_offering&include=space.organization", payloads.ServicePlanList{
IncludeResourceRules: []params.IncludeResourceRule{
{
RelationshipPath: []string{"service_offering"},
Fields: []string{},
},
{
RelationshipPath: []string{"space", "organization"},
Fields: []string{},
},
},
}),
Entry("service broker fields", "fields[service_offering.service_broker]=guid,name", payloads.ServicePlanList{
IncludeResourceRules: []params.IncludeResourceRule{{
Expand All @@ -41,12 +47,12 @@ var _ = Describe("ServicePlan", func() {
)

DescribeTable("invalid query",
func(query string, errMatcher types.GomegaMatcher) {
func(query string, matchError types.GomegaMatcher) {
_, decodeErr := decodeQuery[payloads.ServicePlanList](query)
Expect(decodeErr).To(errMatcher)
Expect(decodeErr).To(matchError)
},
Entry("invalid available", "available=invalid", MatchError(ContainSubstring("failed to parse"))),
Entry("invalid include", "include=foo", MatchError(ContainSubstring("value must be one of: service_offering"))),
Entry("invalid include", "include=foo", MatchError(ContainSubstring("value must be one of"))),
Entry("invalid service broker fields", "fields[service_offering.service_broker]=foo", MatchError(ContainSubstring("value must be one of"))),
)

Expand Down
2 changes: 2 additions & 0 deletions api/repositories/relationships/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func (r *ResourceRelationshipsRepo) ListRelatedResources(ctx context.Context, au
authInfo,
repositories.ListServiceBrokerMessage{GUIDs: relatedResourceGUIDs},
))
case "space", "organization":
return []Resource{}, nil
}

return nil, fmt.Errorf("no repository for type %q", relatedResourceType)
Expand Down
22 changes: 22 additions & 0 deletions api/repositories/relationships/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,26 @@ var _ = Describe("ResourceRelationshipsRepository", func() {
})
})
})

Describe("resorce type space", func() {
BeforeEach(func() {
resourceType = "space"
})

It("returns a empty list", func() {
Expect(listError).NotTo(HaveOccurred())
Expect(result).To(BeEmpty())
})
})

Describe("resorce type space", func() {
BeforeEach(func() {
resourceType = "space"
})

It("returns a empty list", func() {
Expect(listError).NotTo(HaveOccurred())
Expect(result).To(BeEmpty())
})
})
})
Loading