Skip to content

Commit

Permalink
Support listing service instances by guid
Browse files Browse the repository at this point in the history
Co-authored-by: Danail Branekov <danailster@gmail.com>
Co-authored-by: Kieron Browne <kbrowne@vmware.com>
  • Loading branch information
2 people authored and georgethebeatle committed Jul 20, 2023
1 parent 32dcdc9 commit f7895e6
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 15 deletions.
8 changes: 5 additions & 3 deletions api/handlers/service_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ var _ = Describe("ServiceInstance", func() {
_, actualAuthInfo, actualListMessage := serviceInstanceRepo.ListServiceInstancesArgsForCall(0)
Expect(actualAuthInfo).To(Equal(authInfo))
Expect(actualListMessage.Names).To(BeEmpty())
Expect(actualListMessage.SpaceGuids).To(BeEmpty())
Expect(actualListMessage.SpaceGUIDs).To(BeEmpty())

Expect(rr).Should(HaveHTTPStatus(http.StatusOK))
Expect(rr).To(HaveHTTPHeaderWithValue("Content-Type", "application/json"))
Expand All @@ -186,7 +186,8 @@ var _ = Describe("ServiceInstance", func() {
BeforeEach(func() {
requestValidator.DecodeAndValidateURLValuesStub = decodeAndValidateURLValuesStub(&payloads.ServiceInstanceList{
Names: "sc1,sc2",
SpaceGuids: "space1,space2",
SpaceGUIDs: "space1,space2",
GUIDs: "g1,g2",
})
})

Expand All @@ -195,7 +196,8 @@ var _ = Describe("ServiceInstance", func() {
_, _, message := serviceInstanceRepo.ListServiceInstancesArgsForCall(0)

Expect(message.Names).To(ConsistOf("sc1", "sc2"))
Expect(message.SpaceGuids).To(ConsistOf("space1", "space2"))
Expect(message.SpaceGUIDs).To(ConsistOf("space1", "space2"))
Expect(message.GUIDs).To(ConsistOf("g1", "g2"))
})

It("correctly sets query parameters in response pagination links", func() {
Expand Down
11 changes: 7 additions & 4 deletions api/payloads/service_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ func (p *ServiceInstancePatch) UnmarshalJSON(data []byte) error {

type ServiceInstanceList struct {
Names string
SpaceGuids string
GUIDs string
SpaceGUIDs string
OrderBy string
}

Expand All @@ -143,12 +144,13 @@ func (l ServiceInstanceList) Validate() error {
func (l *ServiceInstanceList) ToMessage() repositories.ListServiceInstanceMessage {
return repositories.ListServiceInstanceMessage{
Names: parse.ArrayParam(l.Names),
SpaceGuids: parse.ArrayParam(l.SpaceGuids),
SpaceGUIDs: parse.ArrayParam(l.SpaceGUIDs),
GUIDs: parse.ArrayParam(l.GUIDs),
}
}

func (l *ServiceInstanceList) SupportedKeys() []string {
return []string{"names", "space_guids", "order_by", "per_page", "page"}
return []string{"names", "space_guids", "guids", "order_by", "per_page", "page"}
}

func (l *ServiceInstanceList) IgnoredKeys() []*regexp.Regexp {
Expand All @@ -157,7 +159,8 @@ func (l *ServiceInstanceList) IgnoredKeys() []*regexp.Regexp {

func (l *ServiceInstanceList) DecodeFromURLValues(values url.Values) error {
l.Names = values.Get("names")
l.SpaceGuids = values.Get("space_guids")
l.SpaceGUIDs = values.Get("space_guids")
l.GUIDs = values.Get("guids")
l.OrderBy = values.Get("order_by")
return nil
}
3 changes: 2 additions & 1 deletion api/payloads/service_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ var _ = Describe("ServiceInstanceList", func() {
Expect(*actualServiceInstanceList).To(Equal(expectedServiceInstanceList))
},
Entry("names", "names=name", payloads.ServiceInstanceList{Names: "name"}),
Entry("space_guids", "space_guids=space_guid", payloads.ServiceInstanceList{SpaceGuids: "space_guid"}),
Entry("space_guids", "space_guids=space_guid", payloads.ServiceInstanceList{SpaceGUIDs: "space_guid"}),
Entry("guids", "guids=guid", payloads.ServiceInstanceList{GUIDs: "guid"}),
Entry("created_at", "order_by=created_at", payloads.ServiceInstanceList{OrderBy: "created_at"}),
Entry("-created_at", "order_by=-created_at", payloads.ServiceInstanceList{OrderBy: "-created_at"}),
Entry("updated_at", "order_by=updated_at", payloads.ServiceInstanceList{OrderBy: "updated_at"}),
Expand Down
6 changes: 4 additions & 2 deletions api/repositories/service_instance_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ func (p PatchServiceInstanceMessage) Apply(cfServiceInstance *korifiv1alpha1.CFS

type ListServiceInstanceMessage struct {
Names []string
SpaceGuids []string
SpaceGUIDs []string
GUIDs []string
}

type DeleteServiceInstanceMessage struct {
Expand Down Expand Up @@ -198,9 +199,10 @@ func (r *ServiceInstanceRepo) ListServiceInstances(ctx context.Context, authInfo

preds := []func(korifiv1alpha1.CFServiceInstance) bool{
SetPredicate(message.Names, func(s korifiv1alpha1.CFServiceInstance) string { return s.Spec.DisplayName }),
SetPredicate(message.GUIDs, func(s korifiv1alpha1.CFServiceInstance) string { return s.Name }),
}

spaceGUIDSet := NewSet(message.SpaceGuids...)
spaceGUIDSet := NewSet(message.SpaceGUIDs...)
var filteredServiceInstances []korifiv1alpha1.CFServiceInstance
for ns := range nsList {
if len(spaceGUIDSet) > 0 && !spaceGUIDSet.Includes(ns) {
Expand Down
16 changes: 15 additions & 1 deletion api/repositories/service_instance_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ var _ = Describe("ServiceInstanceRepository", func() {
When("the spaceGUID filter is set", func() {
BeforeEach(func() {
filters = repositories.ListServiceInstanceMessage{
SpaceGuids: []string{
SpaceGUIDs: []string{
cfServiceInstance2.Namespace,
cfServiceInstance3.Namespace,
},
Expand All @@ -447,6 +447,20 @@ var _ = Describe("ServiceInstanceRepository", func() {
))
})
})

When("the serviceGUID filter is set", func() {
BeforeEach(func() {
filters = repositories.ListServiceInstanceMessage{
GUIDs: []string{cfServiceInstance1.Name, cfServiceInstance3.Name},
}
})
It("returns only records for the ServiceInstances within the matching spaces", func() {
Expect(serviceInstanceList).To(ConsistOf(
MatchFields(IgnoreExtras, Fields{"GUID": Equal(cfServiceInstance1.Name)}),
MatchFields(IgnoreExtras, Fields{"GUID": Equal(cfServiceInstance3.Name)}),
))
})
})
})
})

Expand Down
56 changes: 52 additions & 4 deletions tests/e2e/service_instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,9 @@ var _ = Describe("Service Instances", func() {
createSpaceRole("space_developer", certUserName, spaceGUID)
})

It("succeeds", func() {
It("deletes the service instance", func() {
Expect(httpError).NotTo(HaveOccurred())
Expect(httpResp).To(HaveRestyStatusCode(http.StatusNoContent))
})

It("deletes the service instance", func() {
Expect(listServiceInstances().Resources).NotTo(ContainElement(
MatchFields(IgnoreExtras, Fields{
"Name": Equal(existingInstanceName),
Expand All @@ -180,4 +177,55 @@ var _ = Describe("Service Instances", func() {
})
})
})

Describe("List", func() {
var (
anotherSpaceGUID string
anotherInstanceGUID string
serviceInstancesList resourceList[resource]
)

BeforeEach(func() {
anotherSpaceGUID = createSpace(generateGUID("space1"), commonTestOrgGUID)
anotherInstanceGUID = createServiceInstance(anotherSpaceGUID, generateGUID("service-instance"), nil)
})

JustBeforeEach(func() {
serviceInstancesList = resourceList[resource]{}
httpResp, httpError = certClient.R().SetResult(&serviceInstancesList).Get("/v3/service_instances")
})

It("does not return service instances in spaces the user is not permitted", func() {
Expect(httpError).NotTo(HaveOccurred())
Expect(httpResp).To(HaveRestyStatusCode(http.StatusOK))
Expect(serviceInstancesList.Resources).NotTo(ContainElements(
MatchFields(IgnoreExtras, Fields{
"GUID": Equal(existingInstanceGUID),
}),
MatchFields(IgnoreExtras, Fields{
"GUID": Equal(anotherInstanceGUID),
}),
))
})

When("the user has permissions to list service instances", func() {
BeforeEach(func() {
createSpaceRole("space_developer", certUserName, spaceGUID)
createSpaceRole("space_developer", certUserName, anotherSpaceGUID)
})

It("lists the service instances", func() {
Expect(httpError).NotTo(HaveOccurred())
Expect(httpResp).To(HaveRestyStatusCode(http.StatusOK))
Expect(serviceInstancesList.Resources).To(ContainElements(
MatchFields(IgnoreExtras, Fields{
"GUID": Equal(existingInstanceGUID),
}),
MatchFields(IgnoreExtras, Fields{
"GUID": Equal(anotherInstanceGUID),
}),
))
})
})
})
})

0 comments on commit f7895e6

Please sign in to comment.