forked from Onlineberatung/onlineBeratung-migrationTool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from virtualidentityag/develop
staging
- Loading branch information
Showing
11 changed files
with
451 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
src/main/java/com/vi/migrationtool/keycloak/TenantUpdateService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package com.vi.migrationtool.keycloak; | ||
|
||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class TenantUpdateService { | ||
|
||
private static final int USER_PAGE_SIZE = 300; | ||
private static final String TENANT_ID = "tenantId"; | ||
private static final String SUCCESSFULLY_SET_TENANT_ID_FOR_WITH_ID_TO = | ||
"Successfully set tenantId for {} with id {} to {}"; | ||
|
||
private final KeycloakUserService keycloakUserService; | ||
|
||
private final KeycloakLoginService keycloakLoginService; | ||
|
||
private final JdbcTemplate userServiceJdbcTemplate; | ||
|
||
private HttpHeaders authenticateInKeycloak() { | ||
var httpHeaders = new HttpHeaders(); | ||
httpHeaders.setContentType(MediaType.APPLICATION_JSON); | ||
KeycloakLoginResponseDTO loginResponse = keycloakLoginService.loginAdminUser(); | ||
httpHeaders.setBearerAuth(loginResponse.getAccessToken()); | ||
return httpHeaders; | ||
} | ||
|
||
public void updateAdviceSeekersTenant(List<UserTenant> adviceSeekerToTargetTenantCollection) { | ||
var httpHeaders = authenticateInKeycloak(); | ||
if (adviceSeekerToTargetTenantCollection.isEmpty()) { | ||
log.info("No advice seeker found with different tenantId"); | ||
return; | ||
} | ||
for (int i = 0; i < adviceSeekerToTargetTenantCollection.size(); i++) { | ||
var adviceSeekerTenant = adviceSeekerToTargetTenantCollection.get(i); | ||
if (i % USER_PAGE_SIZE == 0) { | ||
httpHeaders = authenticateInKeycloak(); | ||
} | ||
log.info( | ||
"Attempt to set tenantId for adviceseeker with id {} to {}", | ||
adviceSeekerTenant.getUserId(), | ||
adviceSeekerTenant.getTenantId()); | ||
|
||
keycloakUserService.updateUserCustomAttributeWithoutLogin( | ||
TENANT_ID, adviceSeekerTenant.getTenantId(), adviceSeekerTenant.getUserId(), httpHeaders); | ||
|
||
updateTables(adviceSeekerTenant); | ||
log.info( | ||
SUCCESSFULLY_SET_TENANT_ID_FOR_WITH_ID_TO, | ||
"advice seeker, user and keycloak user", | ||
adviceSeekerTenant.getUserId(), | ||
adviceSeekerTenant.getTenantId()); | ||
} | ||
} | ||
|
||
private void updateTables(UserTenant adviceSeekerTenant) { | ||
userServiceJdbcTemplate.update( | ||
"UPDATE user SET tenant_id = ? WHERE user_id = ?", | ||
adviceSeekerTenant.getTenantId(), | ||
adviceSeekerTenant.getUserId()); | ||
|
||
userServiceJdbcTemplate.update( | ||
"UPDATE session SET tenant_id = ? WHERE user_id = ?", | ||
adviceSeekerTenant.getTenantId(), | ||
adviceSeekerTenant.getUserId()); | ||
} | ||
|
||
public void updateConsultantsTenant(List<UserTenant> consultantTargetTenants) { | ||
|
||
if (consultantTargetTenants.isEmpty()) { | ||
log.info("No consultant found with different tenantId"); | ||
return; | ||
} | ||
var httpHeaders = authenticateInKeycloak(); | ||
for (int i = 0; i < consultantTargetTenants.size(); i++) { | ||
if (i % USER_PAGE_SIZE == 0) { | ||
httpHeaders = authenticateInKeycloak(); | ||
} | ||
var consultantTenant = consultantTargetTenants.get(i); | ||
|
||
log.info( | ||
"Attempt to set tenantId for {} with id {} to {}", | ||
"consultant", | ||
consultantTenant.getUserId(), | ||
consultantTenant.getTenantId()); | ||
|
||
keycloakUserService.updateUserCustomAttributeWithoutLogin( | ||
TENANT_ID, consultantTenant.getTenantId(), consultantTenant.getUserId(), httpHeaders); | ||
|
||
userServiceJdbcTemplate.update( | ||
"UPDATE consultant_agency SET tenant_id = ? WHERE consultant_id = ?", | ||
consultantTenant.getTenantId(), | ||
consultantTenant.getUserId()); | ||
|
||
userServiceJdbcTemplate.update( | ||
"UPDATE consultant SET tenant_id = ? WHERE consultant_id = ?", | ||
consultantTenant.getTenantId(), | ||
consultantTenant.getUserId()); | ||
|
||
userServiceJdbcTemplate.update( | ||
"UPDATE session SET tenant_id = ? WHERE consultant_id = ?", | ||
consultantTenant.getTenantId(), | ||
consultantTenant.getUserId()); | ||
|
||
log.info( | ||
SUCCESSFULLY_SET_TENANT_ID_FOR_WITH_ID_TO, | ||
"consultant, consultant_agency, session and keycloak user", | ||
consultantTenant.getUserId(), | ||
consultantTenant.getTenantId()); | ||
} | ||
} | ||
|
||
public void updateAdminTenant(List<UserTenant> adminTargetTenants) { | ||
|
||
if (adminTargetTenants.isEmpty()) { | ||
log.info("No admin found with different tenantId"); | ||
return; | ||
} | ||
var httpHeaders = authenticateInKeycloak(); | ||
for (int i = 0; i < adminTargetTenants.size(); i++) { | ||
if (i % USER_PAGE_SIZE == 0) { | ||
httpHeaders = authenticateInKeycloak(); | ||
} | ||
var adminTenant = adminTargetTenants.get(i); | ||
|
||
log.info( | ||
"Attempt to set tenantId for {} with id {} to {}", | ||
"consultant", | ||
adminTenant.getUserId(), | ||
adminTenant.getTenantId()); | ||
|
||
keycloakUserService.updateUserCustomAttributeWithoutLogin( | ||
TENANT_ID, adminTenant.getTenantId(), adminTenant.getUserId(), httpHeaders); | ||
|
||
userServiceJdbcTemplate.update( | ||
"UPDATE admin SET tenant_id = ? WHERE admin_id = ?", | ||
adminTenant.getTenantId(), | ||
adminTenant.getUserId()); | ||
|
||
log.info( | ||
SUCCESSFULLY_SET_TENANT_ID_FOR_WITH_ID_TO, | ||
"admin table and keycloak user", | ||
adminTenant.getUserId(), | ||
adminTenant.getTenantId()); | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/com/vi/migrationtool/tenantservice/MergeTenantsMigrationTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.vi.migrationtool.tenantservice; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.vi.migrationtool.common.MigrationTasks; | ||
import com.vi.migrationtool.config.BeanAwareSpringLiquibase; | ||
import com.vi.migrationtool.keycloak.KeycloakLoginService; | ||
import com.vi.migrationtool.keycloak.TenantUpdateService; | ||
import java.util.List; | ||
import liquibase.database.Database; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jsoup.helper.Validate; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
@Slf4j | ||
@Setter | ||
public class MergeTenantsMigrationTask extends MigrationTasks { | ||
|
||
String tenantMigrationConfiguration; | ||
private KeycloakLoginService keycloakLoginService; | ||
|
||
@Override | ||
public void execute(Database database) { | ||
Validate.notNull( | ||
tenantMigrationConfiguration, "Tenant migration configuration must not be null"); | ||
var tenantServiceJdbcTemplate = | ||
BeanAwareSpringLiquibase.getNamedBean("tenantServiceJdbcTemplate", JdbcTemplate.class); | ||
var userServiceJdbcTemplate = | ||
BeanAwareSpringLiquibase.getNamedBean("userServiceJdbcTemplate", JdbcTemplate.class); | ||
|
||
var tenantUpdateService = | ||
BeanAwareSpringLiquibase.getNamedBean("tenantUpdateService", TenantUpdateService.class); | ||
|
||
var agencyServiceJdbcTemplate = | ||
BeanAwareSpringLiquibase.getNamedBean("agencyServiceJdbcTemplate", JdbcTemplate.class); | ||
|
||
this.keycloakLoginService = | ||
BeanAwareSpringLiquibase.getNamedBean("keycloakLoginService", KeycloakLoginService.class); | ||
|
||
var objectMapper = | ||
new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
var migrations = readMigrations(objectMapper); | ||
|
||
TenantMigrationService tenantMigrationService = | ||
new TenantMigrationService( | ||
tenantServiceJdbcTemplate, | ||
userServiceJdbcTemplate, | ||
agencyServiceJdbcTemplate, | ||
tenantUpdateService); | ||
|
||
log.info("Read migration configuration: {}", migrations); | ||
|
||
performMigrations(migrations, tenantMigrationService); | ||
} | ||
|
||
private void performMigrations( | ||
List<TenantMigrationConfiguration> migrations, | ||
TenantMigrationService tenantMigrationService) { | ||
migrations.stream() | ||
.forEach( | ||
migration -> { | ||
log.info( | ||
"Migrating tenants from {} to {}", | ||
migration.getSourceTenantId(), | ||
migration.getTargetTenantId()); | ||
tenantMigrationService.performMigration(migration); | ||
}); | ||
} | ||
|
||
private List<TenantMigrationConfiguration> readMigrations(ObjectMapper objectMapper) { | ||
try { | ||
TypeReference<List<TenantMigrationConfiguration>> typeReference = | ||
new TypeReference<List<TenantMigrationConfiguration>>() {}; | ||
return objectMapper.readValue(tenantMigrationConfiguration, typeReference); | ||
} catch (JsonProcessingException e) { | ||
throw new IllegalStateException("Could not process json file ", e); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/vi/migrationtool/tenantservice/TenantMigrationConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.vi.migrationtool.tenantservice; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
@NoArgsConstructor | ||
@Setter | ||
public class TenantMigrationConfiguration { | ||
|
||
private Long sourceTenantId; | ||
private Long targetTenantId; | ||
private boolean deleteSourceTenant; | ||
} |
Oops, something went wrong.