Skip to content

Commit

Permalink
feat: assign dataPlaneId to Domain
Browse files Browse the repository at this point in the history
  • Loading branch information
podlesrafal committed Jan 7, 2025
1 parent 2f9ca59 commit 8e63ec3
Show file tree
Hide file tree
Showing 49 changed files with 545 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<artifactId>gravitee-am-plugins-handlers-identityprovider</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.gravitee.am.plugins.handlers</groupId>
<artifactId>gravitee-am-plugins-handlers-dataplane</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Spring dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* 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 io.gravitee.am.management.handlers.management.api.resources.organizations.environments;

import io.gravitee.am.dataplane.api.DataPlane;
import io.gravitee.am.management.handlers.management.api.resources.AbstractResource;
import io.gravitee.am.model.Acl;
import io.gravitee.am.model.permissions.Permission;
import io.gravitee.am.plugins.dataplane.core.MultiDataPlaneLoader;
import io.gravitee.common.http.MediaType;
import io.reactivex.rxjava3.core.Maybe;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.container.AsyncResponse;
import jakarta.ws.rs.container.Suspended;
import org.springframework.beans.factory.annotation.Autowired;

/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author Titouan COMPIEGNE (titouan.compiegne at graviteesource.com)
* @author GraviteeSource Team
*/
@Tag(name = "dataPlane")
public class DataPlanesResource extends AbstractResource {

@Autowired
private MultiDataPlaneLoader multiDataPlaneLoader;

@GET
@Produces(MediaType.APPLICATION_JSON)
@Operation(
operationId = "listDataPlanes",
summary = "List of data planes",
description = "List all the data planes accessible to the current user. " +
"User must have DOMAIN[CREATE] permission on the specified environment or organization")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "List accessible data planes for current user",
content = @Content(mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = DataPlane.class)))),
@ApiResponse(responseCode = "500", description = "Internal server error")})
public void list(
@PathParam("organizationId") String organizationId,
@PathParam("environmentId") String environmentId,
@Suspended final AsyncResponse response) {

checkAnyPermission(organizationId, environmentId, Permission.DOMAIN, Acl.CREATE)
.andThen(Maybe.just(multiDataPlaneLoader.getDataPlanes()))
.subscribe(response::resume, response::resume);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public MembersResource getMembersResource() {
public DomainsResource getDomainsResource() {
return resourceContext.getResource(DomainsResource.class);
}

@Path("/data-planes")
public DataPlanesResource getDataPlanesResource() {
return resourceContext.getResource(DataPlanesResource.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ public void shouldGetDomains_technicalManagementException() {
public void shouldCreate() {
NewDomain newDomain = new NewDomain();
newDomain.setName("domain-name");
newDomain.setDataPlaneId("data-plane-id");

Domain domain = new Domain();
domain.setId("domain-id");
domain.setName("domain-name");
domain.setDataPlaneId("data-plane-id");

doReturn(Single.just(domain)).when(domainService).create(eq("DEFAULT"), eq("DEFAULT"), any(), any());
doReturn(Single.just(new IdentityProvider())).when(defaultIdentityProviderService).create(domain.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.gravitee.am.plugins.handlers</groupId>
<artifactId>gravitee-am-plugins-handlers-dataplane</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.gravitee.cockpit</groupId>
<artifactId>gravitee-cockpit-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.gravitee.am.common.utils.GraviteeContext;
import io.gravitee.am.common.utils.RandomString;
import io.gravitee.am.common.web.UriBuilder;
import io.gravitee.am.dataplane.api.DataPlaneDescription;
import io.gravitee.am.identityprovider.api.User;
import io.gravitee.am.management.service.DefaultIdentityProviderService;
import io.gravitee.am.management.service.DomainService;
Expand All @@ -40,6 +41,7 @@
import io.gravitee.am.model.membership.MemberType;
import io.gravitee.am.model.oidc.OIDCSettings;
import io.gravitee.am.model.permissions.SystemRole;
import io.gravitee.am.plugins.dataplane.core.MultiDataPlaneLoader;
import io.gravitee.am.repository.management.api.DomainRepository;
import io.gravitee.am.repository.management.api.search.AlertNotifierCriteria;
import io.gravitee.am.repository.management.api.search.AlertTriggerCriteria;
Expand Down Expand Up @@ -148,6 +150,8 @@ public class DomainServiceImpl implements DomainService {

private final Logger LOGGER = LoggerFactory.getLogger(DomainServiceImpl.class);

@Autowired
private MultiDataPlaneLoader multiDataPlaneLoader;

@Lazy
@Autowired
Expand Down Expand Up @@ -337,6 +341,9 @@ public Single<Domain> create(String organizationId, String environmentId, NewDom
LOGGER.debug("Create a new domain: {}", newDomain);
// generate hrid
String hrid = IdGenerator.generate(newDomain.getName());
if (multiDataPlaneLoader.getDataPlanes().stream().map(DataPlaneDescription::id).noneMatch(id -> id.equals(newDomain.getDataPlaneId()))) {
return Single.error(new TechnicalManagementException("An error occurred while trying to create a domain. Data plane with provided Id doesn't exist"));
}
return domainRepository.findByHrid(ReferenceType.ENVIRONMENT, environmentId, hrid)
.isEmpty()
.flatMap(empty -> {
Expand All @@ -357,6 +364,7 @@ public Single<Domain> create(String organizationId, String environmentId, NewDom
domain.setReferenceId(environmentId);
domain.setCreatedAt(new Date());
domain.setUpdatedAt(domain.getCreatedAt());
domain.setDataPlaneId(newDomain.getDataPlaneId());

return environmentService.findById(domain.getReferenceId())
.doOnSuccess(environment -> setDeployMode(domain, environment))
Expand Down Expand Up @@ -645,7 +653,7 @@ public Completable delete(GraviteeContext graviteeContext, String domainId, User
.principal(principal)
.type(EventType.DOMAIN_DELETED)
.throwable(throwable)
.domain(domain)
.domain(domain)
.reference(Reference.organization(graviteeContext.getOrganizationId()))));
})
.onErrorResumeNext(ex -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import io.gravitee.am.common.utils.GraviteeContext;
import io.gravitee.am.dataplane.api.DataPlaneDescription;
import io.gravitee.am.identityprovider.api.DefaultUser;
import io.gravitee.am.management.service.DefaultIdentityProviderService;
import io.gravitee.am.management.service.DomainService;
import io.gravitee.am.management.service.IdentityProviderManager;
import io.gravitee.am.model.Application;
import io.gravitee.am.model.AuthenticationDeviceNotifier;
import io.gravitee.am.model.Certificate;
Expand Down Expand Up @@ -52,6 +51,7 @@
import io.gravitee.am.model.oauth2.Scope;
import io.gravitee.am.model.permissions.SystemRole;
import io.gravitee.am.model.uma.Resource;
import io.gravitee.am.plugins.dataplane.core.MultiDataPlaneLoader;
import io.gravitee.am.repository.exceptions.TechnicalException;
import io.gravitee.am.repository.management.api.DomainRepository;
import io.gravitee.am.repository.management.api.search.AlertNotifierCriteria;
Expand Down Expand Up @@ -164,6 +164,9 @@ public class DomainServiceTest {
@InjectMocks
private DomainServiceImpl domainService = new DomainServiceImpl();

@Mock
private MultiDataPlaneLoader multiDataPlaneLoader;

@Mock
private DomainValidator domainValidator;

Expand Down Expand Up @@ -355,13 +358,16 @@ public void shouldFindByIdsIn_technicalException() {
public void shouldCreate() {
NewDomain newDomain = new NewDomain();
newDomain.setName("my-domain");
newDomain.setDataPlaneId("default");
when(environmentService.findById(ENVIRONMENT_ID)).thenReturn(Single.just(new Environment()));
when(domainReadService.listAll()).thenReturn(Flowable.empty());
Domain domain = new Domain();
domain.setReferenceType(ReferenceType.ENVIRONMENT);
domain.setReferenceId(ENVIRONMENT_ID);
domain.setId("domain-id");
domain.setVersion(DomainVersion.V2_0);
domain.setDataPlaneId("default");
when(multiDataPlaneLoader.getDataPlanes()).thenReturn(List.of(new DataPlaneDescription("default","default","mongodb","test")));
when(domainRepository.findByHrid(ReferenceType.ENVIRONMENT, ENVIRONMENT_ID, "my-domain")).thenReturn(Maybe.empty());
when(domainRepository.create(any(Domain.class))).thenReturn(Single.just(domain));
when(scopeService.create(anyString(), any(NewSystemScope.class))).thenReturn(Single.just(new Scope()));
Expand Down Expand Up @@ -402,13 +408,16 @@ public void shouldCreate_withoutDefaultReporter() {

NewDomain newDomain = new NewDomain();
newDomain.setName("my-domain");
newDomain.setDataPlaneId("default");
when(environmentService.findById(ENVIRONMENT_ID)).thenReturn(Single.just(new Environment()));
when(domainReadService.listAll()).thenReturn(Flowable.empty());
Domain domain = new Domain();
domain.setReferenceType(ReferenceType.ENVIRONMENT);
domain.setReferenceId(ENVIRONMENT_ID);
domain.setId("domain-id");
domain.setVersion(DomainVersion.V2_0);
domain.setDataPlaneId("default");
when(multiDataPlaneLoader.getDataPlanes()).thenReturn(List.of(new DataPlaneDescription("default","default","mongodb","test")));
when(domainRepository.findByHrid(ReferenceType.ENVIRONMENT, ENVIRONMENT_ID, "my-domain")).thenReturn(Maybe.empty());
when(domainRepository.create(any(Domain.class))).thenReturn(Single.just(domain));
when(scopeService.create(anyString(), any(NewSystemScope.class))).thenReturn(Single.just(new Scope()));
Expand Down Expand Up @@ -448,13 +457,16 @@ public void shouldCreate_withoutDefaultIdp() {

NewDomain newDomain = new NewDomain();
newDomain.setName("my-domain");
newDomain.setDataPlaneId("default");
when(environmentService.findById(ENVIRONMENT_ID)).thenReturn(Single.just(new Environment()));
when(domainReadService.listAll()).thenReturn(Flowable.empty());
Domain domain = new Domain();
domain.setReferenceType(ReferenceType.ENVIRONMENT);
domain.setReferenceId(ENVIRONMENT_ID);
domain.setId("domain-id");
domain.setVersion(DomainVersion.V2_0);
domain.setDataPlaneId("default");
when(multiDataPlaneLoader.getDataPlanes()).thenReturn(List.of(new DataPlaneDescription("default","default","mongodb","test")));
when(domainRepository.findByHrid(ReferenceType.ENVIRONMENT, ENVIRONMENT_ID, "my-domain")).thenReturn(Maybe.empty());
when(domainRepository.create(any(Domain.class))).thenReturn(Single.just(domain));
when(scopeService.create(anyString(), any(NewSystemScope.class))).thenReturn(Single.just(new Scope()));
Expand Down Expand Up @@ -493,6 +505,8 @@ public void shouldCreate_withoutDefaultIdp() {
public void shouldCreate_technicalException() {
NewDomain newDomain = Mockito.mock(NewDomain.class);
when(newDomain.getName()).thenReturn("my-domain");
when(newDomain.getDataPlaneId()).thenReturn("default");
when(multiDataPlaneLoader.getDataPlanes()).thenReturn(List.of(new DataPlaneDescription("default","default","mongodb","test")));
when(domainRepository.findByHrid(ReferenceType.ENVIRONMENT, ENVIRONMENT_ID, "my-domain")).thenReturn(Maybe.error(TechnicalException::new));

TestObserver<Domain> testObserver = new TestObserver<>();
Expand All @@ -508,6 +522,8 @@ public void shouldCreate_technicalException() {
public void shouldCreate2_technicalException() {
NewDomain newDomain = Mockito.mock(NewDomain.class);
when(newDomain.getName()).thenReturn("my-domain");
when(newDomain.getDataPlaneId()).thenReturn("default");
when(multiDataPlaneLoader.getDataPlanes()).thenReturn(List.of(new DataPlaneDescription("default","default","mongodb","test")));
when(domainRepository.findByHrid(ReferenceType.ENVIRONMENT, ENVIRONMENT_ID, "my-domain")).thenReturn(Maybe.empty());
when(environmentService.findById(ENVIRONMENT_ID)).thenReturn(Single.just(new Environment()));

Expand All @@ -524,6 +540,8 @@ public void shouldCreate2_technicalException() {
public void shouldCreate_existingDomain() {
NewDomain newDomain = Mockito.mock(NewDomain.class);
when(newDomain.getName()).thenReturn("my-domain");
when(newDomain.getDataPlaneId()).thenReturn("default");
when(multiDataPlaneLoader.getDataPlanes()).thenReturn(List.of(new DataPlaneDescription("default","default","mongodb","test")));
when(domainRepository.findByHrid(ReferenceType.ENVIRONMENT, ENVIRONMENT_ID, "my-domain")).thenReturn(Maybe.just(new Domain()));

TestObserver<Domain> testObserver = new TestObserver<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,24 @@
package io.gravitee.am.service.model;

import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;

/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
@Getter
@Setter
public class NewDomain {

@NotNull
private String name;

private String description;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
@NotNull
private String dataPlaneId;

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const createDomain = (accessToken, name, description): Promise<Domain> =>
newDomain: {
name: name,
description: description,
dataPlaneId: "default"
},
});

Expand Down
2 changes: 2 additions & 0 deletions gravitee-am-test/api/management/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ apis/AuditApi.ts
apis/AuthenticationDeviceNotifierApi.ts
apis/BotDetectionApi.ts
apis/CertificateApi.ts
apis/DataPlaneApi.ts
apis/DefaultApi.ts
apis/DeviceIdentifierApi.ts
apis/DeviceIdentifiersApi.ts
Expand Down Expand Up @@ -81,6 +82,7 @@ models/ClientSecret.ts
models/CookieSettings.ts
models/CorsSettings.ts
models/Credential.ts
models/DataPlane.ts
models/DeviceIdentifier.ts
models/Domain.ts
models/DomainUserBulkRequest.ts
Expand Down
Loading

0 comments on commit 8e63ec3

Please sign in to comment.