Validate tenant profile usage - can not use isolated profiles in monolith setup

This commit is contained in:
Volodymyr Babak 2021-10-22 18:23:07 +03:00 committed by Andrew Shvayka
parent 344878b976
commit ba9ad04a90
2 changed files with 35 additions and 0 deletions

View File

@ -19,6 +19,7 @@ import com.google.common.util.concurrent.ListenableFuture;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.TenantInfo;
@ -55,6 +56,9 @@ public class TenantServiceImpl extends AbstractEntityService implements TenantSe
private static final String DEFAULT_TENANT_REGION = "Global";
public static final String INCORRECT_TENANT_ID = "Incorrect tenantId ";
@Value("${zk.enabled}")
private Boolean zkEnabled;
@Autowired
private TenantDao tenantDao;
@ -190,6 +194,7 @@ public class TenantServiceImpl extends AbstractEntityService implements TenantSe
if (!StringUtils.isEmpty(tenant.getEmail())) {
validateEmail(tenant.getEmail());
}
validateTenantProfile(tenantId, tenant);
}
@Override
@ -198,6 +203,14 @@ public class TenantServiceImpl extends AbstractEntityService implements TenantSe
if (old == null) {
throw new DataValidationException("Can't update non existing tenant!");
}
validateTenantProfile(tenantId, tenant);
}
private void validateTenantProfile(TenantId tenantId, Tenant tenant) {
TenantProfile tenantProfileById = tenantProfileService.findTenantProfileById(tenantId, tenant.getTenantProfileId());
if (!zkEnabled && (tenantProfileById.isIsolatedTbCore() || tenantProfileById.isIsolatedTbRuleEngine())) {
throw new DataValidationException("Can't use isolated tenant profiles in monolith setup!");
}
}
};

View File

@ -20,8 +20,12 @@ import org.junit.Assert;
import org.junit.Test;
import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.TenantInfo;
import org.thingsboard.server.common.data.TenantProfile;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration;
import org.thingsboard.server.common.data.tenant.profile.TenantProfileData;
import org.thingsboard.server.dao.exception.DataValidationException;
import java.util.ArrayList;
@ -253,4 +257,22 @@ public abstract class BaseTenantServiceTest extends AbstractServiceTest {
Assert.assertTrue(pageData.getData().isEmpty());
}
@Test(expected = DataValidationException.class)
public void testSaveTenantWithIsolatedProfileInMonolithSetup() {
TenantProfile tenantProfile = new TenantProfile();
tenantProfile.setName("Isolated Tenant Profile");
TenantProfileData profileData = new TenantProfileData();
profileData.setConfiguration(new DefaultTenantProfileConfiguration());
tenantProfile.setProfileData(profileData);
tenantProfile.setDefault(false);
tenantProfile.setIsolatedTbCore(true);
tenantProfile.setIsolatedTbRuleEngine(true);
TenantProfile isolatedTenantProfile = tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile);
Tenant tenant = new Tenant();
tenant.setTitle("Tenant");
tenant.setTenantProfileId(isolatedTenantProfile.getId());
tenantService.saveTenant(tenant);
}
}