diff --git a/api/payloads/params/include.go b/api/payloads/params/include.go index 65fbe0bc5..5899eae2d 100644 --- a/api/payloads/params/include.go +++ b/api/payloads/params/include.go @@ -4,9 +4,6 @@ import ( "fmt" "net/url" "strings" - - "code.cloudfoundry.org/korifi/api/payloads/validation" - jellidation "github.com/jellydator/validation" ) type IncludeResourceRule struct { @@ -14,13 +11,6 @@ type IncludeResourceRule struct { 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) diff --git a/api/payloads/service_plan.go b/api/payloads/service_plan.go index 8d2594108..a5ef1c4b3 100644 --- a/api/payloads/service_plan.go +++ b/api/payloads/service_plan.go @@ -6,6 +6,7 @@ import ( "regexp" "slices" "strconv" + "strings" "code.cloudfoundry.org/korifi/api/payloads/params" "code.cloudfoundry.org/korifi/api/payloads/parse" @@ -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), @@ -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 { diff --git a/api/payloads/service_plan_test.go b/api/payloads/service_plan_test.go index a20b179ef..b8573ed27 100644 --- a/api/payloads/service_plan_test.go +++ b/api/payloads/service_plan_test.go @@ -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{{ @@ -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"))), ) diff --git a/api/repositories/relationships/repository.go b/api/repositories/relationships/repository.go index e5ac1146d..95d9f3adf 100644 --- a/api/repositories/relationships/repository.go +++ b/api/repositories/relationships/repository.go @@ -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) diff --git a/api/repositories/relationships/repository_test.go b/api/repositories/relationships/repository_test.go index b76cf2b29..952b610d1 100644 --- a/api/repositories/relationships/repository_test.go +++ b/api/repositories/relationships/repository_test.go @@ -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()) + }) + }) })