Skip to content

Commit 92fa5db

Browse files
adding create security group impl and tests
1 parent b5aea46 commit 92fa5db

File tree

7 files changed

+302
-9
lines changed

7 files changed

+302
-9
lines changed

cloudfoundry-client-reactor/src/main/java/org/cloudfoundry/reactor/client/_ReactorCloudFoundryClient.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.cloudfoundry.client.v2.routemappings.RouteMappings;
3535
import org.cloudfoundry.client.v2.routes.Routes;
3636
import org.cloudfoundry.client.v2.securitygroups.SecurityGroups;
37+
import org.cloudfoundry.client.v3.securitygroups.SecurityGroupsV3;
3738
import org.cloudfoundry.client.v2.servicebindings.ServiceBindingsV2;
3839
import org.cloudfoundry.client.v2.servicebrokers.ServiceBrokers;
3940
import org.cloudfoundry.client.v2.serviceinstances.ServiceInstances;
@@ -91,6 +92,7 @@
9192
import org.cloudfoundry.reactor.client.v2.routemappings.ReactorRouteMappings;
9293
import org.cloudfoundry.reactor.client.v2.routes.ReactorRoutes;
9394
import org.cloudfoundry.reactor.client.v2.securitygroups.ReactorSecurityGroups;
95+
import org.cloudfoundry.reactor.client.v3.securitygroups.ReactorSecurityGroupsV3;
9496
import org.cloudfoundry.reactor.client.v2.servicebindings.ReactorServiceBindingsV2;
9597
import org.cloudfoundry.reactor.client.v2.servicebrokers.ReactorServiceBrokers;
9698
import org.cloudfoundry.reactor.client.v2.serviceinstances.ReactorServiceInstances;
@@ -151,7 +153,8 @@ public AdminV3 adminV3() {
151153
@Override
152154
@Value.Derived
153155
public ApplicationUsageEvents applicationUsageEvents() {
154-
return new ReactorApplicationUsageEvents(getConnectionContext(), getRootV2(), getTokenProvider(), getRequestTags());
156+
return new ReactorApplicationUsageEvents(getConnectionContext(), getRootV2(), getTokenProvider(),
157+
getRequestTags());
155158
}
156159

157160
@Override
@@ -228,7 +231,8 @@ public Droplets droplets() {
228231
@Override
229232
@Value.Derived
230233
public EnvironmentVariableGroups environmentVariableGroups() {
231-
return new ReactorEnvironmentVariableGroups(getConnectionContext(), getRootV2(), getTokenProvider(), getRequestTags());
234+
return new ReactorEnvironmentVariableGroups(getConnectionContext(), getRootV2(), getTokenProvider(),
235+
getRequestTags());
232236
}
233237

234238
@Override
@@ -270,7 +274,8 @@ public JobsV3 jobsV3() {
270274
@Override
271275
@Value.Derived
272276
public OrganizationQuotaDefinitions organizationQuotaDefinitions() {
273-
return new ReactorOrganizationQuotaDefinitions(getConnectionContext(), getRootV2(), getTokenProvider(), getRequestTags());
277+
return new ReactorOrganizationQuotaDefinitions(getConnectionContext(), getRootV2(), getTokenProvider(),
278+
getRequestTags());
274279
}
275280

276281
@Override
@@ -345,6 +350,12 @@ public SecurityGroups securityGroups() {
345350
return new ReactorSecurityGroups(getConnectionContext(), getRootV2(), getTokenProvider(), getRequestTags());
346351
}
347352

353+
@Override
354+
@Value.Derived
355+
public SecurityGroupsV3 securityGroupsV3() {
356+
return new ReactorSecurityGroupsV3(getConnectionContext(), getRootV2(), getTokenProvider(), getRequestTags());
357+
}
358+
348359
@Override
349360
@Value.Derived
350361
public ServiceBindingsV2 serviceBindingsV2() {
@@ -366,7 +377,7 @@ public ServiceBrokers serviceBrokers() {
366377
@Override
367378
@Value.Derived
368379
public ServiceBrokersV3 serviceBrokersV3() {
369-
return new ReactorServiceBrokersV3(getConnectionContext(), getRootV3(), getTokenProvider(), getRequestTags());
380+
return new ReactorServiceBrokersV3(getConnectionContext(), getRootV3(), getTokenProvider(), getRequestTags());
370381
}
371382

372383
@Override
@@ -396,7 +407,8 @@ public ServiceOfferingsV3 serviceOfferingsV3() {
396407
@Override
397408
@Value.Derived
398409
public ServicePlanVisibilities servicePlanVisibilities() {
399-
return new ReactorServicePlanVisibilities(getConnectionContext(), getRootV2(), getTokenProvider(), getRequestTags());
410+
return new ReactorServicePlanVisibilities(getConnectionContext(), getRootV2(), getTokenProvider(),
411+
getRequestTags());
400412
}
401413

402414
@Override
@@ -432,7 +444,8 @@ public SharedDomains sharedDomains() {
432444
@Override
433445
@Value.Derived
434446
public SpaceQuotaDefinitions spaceQuotaDefinitions() {
435-
return new ReactorSpaceQuotaDefinitions(getConnectionContext(), getRootV2(), getTokenProvider(), getRequestTags());
447+
return new ReactorSpaceQuotaDefinitions(getConnectionContext(), getRootV2(), getTokenProvider(),
448+
getRequestTags());
436449
}
437450

438451
@Override
@@ -468,7 +481,8 @@ public Tasks tasks() {
468481
@Override
469482
@Value.Derived
470483
public UserProvidedServiceInstances userProvidedServiceInstances() {
471-
return new ReactorUserProvidedServiceInstances(getConnectionContext(), getRootV2(), getTokenProvider(), getRequestTags());
484+
return new ReactorUserProvidedServiceInstances(getConnectionContext(), getRootV2(), getTokenProvider(),
485+
getRequestTags());
472486
}
473487

474488
@Override
@@ -483,7 +497,8 @@ public Users users() {
483497
abstract ConnectionContext getConnectionContext();
484498

485499
/**
486-
* Map of http header name and value which will be added to every request to the controller
500+
* Map of http header name and value which will be added to every request to the
501+
* controller
487502
*/
488503
@Value.Default
489504
Map<String, String> getRequestTags() {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2013-2021 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.reactor.client.v3.securitygroups;
18+
19+
import org.cloudfoundry.client.v3.securitygroups.SecurityGroupsV3;
20+
import org.cloudfoundry.client.v3.securitygroups.CreateSecurityGroupRequest;
21+
import org.cloudfoundry.client.v3.securitygroups.CreateSecurityGroupResponse;
22+
import org.cloudfoundry.reactor.ConnectionContext;
23+
import org.cloudfoundry.reactor.TokenProvider;
24+
import org.cloudfoundry.reactor.client.v3.AbstractClientV3Operations;
25+
import reactor.core.publisher.Mono;
26+
import java.util.Map;
27+
28+
/**
29+
* The Reactor-based implementation of {@link ServiceBindingsV3}
30+
*/
31+
public final class ReactorSecurityGroupsV3 extends AbstractClientV3Operations implements SecurityGroupsV3 {
32+
33+
/**
34+
* Creates an instance
35+
*
36+
* @param connectionContext the {@link ConnectionContext} to use when
37+
* communicating with the server
38+
* @param root the root URI of the server. Typically something like
39+
* {@code https://api.run.pivotal.io}.
40+
* @param tokenProvider the {@link TokenProvider} to use when communicating
41+
* with the server
42+
* @param requestTags map with custom http headers which will be added to
43+
* web request
44+
*/
45+
public ReactorSecurityGroupsV3(ConnectionContext connectionContext, Mono<String> root, TokenProvider tokenProvider,
46+
Map<String, String> requestTags) {
47+
super(connectionContext, root, tokenProvider, requestTags);
48+
}
49+
50+
@Override
51+
public Mono<CreateSecurityGroupResponse> create(CreateSecurityGroupRequest request) {
52+
return post(request, CreateSecurityGroupResponse.class, builder -> builder.pathSegment("security_groups"))
53+
.checkpoint();
54+
55+
}
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2013-2021 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.reactor.client.v3.securitygroups;
18+
19+
import org.cloudfoundry.reactor.InteractionContext;
20+
import org.cloudfoundry.reactor.TestRequest;
21+
import org.cloudfoundry.reactor.TestResponse;
22+
import org.cloudfoundry.client.v3.securitygroups.Relationships;
23+
import org.cloudfoundry.client.v3.securitygroups.CreateSecurityGroupRequest;
24+
import org.cloudfoundry.client.v3.securitygroups.CreateSecurityGroupResponse;
25+
import org.cloudfoundry.client.v3.securitygroups.GloballyEnabled;
26+
import org.cloudfoundry.client.v3.securitygroups.Protocol;
27+
import org.cloudfoundry.client.v3.securitygroups.Rule;
28+
import org.cloudfoundry.client.v3.securitygroups.CreateSecurityGroupRequest;
29+
import org.cloudfoundry.client.v3.ToManyRelationship;
30+
import org.cloudfoundry.reactor.client.AbstractClientApiTest;
31+
import org.junit.Test;
32+
import reactor.test.StepVerifier;
33+
import org.cloudfoundry.client.v3.Link;
34+
import org.cloudfoundry.client.v3.Relationship;
35+
36+
import java.time.Duration;
37+
import java.util.Collections;
38+
39+
import static io.netty.handler.codec.http.HttpMethod.POST;
40+
import static io.netty.handler.codec.http.HttpResponseStatus.ACCEPTED;
41+
import static io.netty.handler.codec.http.HttpResponseStatus.CREATED;
42+
43+
public final class ReactorSecurityGroupsV3Test extends AbstractClientApiTest {
44+
45+
private final ReactorSecurityGroupsV3 securityGroups = new ReactorSecurityGroupsV3(CONNECTION_CONTEXT,
46+
this.root,
47+
TOKEN_PROVIDER, Collections.emptyMap());
48+
49+
@Test
50+
public void create() {
51+
mockRequest(InteractionContext.builder()
52+
.request(TestRequest.builder()
53+
.method(POST).path("/security_groups")
54+
.payload("fixtures/client/v3/security_groups/POST_request.json")
55+
.build())
56+
.response(TestResponse.builder()
57+
.status(CREATED)
58+
.payload("fixtures/client/v3/security_groups/POST_response.json")
59+
.build())
60+
.build());
61+
this.securityGroups
62+
.create(CreateSecurityGroupRequest.builder()
63+
64+
.rules(Rule.builder()
65+
.protocol(Protocol.TCP)
66+
.destination("10.10.10.0/24")
67+
.ports("443,80,8080")
68+
.build())
69+
.name("my-group0")
70+
.rules(Rule.builder()
71+
.protocol(Protocol.ICMP)
72+
.destination("10.10.10.0/24")
73+
.description("Allow ping requests to private services")
74+
.type(8)
75+
.code(0)
76+
.build())
77+
.build())
78+
.as(StepVerifier::create)
79+
.expectNext(CreateSecurityGroupResponse.builder()
80+
.name("my-group0")
81+
.id("b85a788e-671f-4549-814d-e34cdb2f539a")
82+
.createdAt("2020-02-20T17:42:08Z")
83+
.updatedAt("2020-02-20T17:42:08Z")
84+
.globallyEnabled(GloballyEnabled.builder()
85+
.staging(false)
86+
.running(true)
87+
.build())
88+
.rules(Rule.builder()
89+
.protocol(Protocol.TCP)
90+
.destination("10.10.10.0/24")
91+
.ports("443,80,8080")
92+
.build())
93+
.rules(Rule.builder()
94+
.protocol(Protocol.ICMP)
95+
.destination("10.10.10.0/24")
96+
.description("Allow ping requests to private services")
97+
.type(8)
98+
.code(0)
99+
.build())
100+
.relationships(Relationships.builder()
101+
.stagingSpaces(ToManyRelationship.builder()
102+
.data(Relationship.builder()
103+
.id("space-guid-1")
104+
.build())
105+
.data(Relationship.builder()
106+
.id("space-guid-2")
107+
.build())
108+
.build())
109+
.runningSpaces(ToManyRelationship.builder().build())
110+
.build())
111+
.link("self", Link.builder()
112+
.href("https://api.example.org/v3/security_groups/b85a788e-671f-4549-814d-e34cdb2f539a")
113+
.build())
114+
.build())
115+
.expectComplete()
116+
.verify(Duration.ofSeconds(5));
117+
118+
}
119+
120+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "my-group0",
3+
"rules": [
4+
{
5+
"protocol": "tcp",
6+
"destination": "10.10.10.0/24",
7+
"ports": "443,80,8080"
8+
},
9+
{
10+
"protocol": "icmp",
11+
"destination": "10.10.10.0/24",
12+
"type": 8,
13+
"code": 0,
14+
"description": "Allow ping requests to private services"
15+
}
16+
]
17+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"guid": "b85a788e-671f-4549-814d-e34cdb2f539a",
3+
"created_at": "2020-02-20T17:42:08Z",
4+
"updated_at": "2020-02-20T17:42:08Z",
5+
"name": "my-group0",
6+
"globally_enabled": {
7+
"running": true,
8+
"staging": false
9+
},
10+
"rules": [
11+
{
12+
"protocol": "tcp",
13+
"destination": "10.10.10.0/24",
14+
"ports": "443,80,8080"
15+
},
16+
{
17+
"protocol": "icmp",
18+
"destination": "10.10.10.0/24",
19+
"type": 8,
20+
"code": 0,
21+
"description": "Allow ping requests to private services"
22+
}
23+
],
24+
"relationships": {
25+
"staging_spaces": {
26+
"data": [
27+
{
28+
"guid": "space-guid-1"
29+
},
30+
{
31+
"guid": "space-guid-2"
32+
}
33+
]
34+
},
35+
"running_spaces": {
36+
"data": []
37+
}
38+
},
39+
"links": {
40+
"self": {
41+
"href": "https://api.example.org/v3/security_groups/b85a788e-671f-4549-814d-e34cdb2f539a"
42+
}
43+
}
44+
}

cloudfoundry-client/src/main/java/org/cloudfoundry/client/CloudFoundryClient.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import org.cloudfoundry.client.v3.resourcematch.ResourceMatchV3;
6464
import org.cloudfoundry.client.v3.roles.RolesV3;
6565
import org.cloudfoundry.client.v3.routes.RoutesV3;
66+
import org.cloudfoundry.client.v3.securitygroups.SecurityGroupsV3;
6667
import org.cloudfoundry.client.v3.serviceinstances.ServiceInstancesV3;
6768
import org.cloudfoundry.client.v3.servicebindings.ServiceBindingsV3;
6869
import org.cloudfoundry.client.v3.servicebrokers.ServiceBrokersV3;
@@ -247,6 +248,11 @@ public interface CloudFoundryClient {
247248
*/
248249
SecurityGroups securityGroups();
249250

251+
/**
252+
* Main entry point to the Cloud Foundry Security Groups V3 Client API
253+
*/
254+
SecurityGroupsV3 securityGroupsV3();
255+
250256
/**
251257
* Main entry point to the Cloud Foundry Service Bindings V2 Client API
252258
*/
@@ -348,7 +354,8 @@ public interface CloudFoundryClient {
348354
Tasks tasks();
349355

350356
/**
351-
* Main entry point to the Cloud Foundry User Provided Service Instances Client API
357+
* Main entry point to the Cloud Foundry User Provided Service Instances Client
358+
* API
352359
*/
353360
UserProvidedServiceInstances userProvidedServiceInstances();
354361

0 commit comments

Comments
 (0)