Skip to content

Commit b7255e7

Browse files
uzabanovgeorgethebeatle
authored andcommitted
Filter service bindings by display name
`cf curl /v3/service_credential_bindings?names=b1,b3`, The names querry params is now supported. This also allows to use `cf service-key` command fixes #4149
1 parent 8fd122b commit b7255e7

File tree

9 files changed

+82
-2
lines changed

9 files changed

+82
-2
lines changed

api/payloads/service_binding.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func (r ServiceBindingRelationships) Validate() error {
7171
}
7272

7373
type ServiceBindingList struct {
74+
Names string
7475
Type string
7576
AppGUIDs string
7677
ServiceInstanceGUIDs string
@@ -92,6 +93,7 @@ func (l ServiceBindingList) Validate() error {
9293

9394
func (l *ServiceBindingList) ToMessage() repositories.ListServiceBindingsMessage {
9495
return repositories.ListServiceBindingsMessage{
96+
Names: parse.ArrayParam(l.Names),
9597
ServiceInstanceGUIDs: parse.ArrayParam(l.ServiceInstanceGUIDs),
9698
AppGUIDs: parse.ArrayParam(l.AppGUIDs),
9799
LabelSelector: l.LabelSelector,
@@ -103,11 +105,12 @@ func (l *ServiceBindingList) ToMessage() repositories.ListServiceBindingsMessage
103105
}
104106

105107
func (l *ServiceBindingList) SupportedKeys() []string {
106-
return []string{"app_guids", "service_instance_guids", "include", "type", "order_by", "per_page", "page", "label_selector", "service_plan_guids"}
108+
return []string{"names", "app_guids", "service_instance_guids", "include", "type", "order_by", "per_page", "page", "label_selector", "service_plan_guids"}
107109
}
108110

109111
func (l *ServiceBindingList) DecodeFromURLValues(values url.Values) error {
110112
l.Type = values.Get("type")
113+
l.Names = values.Get("names")
111114
l.AppGUIDs = values.Get("app_guids")
112115
l.ServiceInstanceGUIDs = values.Get("service_instance_guids")
113116
l.Include = values.Get("include")

api/payloads/service_binding_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var _ = Describe("ServiceBindingList", func() {
2323
},
2424
Entry("type", "type=key", payloads.ServiceBindingList{Type: korifiv1alpha1.CFServiceBindingTypeKey}),
2525
Entry("type", "type=app", payloads.ServiceBindingList{Type: korifiv1alpha1.CFServiceBindingTypeApp}),
26+
Entry("names", "names=name1,name2", payloads.ServiceBindingList{Names: "name1,name2"}),
2627
Entry("app_guids", "app_guids=app_guid", payloads.ServiceBindingList{AppGUIDs: "app_guid"}),
2728
Entry("service_instance_guids", "service_instance_guids=si_guid", payloads.ServiceBindingList{ServiceInstanceGUIDs: "si_guid"}),
2829
Entry("include", "include=app", payloads.ServiceBindingList{Include: "app"}),
@@ -58,6 +59,7 @@ var _ = Describe("ServiceBindingList", func() {
5859
BeforeEach(func() {
5960
payload = payloads.ServiceBindingList{
6061
Type: korifiv1alpha1.CFServiceBindingTypeApp,
62+
Names: "binding1,binding2",
6163
AppGUIDs: "app1,app2",
6264
ServiceInstanceGUIDs: "s1,s2",
6365
Include: "include",
@@ -79,6 +81,7 @@ var _ = Describe("ServiceBindingList", func() {
7981
Expect(message).To(Equal(repositories.ListServiceBindingsMessage{
8082
Type: korifiv1alpha1.CFServiceBindingTypeApp,
8183
AppGUIDs: []string{"app1", "app2"},
84+
Names: []string{"binding1", "binding2"},
8285
ServiceInstanceGUIDs: []string{"s1", "s2"},
8386
LabelSelector: "foo=bar",
8487
PlanGUIDs: []string{"p1", "p2"},

api/repositories/service_binding_repository.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ type DeleteServiceBindingMessage struct {
102102
}
103103

104104
type ListServiceBindingsMessage struct {
105+
Names []string
105106
AppGUIDs []string
106107
ServiceInstanceGUIDs []string
107108
LabelSelector string
@@ -114,6 +115,7 @@ type ListServiceBindingsMessage struct {
114115
func (m *ListServiceBindingsMessage) toListOptions() []ListOption {
115116
opts := []ListOption{
116117
WithLabelSelector(m.LabelSelector),
118+
WithLabelIn(korifiv1alpha1.DisplayNameLabelKey, tools.EncodeValuesToSha224(m.Names...)),
117119
WithLabelIn(korifiv1alpha1.CFServiceInstanceGUIDLabelKey, m.ServiceInstanceGUIDs),
118120
WithLabelIn(korifiv1alpha1.CFAppGUIDLabelKey, m.AppGUIDs),
119121
WithLabelIn(korifiv1alpha1.PlanGUIDLabelKey, m.PlanGUIDs),

api/repositories/service_binding_repository_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,7 @@ var _ = Describe("ServiceBindingRepo", func() {
999999
fakeKlient = new(fake.Klient)
10001000
repo = repositories.NewServiceBindingRepo(fakeKlient, nil, nil, nil)
10011001
requestMessage = repositories.ListServiceBindingsMessage{
1002+
Names: []string{"b1", "b2"},
10021003
AppGUIDs: []string{"a1", "a2"},
10031004
ServiceInstanceGUIDs: []string{"s1", "s2"},
10041005
LabelSelector: "foo=bar",
@@ -1019,6 +1020,7 @@ var _ = Describe("ServiceBindingRepo", func() {
10191020
repositories.WithLabelSelector("foo=bar"),
10201021
repositories.WithLabelIn(korifiv1alpha1.PlanGUIDLabelKey, []string{"p1", "p2"}),
10211022
repositories.WithLabelIn(korifiv1alpha1.CFAppGUIDLabelKey, []string{"a1", "a2"}),
1023+
repositories.WithLabelIn(korifiv1alpha1.DisplayNameLabelKey, tools.EncodeValuesToSha224("b1", "b2")),
10221024
repositories.WithLabelIn(korifiv1alpha1.CFServiceInstanceGUIDLabelKey, []string{"s1", "s2"}),
10231025
repositories.WithOrdering("created_at"),
10241026
repositories.WithPaging(repositories.Pagination{

controllers/webhooks/label_indexer/webhook.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func NewWebhook() *LabelIndexerWebhook {
8282
},
8383
"CFServiceBinding": {
8484
LabelRule{Label: korifiv1alpha1.SpaceGUIDLabelKey, IndexingFunc: Unquote(JSONValue("$.metadata.namespace"))},
85+
LabelRule{Label: korifiv1alpha1.DisplayNameLabelKey, IndexingFunc: SHA224(Unquote(JSONValue("$.spec.displayName")))},
8586
LabelRule{Label: korifiv1alpha1.CFServiceInstanceGUIDLabelKey, IndexingFunc: Unquote(JSONValue("$.spec.service.name"))},
8687
LabelRule{Label: korifiv1alpha1.CFAppGUIDLabelKey, IndexingFunc: Unquote(JSONValue("$.spec.appRef.name"))},
8788
LabelRule{Label: korifiv1alpha1.CFServiceBindingTypeLabelKey, IndexingFunc: Unquote(JSONValue("$.spec.type"))},

controllers/webhooks/label_indexer/webhook_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package label_indexer_test
22

33
import (
44
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
5+
"code.cloudfoundry.org/korifi/tools"
56
"code.cloudfoundry.org/korifi/tools/k8s"
67
"github.com/google/uuid"
78
. "github.com/onsi/ginkgo/v2"
@@ -284,7 +285,8 @@ var _ = Describe("LabelIndexerWebhook", func() {
284285
Namespace: namespace,
285286
},
286287
Spec: korifiv1alpha1.CFServiceBindingSpec{
287-
Type: "app",
288+
Type: "app",
289+
DisplayName: tools.PtrTo("my-awesome-binding"),
288290
Service: corev1.ObjectReference{
289291
Name: uuid.NewString(),
290292
},
@@ -304,6 +306,7 @@ var _ = Describe("LabelIndexerWebhook", func() {
304306
g.Expect(adminClient.Get(ctx, client.ObjectKeyFromObject(binding), binding)).To(Succeed())
305307
g.Expect(binding.Labels).To(MatchKeys(IgnoreExtras, Keys{
306308
korifiv1alpha1.SpaceGUIDLabelKey: Equal(binding.Namespace),
309+
korifiv1alpha1.DisplayNameLabelKey: Equal("473e539de6d59aeed16a5e053c3e1180d10f4dd63da6bd7fdba3d2c9"), // SHA224 hash of "my-awesome-binding"
307310
korifiv1alpha1.CFServiceInstanceGUIDLabelKey: Equal(binding.Spec.Service.Name),
308311
korifiv1alpha1.CFAppGUIDLabelKey: Equal(binding.Spec.AppRef.Name),
309312
korifiv1alpha1.CFServiceBindingTypeLabelKey: Equal(binding.Spec.Type),

migration/migration/executor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const MigratedByLabelKey = "korifi.cloudfoundry.org/migrated-by"
1818

1919
var korifiObjectLists = []client.ObjectList{
2020
&korifiv1alpha1.CFRouteList{},
21+
&korifiv1alpha1.CFServiceBindingList{},
2122
}
2223

2324
type Migrator struct {

migration/migration/executor_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ var _ = Describe("Executor", func() {
2020
},
2121
},
2222
}))
23+
24+
Describe("CFServiceBinding", test(&korifiv1alpha1.CFServiceBinding{
25+
Spec: korifiv1alpha1.CFServiceBindingSpec{
26+
Type: "key",
27+
},
28+
}))
2329
})
2430

2531
func test(testObj client.Object) func() {

tests/smoke/service_keys_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package smoke_test
2+
3+
import (
4+
"code.cloudfoundry.org/korifi/tests/helpers"
5+
"code.cloudfoundry.org/korifi/tests/helpers/broker"
6+
7+
"github.com/BooleanCat/go-functional/v2/it"
8+
"github.com/google/uuid"
9+
. "github.com/onsi/ginkgo/v2"
10+
. "github.com/onsi/gomega"
11+
. "github.com/onsi/gomega/gexec"
12+
)
13+
14+
var _ = Describe("cf service-key", func() {
15+
var (
16+
serviceName string
17+
serviceKeysSession *Session
18+
serviceKeyName string
19+
)
20+
21+
BeforeEach(func() {
22+
serviceKeyName = uuid.NewString()
23+
appName := uuid.NewString()
24+
Expect(helpers.Cf("create-app", appName)).To(Exit(0))
25+
26+
serviceName = uuid.NewString()
27+
brokerName := uuid.NewString()
28+
Expect(helpers.Cf(
29+
"create-service-broker",
30+
brokerName,
31+
"broker-user",
32+
"broker-password",
33+
sharedData.BrokerURL,
34+
)).To(Exit(0))
35+
DeferCleanup(func() {
36+
broker.NewDeleter(sharedData.RootNamespace).ForBrokerName(brokerName).Delete()
37+
})
38+
39+
Expect(helpers.Cf("enable-service-access", "sample-service", "-b", brokerName)).To(Exit(0))
40+
serviceKeysSession = helpers.Cf("create-service", "sample-service", "sample", serviceName, "-b", brokerName)
41+
Expect(serviceKeysSession).To(Exit(0))
42+
})
43+
44+
JustBeforeEach(func() {
45+
serviceKeysSession = helpers.Cf("create-service-key", serviceName, serviceKeyName, "--wait")
46+
Expect(serviceKeysSession).To(Exit(0))
47+
})
48+
49+
It("creates a service key", func() {
50+
serviceKeysSession = helpers.Cf("service-key", serviceName, serviceKeyName)
51+
Expect(serviceKeysSession).To(Exit(0))
52+
lines := it.MustCollect(it.LinesString(serviceKeysSession.Out))
53+
Expect(lines).To(ContainElements(
54+
ContainSubstring(`"credentials"`),
55+
ContainSubstring(`"username"`),
56+
ContainSubstring(`"password"`),
57+
))
58+
})
59+
})

0 commit comments

Comments
 (0)