Skip to content

Commit

Permalink
adding create security group impl and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
radoslav-tomov committed Jul 6, 2023
1 parent b5aea46 commit 92fa5db
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.cloudfoundry.client.v2.routemappings.RouteMappings;
import org.cloudfoundry.client.v2.routes.Routes;
import org.cloudfoundry.client.v2.securitygroups.SecurityGroups;
import org.cloudfoundry.client.v3.securitygroups.SecurityGroupsV3;
import org.cloudfoundry.client.v2.servicebindings.ServiceBindingsV2;
import org.cloudfoundry.client.v2.servicebrokers.ServiceBrokers;
import org.cloudfoundry.client.v2.serviceinstances.ServiceInstances;
Expand Down Expand Up @@ -91,6 +92,7 @@
import org.cloudfoundry.reactor.client.v2.routemappings.ReactorRouteMappings;
import org.cloudfoundry.reactor.client.v2.routes.ReactorRoutes;
import org.cloudfoundry.reactor.client.v2.securitygroups.ReactorSecurityGroups;
import org.cloudfoundry.reactor.client.v3.securitygroups.ReactorSecurityGroupsV3;
import org.cloudfoundry.reactor.client.v2.servicebindings.ReactorServiceBindingsV2;
import org.cloudfoundry.reactor.client.v2.servicebrokers.ReactorServiceBrokers;
import org.cloudfoundry.reactor.client.v2.serviceinstances.ReactorServiceInstances;
Expand Down Expand Up @@ -151,7 +153,8 @@ public AdminV3 adminV3() {
@Override
@Value.Derived
public ApplicationUsageEvents applicationUsageEvents() {
return new ReactorApplicationUsageEvents(getConnectionContext(), getRootV2(), getTokenProvider(), getRequestTags());
return new ReactorApplicationUsageEvents(getConnectionContext(), getRootV2(), getTokenProvider(),
getRequestTags());
}

@Override
Expand Down Expand Up @@ -228,7 +231,8 @@ public Droplets droplets() {
@Override
@Value.Derived
public EnvironmentVariableGroups environmentVariableGroups() {
return new ReactorEnvironmentVariableGroups(getConnectionContext(), getRootV2(), getTokenProvider(), getRequestTags());
return new ReactorEnvironmentVariableGroups(getConnectionContext(), getRootV2(), getTokenProvider(),
getRequestTags());
}

@Override
Expand Down Expand Up @@ -270,7 +274,8 @@ public JobsV3 jobsV3() {
@Override
@Value.Derived
public OrganizationQuotaDefinitions organizationQuotaDefinitions() {
return new ReactorOrganizationQuotaDefinitions(getConnectionContext(), getRootV2(), getTokenProvider(), getRequestTags());
return new ReactorOrganizationQuotaDefinitions(getConnectionContext(), getRootV2(), getTokenProvider(),
getRequestTags());
}

@Override
Expand Down Expand Up @@ -345,6 +350,12 @@ public SecurityGroups securityGroups() {
return new ReactorSecurityGroups(getConnectionContext(), getRootV2(), getTokenProvider(), getRequestTags());
}

@Override
@Value.Derived
public SecurityGroupsV3 securityGroupsV3() {
return new ReactorSecurityGroupsV3(getConnectionContext(), getRootV2(), getTokenProvider(), getRequestTags());
}

@Override
@Value.Derived
public ServiceBindingsV2 serviceBindingsV2() {
Expand All @@ -366,7 +377,7 @@ public ServiceBrokers serviceBrokers() {
@Override
@Value.Derived
public ServiceBrokersV3 serviceBrokersV3() {
return new ReactorServiceBrokersV3(getConnectionContext(), getRootV3(), getTokenProvider(), getRequestTags());
return new ReactorServiceBrokersV3(getConnectionContext(), getRootV3(), getTokenProvider(), getRequestTags());
}

@Override
Expand Down Expand Up @@ -396,7 +407,8 @@ public ServiceOfferingsV3 serviceOfferingsV3() {
@Override
@Value.Derived
public ServicePlanVisibilities servicePlanVisibilities() {
return new ReactorServicePlanVisibilities(getConnectionContext(), getRootV2(), getTokenProvider(), getRequestTags());
return new ReactorServicePlanVisibilities(getConnectionContext(), getRootV2(), getTokenProvider(),
getRequestTags());
}

@Override
Expand Down Expand Up @@ -432,7 +444,8 @@ public SharedDomains sharedDomains() {
@Override
@Value.Derived
public SpaceQuotaDefinitions spaceQuotaDefinitions() {
return new ReactorSpaceQuotaDefinitions(getConnectionContext(), getRootV2(), getTokenProvider(), getRequestTags());
return new ReactorSpaceQuotaDefinitions(getConnectionContext(), getRootV2(), getTokenProvider(),
getRequestTags());
}

@Override
Expand Down Expand Up @@ -468,7 +481,8 @@ public Tasks tasks() {
@Override
@Value.Derived
public UserProvidedServiceInstances userProvidedServiceInstances() {
return new ReactorUserProvidedServiceInstances(getConnectionContext(), getRootV2(), getTokenProvider(), getRequestTags());
return new ReactorUserProvidedServiceInstances(getConnectionContext(), getRootV2(), getTokenProvider(),
getRequestTags());
}

@Override
Expand All @@ -483,7 +497,8 @@ public Users users() {
abstract ConnectionContext getConnectionContext();

/**
* Map of http header name and value which will be added to every request to the controller
* Map of http header name and value which will be added to every request to the
* controller
*/
@Value.Default
Map<String, String> getRequestTags() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.cloudfoundry.reactor.client.v3.securitygroups;

import org.cloudfoundry.client.v3.securitygroups.SecurityGroupsV3;
import org.cloudfoundry.client.v3.securitygroups.CreateSecurityGroupRequest;
import org.cloudfoundry.client.v3.securitygroups.CreateSecurityGroupResponse;
import org.cloudfoundry.reactor.ConnectionContext;
import org.cloudfoundry.reactor.TokenProvider;
import org.cloudfoundry.reactor.client.v3.AbstractClientV3Operations;
import reactor.core.publisher.Mono;
import java.util.Map;

/**
* The Reactor-based implementation of {@link ServiceBindingsV3}
*/
public final class ReactorSecurityGroupsV3 extends AbstractClientV3Operations implements SecurityGroupsV3 {

/**
* Creates an instance
*
* @param connectionContext the {@link ConnectionContext} to use when
* communicating with the server
* @param root the root URI of the server. Typically something like
* {@code https://api.run.pivotal.io}.
* @param tokenProvider the {@link TokenProvider} to use when communicating
* with the server
* @param requestTags map with custom http headers which will be added to
* web request
*/
public ReactorSecurityGroupsV3(ConnectionContext connectionContext, Mono<String> root, TokenProvider tokenProvider,
Map<String, String> requestTags) {
super(connectionContext, root, tokenProvider, requestTags);
}

@Override
public Mono<CreateSecurityGroupResponse> create(CreateSecurityGroupRequest request) {
return post(request, CreateSecurityGroupResponse.class, builder -> builder.pathSegment("security_groups"))
.checkpoint();

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.cloudfoundry.reactor.client.v3.securitygroups;

import org.cloudfoundry.reactor.InteractionContext;
import org.cloudfoundry.reactor.TestRequest;
import org.cloudfoundry.reactor.TestResponse;
import org.cloudfoundry.client.v3.securitygroups.Relationships;
import org.cloudfoundry.client.v3.securitygroups.CreateSecurityGroupRequest;
import org.cloudfoundry.client.v3.securitygroups.CreateSecurityGroupResponse;
import org.cloudfoundry.client.v3.securitygroups.GloballyEnabled;
import org.cloudfoundry.client.v3.securitygroups.Protocol;
import org.cloudfoundry.client.v3.securitygroups.Rule;
import org.cloudfoundry.client.v3.securitygroups.CreateSecurityGroupRequest;
import org.cloudfoundry.client.v3.ToManyRelationship;
import org.cloudfoundry.reactor.client.AbstractClientApiTest;
import org.junit.Test;
import reactor.test.StepVerifier;
import org.cloudfoundry.client.v3.Link;
import org.cloudfoundry.client.v3.Relationship;

import java.time.Duration;
import java.util.Collections;

import static io.netty.handler.codec.http.HttpMethod.POST;
import static io.netty.handler.codec.http.HttpResponseStatus.ACCEPTED;
import static io.netty.handler.codec.http.HttpResponseStatus.CREATED;

public final class ReactorSecurityGroupsV3Test extends AbstractClientApiTest {

private final ReactorSecurityGroupsV3 securityGroups = new ReactorSecurityGroupsV3(CONNECTION_CONTEXT,
this.root,
TOKEN_PROVIDER, Collections.emptyMap());

@Test
public void create() {
mockRequest(InteractionContext.builder()
.request(TestRequest.builder()
.method(POST).path("/security_groups")
.payload("fixtures/client/v3/security_groups/POST_request.json")
.build())
.response(TestResponse.builder()
.status(CREATED)
.payload("fixtures/client/v3/security_groups/POST_response.json")
.build())
.build());
this.securityGroups
.create(CreateSecurityGroupRequest.builder()

.rules(Rule.builder()
.protocol(Protocol.TCP)
.destination("10.10.10.0/24")
.ports("443,80,8080")
.build())
.name("my-group0")
.rules(Rule.builder()
.protocol(Protocol.ICMP)
.destination("10.10.10.0/24")
.description("Allow ping requests to private services")
.type(8)
.code(0)
.build())
.build())
.as(StepVerifier::create)
.expectNext(CreateSecurityGroupResponse.builder()
.name("my-group0")
.id("b85a788e-671f-4549-814d-e34cdb2f539a")
.createdAt("2020-02-20T17:42:08Z")
.updatedAt("2020-02-20T17:42:08Z")
.globallyEnabled(GloballyEnabled.builder()
.staging(false)
.running(true)
.build())
.rules(Rule.builder()
.protocol(Protocol.TCP)
.destination("10.10.10.0/24")
.ports("443,80,8080")
.build())
.rules(Rule.builder()
.protocol(Protocol.ICMP)
.destination("10.10.10.0/24")
.description("Allow ping requests to private services")
.type(8)
.code(0)
.build())
.relationships(Relationships.builder()
.stagingSpaces(ToManyRelationship.builder()
.data(Relationship.builder()
.id("space-guid-1")
.build())
.data(Relationship.builder()
.id("space-guid-2")
.build())
.build())
.runningSpaces(ToManyRelationship.builder().build())
.build())
.link("self", Link.builder()
.href("https://api.example.org/v3/security_groups/b85a788e-671f-4549-814d-e34cdb2f539a")
.build())
.build())
.expectComplete()
.verify(Duration.ofSeconds(5));

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "my-group0",
"rules": [
{
"protocol": "tcp",
"destination": "10.10.10.0/24",
"ports": "443,80,8080"
},
{
"protocol": "icmp",
"destination": "10.10.10.0/24",
"type": 8,
"code": 0,
"description": "Allow ping requests to private services"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"guid": "b85a788e-671f-4549-814d-e34cdb2f539a",
"created_at": "2020-02-20T17:42:08Z",
"updated_at": "2020-02-20T17:42:08Z",
"name": "my-group0",
"globally_enabled": {
"running": true,
"staging": false
},
"rules": [
{
"protocol": "tcp",
"destination": "10.10.10.0/24",
"ports": "443,80,8080"
},
{
"protocol": "icmp",
"destination": "10.10.10.0/24",
"type": 8,
"code": 0,
"description": "Allow ping requests to private services"
}
],
"relationships": {
"staging_spaces": {
"data": [
{
"guid": "space-guid-1"
},
{
"guid": "space-guid-2"
}
]
},
"running_spaces": {
"data": []
}
},
"links": {
"self": {
"href": "https://api.example.org/v3/security_groups/b85a788e-671f-4549-814d-e34cdb2f539a"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import org.cloudfoundry.client.v3.resourcematch.ResourceMatchV3;
import org.cloudfoundry.client.v3.roles.RolesV3;
import org.cloudfoundry.client.v3.routes.RoutesV3;
import org.cloudfoundry.client.v3.securitygroups.SecurityGroupsV3;
import org.cloudfoundry.client.v3.serviceinstances.ServiceInstancesV3;
import org.cloudfoundry.client.v3.servicebindings.ServiceBindingsV3;
import org.cloudfoundry.client.v3.servicebrokers.ServiceBrokersV3;
Expand Down Expand Up @@ -247,6 +248,11 @@ public interface CloudFoundryClient {
*/
SecurityGroups securityGroups();

/**
* Main entry point to the Cloud Foundry Security Groups V3 Client API
*/
SecurityGroupsV3 securityGroupsV3();

/**
* Main entry point to the Cloud Foundry Service Bindings V2 Client API
*/
Expand Down Expand Up @@ -348,7 +354,8 @@ public interface CloudFoundryClient {
Tasks tasks();

/**
* Main entry point to the Cloud Foundry User Provided Service Instances Client API
* Main entry point to the Cloud Foundry User Provided Service Instances Client
* API
*/
UserProvidedServiceInstances userProvidedServiceInstances();

Expand Down
Loading

0 comments on commit 92fa5db

Please sign in to comment.