Skip to content

Commit b6cb5ef

Browse files
committed
Merge 106155632-delete-service-broker to master
[Completes #106155632]
2 parents b590a18 + 5b5327c commit b6cb5ef

File tree

5 files changed

+201
-4
lines changed

5 files changed

+201
-4
lines changed

cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/serviceadmin/DefaultServiceAdmin.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
import org.cloudfoundry.client.v2.servicebrokers.ListServiceBrokersRequest;
2222
import org.cloudfoundry.client.v2.servicebrokers.ServiceBrokerEntity;
2323
import org.cloudfoundry.client.v2.servicebrokers.ServiceBrokerResource;
24+
import org.cloudfoundry.util.ExceptionUtils;
2425
import org.cloudfoundry.util.PaginationUtils;
2526
import org.cloudfoundry.util.ResourceUtils;
2627
import reactor.core.publisher.Flux;
2728
import reactor.core.publisher.Mono;
2829

30+
import java.util.NoSuchElementException;
2931
import java.util.Optional;
3032

3133
public final class DefaultServiceAdmin implements ServiceAdmin {
@@ -47,11 +49,28 @@ public Mono<Void> create(CreateServiceBrokerRequest request) {
4749
}
4850

4951
@Override
50-
public Flux<ServiceBroker> listServiceBrokers() {
52+
public Mono<Void> delete(DeleteServiceBrokerRequest request) {
53+
return getServiceBrokerId(this.cloudFoundryClient, request.getName())
54+
.then(serviceBrokerId -> requestDeleteServiceBroker(this.cloudFoundryClient, serviceBrokerId));
55+
}
56+
57+
@Override
58+
public Flux<ServiceBroker> list() {
5159
return requestServiceBrokers(this.cloudFoundryClient)
5260
.map(this::toServiceBroker);
5361
}
5462

63+
private static Mono<ServiceBrokerResource> getServiceBroker(CloudFoundryClient cloudFoundryClient, String serviceBrokerName) {
64+
return requestListServiceBrokers(cloudFoundryClient, serviceBrokerName)
65+
.single()
66+
.otherwise(NoSuchElementException.class, t -> ExceptionUtils.illegalArgument("Service Broker %s does not exist", serviceBrokerName));
67+
}
68+
69+
private static Mono<String> getServiceBrokerId(CloudFoundryClient cloudFoundryClient, String serviceBrokerName) {
70+
return getServiceBroker(cloudFoundryClient, serviceBrokerName)
71+
.map(ResourceUtils::getId);
72+
}
73+
5574
private static Mono<CreateServiceBrokerResponse> requestCreateServiceBroker(CloudFoundryClient cloudFoundryClient, String name, String url, String username, String password,
5675
Boolean isSpaceScoped, String spaceId) {
5776
return cloudFoundryClient.serviceBrokers()
@@ -64,6 +83,22 @@ private static Mono<CreateServiceBrokerResponse> requestCreateServiceBroker(Clou
6483
.build());
6584
}
6685

86+
private static Mono<Void> requestDeleteServiceBroker(CloudFoundryClient cloudFoundryClient, String serviceBrokerId) {
87+
return cloudFoundryClient.serviceBrokers()
88+
.delete(org.cloudfoundry.client.v2.servicebrokers.DeleteServiceBrokerRequest.builder()
89+
.serviceBrokerId(serviceBrokerId)
90+
.build());
91+
}
92+
93+
private static Flux<ServiceBrokerResource> requestListServiceBrokers(CloudFoundryClient cloudFoundryClient, String serviceBrokerName) {
94+
return PaginationUtils
95+
.requestResources(page -> cloudFoundryClient.serviceBrokers()
96+
.list(ListServiceBrokersRequest.builder()
97+
.name(serviceBrokerName)
98+
.page(page)
99+
.build()));
100+
}
101+
67102
private static Flux<ServiceBrokerResource> requestServiceBrokers(CloudFoundryClient cloudFoundryClient) {
68103
return PaginationUtils
69104
.requestResources(page -> cloudFoundryClient.serviceBrokers()

cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/serviceadmin/ServiceAdmin.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,19 @@ public interface ServiceAdmin {
3333
*/
3434
Mono<Void> create(CreateServiceBrokerRequest request);
3535

36+
/**
37+
* Deletes a service broker
38+
*
39+
* @param request the Delete Service Broker request
40+
* @return a completion indicator
41+
*/
42+
Mono<Void> delete(DeleteServiceBrokerRequest request);
43+
3644
/**
3745
* Lists the service brokers
3846
*
3947
* @return the service brokers
4048
*/
41-
Flux<ServiceBroker> listServiceBrokers();
49+
Flux<ServiceBroker> list();
50+
4251
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2013-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.cloudfoundry.operations.serviceadmin;
18+
19+
import org.immutables.value.Value;
20+
21+
/**
22+
* Request options for the delete service broker operation
23+
*/
24+
@Value.Immutable
25+
abstract class _DeleteServiceBrokerRequest {
26+
27+
/**
28+
* The name of the Service Broker
29+
*/
30+
abstract String getName();
31+
32+
}

cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/serviceadmin/DefaultServiceAdminTest.java

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ private static void requestCreateServiceBroker(CloudFoundryClient cloudFoundryCl
4747
.build()));
4848
}
4949

50+
private static void requestDeleteServiceBroker(CloudFoundryClient cloudFoundryClient, String serviceBrokerId) {
51+
when(cloudFoundryClient.serviceBrokers()
52+
.delete(org.cloudfoundry.client.v2.servicebrokers.DeleteServiceBrokerRequest.builder()
53+
.serviceBrokerId(serviceBrokerId)
54+
.build()))
55+
.thenReturn(Mono.empty());
56+
}
57+
5058
private static void requestListServiceBrokers(CloudFoundryClient cloudFoundryClient) {
5159
when(cloudFoundryClient.serviceBrokers()
5260
.list(ListServiceBrokersRequest.builder()
@@ -61,6 +69,21 @@ private static void requestListServiceBrokers(CloudFoundryClient cloudFoundryCli
6169
.build()));
6270
}
6371

72+
private static void requestListServiceBrokers(CloudFoundryClient cloudFoundryClient, String serviceBrokerName) {
73+
when(cloudFoundryClient.serviceBrokers()
74+
.list(ListServiceBrokersRequest.builder()
75+
.name(serviceBrokerName)
76+
.page(1)
77+
.build()))
78+
.thenReturn(Mono
79+
.just(fill(ListServiceBrokersResponse.builder())
80+
.resource(fill(ServiceBrokerResource.builder(), "service-broker-")
81+
.entity(fill(ServiceBrokerEntity.builder(), "service-broker-resource-")
82+
.build())
83+
.build())
84+
.build()));
85+
}
86+
6487
private static void requestListServiceBrokersEmpty(CloudFoundryClient cloudFoundryClient) {
6588
when(cloudFoundryClient.serviceBrokers()
6689
.list(ListServiceBrokersRequest.builder()
@@ -71,6 +94,17 @@ private static void requestListServiceBrokersEmpty(CloudFoundryClient cloudFound
7194
.build()));
7295
}
7396

97+
private static void requestListServiceBrokersEmpty(CloudFoundryClient cloudFoundryClient, String serviceBrokerName) {
98+
when(cloudFoundryClient.serviceBrokers()
99+
.list(ListServiceBrokersRequest.builder()
100+
.name(serviceBrokerName)
101+
.page(1)
102+
.build()))
103+
.thenReturn(Mono
104+
.just(fill(ListServiceBrokersResponse.builder())
105+
.build()));
106+
}
107+
74108
public static final class CreateServiceBroker extends AbstractOperationsApiTest<Void> {
75109

76110
private final DefaultServiceAdmin serviceAdmin = new DefaultServiceAdmin(this.cloudFoundryClient, Mono.just(TEST_SPACE_ID));
@@ -126,6 +160,57 @@ protected Mono<Void> invoke() {
126160

127161
}
128162

163+
public static final class DeleteServiceBroker extends AbstractOperationsApiTest<Void> {
164+
165+
private final DefaultServiceAdmin serviceAdmin = new DefaultServiceAdmin(this.cloudFoundryClient, Mono.just(TEST_SPACE_ID));
166+
167+
@Before
168+
public void setUp() throws Exception {
169+
requestListServiceBrokers(this.cloudFoundryClient, "test-service-broker-name");
170+
requestDeleteServiceBroker(this.cloudFoundryClient, "test-service-broker-id");
171+
}
172+
173+
@Override
174+
protected void assertions(TestSubscriber<Void> testSubscriber) {
175+
// Expects onComplete() with no onNext()
176+
}
177+
178+
@Override
179+
protected Mono<Void> invoke() {
180+
return this.serviceAdmin
181+
.delete(DeleteServiceBrokerRequest.builder()
182+
.name("test-service-broker-name")
183+
.build());
184+
}
185+
186+
}
187+
188+
public static final class DeleteServiceBrokerNoServiceBroker extends AbstractOperationsApiTest<Void> {
189+
190+
private final DefaultServiceAdmin serviceAdmin = new DefaultServiceAdmin(this.cloudFoundryClient, Mono.just(TEST_SPACE_ID));
191+
192+
@Before
193+
public void setUp() throws Exception {
194+
requestListServiceBrokersEmpty(this.cloudFoundryClient, "test-service-broker-name");
195+
requestDeleteServiceBroker(this.cloudFoundryClient, "test-service-broker-id");
196+
}
197+
198+
@Override
199+
protected void assertions(TestSubscriber<Void> testSubscriber) {
200+
testSubscriber
201+
.assertError(IllegalArgumentException.class, String.format("Service Broker %s does not exist", "test-service-broker-name"));
202+
}
203+
204+
@Override
205+
protected Mono<Void> invoke() {
206+
return this.serviceAdmin
207+
.delete(DeleteServiceBrokerRequest.builder()
208+
.name("test-service-broker-name")
209+
.build());
210+
}
211+
212+
}
213+
129214
public static final class ListServiceBrokers extends AbstractOperationsApiTest<ServiceBroker> {
130215

131216
private final DefaultServiceAdmin serviceAdmin = new DefaultServiceAdmin(this.cloudFoundryClient, Mono.just(TEST_SPACE_ID));
@@ -148,7 +233,7 @@ protected void assertions(TestSubscriber<ServiceBroker> testSubscriber) {
148233
@Override
149234
protected Publisher<ServiceBroker> invoke() {
150235
return this.serviceAdmin
151-
.listServiceBrokers();
236+
.list();
152237
}
153238

154239
}
@@ -170,7 +255,7 @@ protected void assertions(TestSubscriber<ServiceBroker> testSubscriber) {
170255
@Override
171256
protected Publisher<ServiceBroker> invoke() {
172257
return this.serviceAdmin
173-
.listServiceBrokers();
258+
.list();
174259
}
175260

176261
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2013-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.cloudfoundry.operations.serviceadmin;
18+
19+
import org.junit.Test;
20+
21+
public class DeleteServiceBrokerRequestTest {
22+
23+
@Test(expected = IllegalStateException.class)
24+
public void noName() {
25+
DeleteServiceBrokerRequest.builder()
26+
.build();
27+
}
28+
29+
@Test
30+
public void valid() {
31+
DeleteServiceBrokerRequest.builder()
32+
.name("test-service-broker")
33+
.build();
34+
}
35+
36+
}

0 commit comments

Comments
 (0)