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

Add "guids" as a valid package list filter #3142

Merged
merged 1 commit into from
Feb 24, 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
5 changes: 3 additions & 2 deletions api/handlers/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ import (
"code.cloudfoundry.org/korifi/api/handlers/fake"
"code.cloudfoundry.org/korifi/api/payloads"
"code.cloudfoundry.org/korifi/api/repositories"
. "code.cloudfoundry.org/korifi/tests/matchers"
"code.cloudfoundry.org/korifi/tools"

. "code.cloudfoundry.org/korifi/tests/matchers"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"k8s.io/apimachinery/pkg/types"
)

Expand Down Expand Up @@ -192,6 +191,7 @@ var _ = Describe("Package", func() {
It("calls the package repository with expected arguments", func() {
_, _, message := packageRepo.ListPackagesArgsForCall(0)
Expect(message).To(Equal(repositories.ListPackagesMessage{
GUIDs: []string{},
AppGUIDs: []string{appGUID},
States: []string{},
}))
Expand Down Expand Up @@ -252,6 +252,7 @@ var _ = Describe("Package", func() {
It("calls repository ListPackage with the correct message object", func() {
_, _, message := packageRepo.ListPackagesArgsForCall(0)
Expect(message).To(Equal(repositories.ListPackagesMessage{
GUIDs: []string{},
AppGUIDs: []string{},
States: []string{"READY", "AWAITING_UPLOAD"},
}))
Expand Down
6 changes: 5 additions & 1 deletion api/payloads/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"code.cloudfoundry.org/korifi/api/payloads/parse"
"code.cloudfoundry.org/korifi/api/payloads/validation"
"code.cloudfoundry.org/korifi/api/repositories"

jellidation "github.com/jellydator/validation"
)

Expand Down Expand Up @@ -89,23 +90,26 @@ func (u *PackageUpdate) ToMessage(packageGUID string) repositories.UpdatePackage
}

type PackageList struct {
GUIDs string
AppGUIDs string
States string
OrderBy string
}

func (p *PackageList) ToMessage() repositories.ListPackagesMessage {
return repositories.ListPackagesMessage{
GUIDs: parse.ArrayParam(p.GUIDs),
AppGUIDs: parse.ArrayParam(p.AppGUIDs),
States: parse.ArrayParam(p.States),
}
}

func (p *PackageList) SupportedKeys() []string {
return []string{"app_guids", "states", "order_by", "per_page", "page"}
return []string{"guids", "app_guids", "states", "order_by", "per_page", "page"}
}

func (p *PackageList) DecodeFromURLValues(values url.Values) error {
p.GUIDs = values.Get("guids")
p.AppGUIDs = values.Get("app_guids")
p.States = values.Get("states")
p.OrderBy = values.Get("order_by")
Expand Down
22 changes: 18 additions & 4 deletions api/payloads/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"code.cloudfoundry.org/korifi/api/payloads"
"code.cloudfoundry.org/korifi/api/repositories"
"code.cloudfoundry.org/korifi/tools"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gstruct"
Expand Down Expand Up @@ -296,13 +297,14 @@ var _ = Describe("PackageUpdate", func() {

var _ = Describe("PackageList", func() {
DescribeTable("valid query",
func(query string, expectedPackageListQueryParameters payloads.PackageList) {
actualPackageListQueryParameters, decodeErr := decodeQuery[payloads.PackageList](query)
func(query string, expectedPackageList payloads.PackageList) {
actualPackageList, decodeErr := decodeQuery[payloads.PackageList](query)

Expect(decodeErr).NotTo(HaveOccurred())
Expect(*actualPackageListQueryParameters).To(Equal(expectedPackageListQueryParameters))
Expect(*actualPackageList).To(Equal(expectedPackageList))
},
Entry("app_guids", "app_guids=g1,g2", payloads.PackageList{AppGUIDs: "g1,g2"}),
Entry("guids", "guids=g1,g2", payloads.PackageList{GUIDs: "g1,g2"}),
Entry("app_guids", "app_guids=ag1,ag2", payloads.PackageList{AppGUIDs: "ag1,ag2"}),
Entry("states", "states=s1,s2", payloads.PackageList{States: "s1,s2"}),
Entry("created_at", "order_by=created_at", payloads.PackageList{OrderBy: "created_at"}),
Entry("-created_at", "order_by=-created_at", payloads.PackageList{OrderBy: "-created_at"}),
Expand All @@ -311,6 +313,18 @@ var _ = Describe("PackageList", func() {
Entry("empty", "order_by=", payloads.PackageList{OrderBy: ""}),
)

DescribeTable("ToMessage",
func(packageList payloads.PackageList, expectedListPackagesMessage repositories.ListPackagesMessage) {
actualListPackagesMessage := packageList.ToMessage()

Expect(actualListPackagesMessage).To(Equal(expectedListPackagesMessage))
},
Entry("guids", payloads.PackageList{GUIDs: "g1,g2", AppGUIDs: "", States: ""}, repositories.ListPackagesMessage{GUIDs: []string{"g1", "g2"}, AppGUIDs: []string{}, States: []string{}}),
Entry("app_guids", payloads.PackageList{GUIDs: "", AppGUIDs: "ag1,ag2", States: ""}, repositories.ListPackagesMessage{GUIDs: []string{}, AppGUIDs: []string{"ag1", "ag2"}, States: []string{}}),
Entry("states", payloads.PackageList{GUIDs: "", AppGUIDs: "", States: "s1,s2"}, repositories.ListPackagesMessage{GUIDs: []string{}, AppGUIDs: []string{}, States: []string{"s1", "s2"}}),
Entry("empty", payloads.PackageList{}, repositories.ListPackagesMessage{GUIDs: []string{}, AppGUIDs: []string{}, States: []string{}}),
)

DescribeTable("invalid query",
func(query string, expectedErrMsg string) {
_, decodeErr := decodeQuery[payloads.PackageList](query)
Expand Down
4 changes: 3 additions & 1 deletion api/repositories/package_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"code.cloudfoundry.org/korifi/controllers/controllers/workloads"
"code.cloudfoundry.org/korifi/tools/dockercfg"
"code.cloudfoundry.org/korifi/tools/k8s"

"github.com/google/go-containerregistry/pkg/name"
"github.com/google/uuid"

corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -81,6 +81,7 @@ type PackageRecord struct {
}

type ListPackagesMessage struct {
GUIDs []string
AppGUIDs []string
States []string
}
Expand Down Expand Up @@ -298,6 +299,7 @@ func (r *PackageRepo) ListPackages(ctx context.Context, authInfo authorization.I
}

preds := []func(korifiv1alpha1.CFPackage) bool{
SetPredicate(message.GUIDs, func(s korifiv1alpha1.CFPackage) string { return s.Name }),
SetPredicate(message.AppGUIDs, func(s korifiv1alpha1.CFPackage) string { return s.Spec.AppRef.Name }),
}
if len(message.States) > 0 {
Expand Down
21 changes: 18 additions & 3 deletions api/repositories/package_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"code.cloudfoundry.org/korifi/tests/matchers"
"code.cloudfoundry.org/korifi/tools"
"code.cloudfoundry.org/korifi/tools/k8s"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
Expand All @@ -23,6 +22,7 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("PackageRepository", func() {
Expand Down Expand Up @@ -553,7 +553,22 @@ var _ = Describe("PackageRepository", func() {
))
})

When("app_guids filter is provided", func() {
When("the guids filter is provided", func() {
BeforeEach(func() {
listMessage = repositories.ListPackagesMessage{GUIDs: []string{package1GUID}}
})

It("fetches the specified package", func() {
Expect(packageList).To(ConsistOf(
MatchFields(IgnoreExtras, Fields{
"GUID": Equal(package1GUID),
"AppGUID": Equal(appGUID),
}),
))
})
})

When("the app_guids filter is provided", func() {
BeforeEach(func() {
listMessage = repositories.ListPackagesMessage{AppGUIDs: []string{appGUID}}
})
Expand All @@ -569,7 +584,7 @@ var _ = Describe("PackageRepository", func() {
})
})

When("State filter is provided", func() {
When("the state filter is provided", func() {
When("filtering by State=READY", func() {
BeforeEach(func() {
listMessage = repositories.ListPackagesMessage{States: []string{"READY"}}
Expand Down
Loading