RED-4512: Added endpoint to get tenants and added additional attributes to tenants

This commit is contained in:
deiflaender 2022-09-01 09:40:54 +02:00
parent 0adf8026ce
commit f9f2e7435a
9 changed files with 60 additions and 10 deletions

View File

@ -12,6 +12,8 @@ import lombok.NoArgsConstructor;
public class Tenant { public class Tenant {
private String tenantId; private String tenantId;
private String displayName;
private String guid;
private String jdbcUrl; private String jdbcUrl;
private String user; private String user;
private String password; private String password;

View File

@ -1,17 +1,24 @@
package com.iqser.red.service.persistence.service.v1.api.resources; package com.iqser.red.service.persistence.service.v1.api.resources;
import java.util.List;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.ResponseStatus;
import com.iqser.red.service.persistence.service.v1.api.model.multitenancy.Tenant; import com.iqser.red.service.persistence.service.v1.api.model.multitenancy.Tenant;
@ResponseStatus(value = HttpStatus.OK)
public interface TenantsResource { public interface TenantsResource {
@ResponseStatus(value = HttpStatus.OK) @PostMapping(value = "/tenants", consumes = MediaType.APPLICATION_JSON_VALUE)
@PostMapping("/tenants")
void createTenant(@RequestBody Tenant tenant); void createTenant(@RequestBody Tenant tenant);
@GetMapping(value = "/tenants", produces = MediaType.APPLICATION_JSON_VALUE)
List<Tenant> getTenants();
} }

View File

@ -21,9 +21,13 @@ public class TenantEntity {
@Id @Id
private String tenantId; private String tenantId;
@Column @Column
private String displayName;
@Column
private String guid;
@Column
private String jdbcUrl; private String jdbcUrl;
@Column(name = "username") @Column
private String user; private String username;
@Column @Column
private String password; private String password;
} }

View File

@ -92,7 +92,7 @@ public class DynamicDataSourceBasedMultiTenantConnectionProvider extends Abstrac
.type(HikariDataSource.class) .type(HikariDataSource.class)
.build(); .build();
ds.setUsername(tenant.getUser()); ds.setUsername(tenant.getUsername());
ds.setPassword(decryptedPassword); ds.setPassword(decryptedPassword);
ds.setJdbcUrl(tenant.getJdbcUrl()); ds.setJdbcUrl(tenant.getJdbcUrl());

View File

@ -1,6 +1,7 @@
package com.iqser.red.service.peristence.v1.server.controller; package com.iqser.red.service.peristence.v1.server.controller;
import org.springframework.web.bind.annotation.PostMapping; import java.util.List;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -17,10 +18,15 @@ public class TenantsController implements TenantsResource {
private final TenantManagementService tenantManagementService; private final TenantManagementService tenantManagementService;
@PostMapping("/tenants")
public void createTenant(@RequestBody Tenant tenant) { public void createTenant(@RequestBody Tenant tenant) {
tenantManagementService.createTenant(tenant); tenantManagementService.createTenant(tenant);
} }
public List<Tenant> getTenants(){
return tenantManagementService.getTenants();
}
} }

View File

@ -51,7 +51,7 @@ public class TenantSpringLiquibaseExecutor implements InitializingBean, Resource
for (TenantEntity tenant : tenants) { for (TenantEntity tenant : tenants) {
log.info("Initializing Liquibase for tenant " + tenant.getTenantId()); log.info("Initializing Liquibase for tenant " + tenant.getTenantId());
try (Connection connection = DriverManager.getConnection(tenant.getJdbcUrl(), tenant.getUser(), encryptionService.decrypt(tenant.getPassword()))) { try (Connection connection = DriverManager.getConnection(tenant.getJdbcUrl(), tenant.getUsername(), encryptionService.decrypt(tenant.getPassword()))) {
DataSource tenantDataSource = new SingleConnectionDataSource(connection, false); DataSource tenantDataSource = new SingleConnectionDataSource(connection, false);
SpringLiquibase liquibase = this.getSpringLiquibase(tenantDataSource); SpringLiquibase liquibase = this.getSpringLiquibase(tenantDataSource);
liquibase.afterPropertiesSet(); liquibase.afterPropertiesSet();

View File

@ -2,6 +2,9 @@ package com.iqser.red.service.peristence.v1.server.service;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -58,7 +61,9 @@ public class TenantManagementService {
TenantEntity tenantEntity = TenantEntity.builder() TenantEntity tenantEntity = TenantEntity.builder()
.tenantId(tenant.getTenantId()) .tenantId(tenant.getTenantId())
.user(tenant.getUser()) .displayName(tenant.getDisplayName())
.guid(UUID.randomUUID().toString())
.username(tenant.getUser())
.jdbcUrl(tenant.getJdbcUrl()) .jdbcUrl(tenant.getJdbcUrl())
.password(encryptedPassword) .password(encryptedPassword)
.build(); .build();
@ -67,6 +72,25 @@ public class TenantManagementService {
} }
public List<Tenant> getTenants() {
return tenantRepository.findAll().stream().map(this::convert).collect(Collectors.toList());
}
private Tenant convert(TenantEntity entity) {
return Tenant.builder()
.tenantId(entity.getTenantId())
.displayName(entity.getDisplayName())
.guid(entity.getGuid())
.jdbcUrl(entity.getJdbcUrl())
.password(encryptionService.decrypt(entity.getPassword()))
.user(entity.getUsername())
.build();
}
private void runLiquibase(DataSource dataSource) throws LiquibaseException { private void runLiquibase(DataSource dataSource) throws LiquibaseException {
SpringLiquibase liquibase = getSpringLiquibase(dataSource); SpringLiquibase liquibase = getSpringLiquibase(dataSource);

View File

@ -12,6 +12,12 @@ databaseChangeLog:
primaryKeyName: tenant_pkey primaryKeyName: tenant_pkey
name: tenant_id name: tenant_id
type: VARCHAR(255) type: VARCHAR(255)
- column:
name: display_name
type: VARCHAR(255)
- column:
name: guid
type: VARCHAR(255)
- column: - column:
name: username name: username
type: VARCHAR(255) type: VARCHAR(255)

View File

@ -5,6 +5,7 @@ import static org.mockito.Mockito.when;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.util.UUID;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -253,7 +254,7 @@ public abstract class AbstractPersistenceServerServiceTest {
createDatabase("redaction", "redaction"); createDatabase("redaction", "redaction");
createSchema(jdbcUrl, "redaction", "redaction"); createSchema(jdbcUrl, "redaction", "redaction");
tenantsClient.createTenant(new Tenant("redaction", jdbcUrl, "redaction", "redaction")); tenantsClient.createTenant(new Tenant("redaction","Redaction default", UUID.randomUUID().toString(), jdbcUrl, "redaction", "redaction"));
} }
} }