Commit ba9ad04a905ca37f7b96e9ec998b850102216186
Committed by
Andrew Shvayka
1 parent
344878b9
Validate tenant profile usage - can not use isolated profiles in monolith setup
Showing
2 changed files
with
35 additions
and
0 deletions
@@ -19,6 +19,7 @@ import com.google.common.util.concurrent.ListenableFuture; | @@ -19,6 +19,7 @@ import com.google.common.util.concurrent.ListenableFuture; | ||
19 | import lombok.extern.slf4j.Slf4j; | 19 | import lombok.extern.slf4j.Slf4j; |
20 | import org.apache.commons.lang3.StringUtils; | 20 | import org.apache.commons.lang3.StringUtils; |
21 | import org.springframework.beans.factory.annotation.Autowired; | 21 | import org.springframework.beans.factory.annotation.Autowired; |
22 | +import org.springframework.beans.factory.annotation.Value; | ||
22 | import org.springframework.stereotype.Service; | 23 | import org.springframework.stereotype.Service; |
23 | import org.thingsboard.server.common.data.Tenant; | 24 | import org.thingsboard.server.common.data.Tenant; |
24 | import org.thingsboard.server.common.data.TenantInfo; | 25 | import org.thingsboard.server.common.data.TenantInfo; |
@@ -55,6 +56,9 @@ public class TenantServiceImpl extends AbstractEntityService implements TenantSe | @@ -55,6 +56,9 @@ public class TenantServiceImpl extends AbstractEntityService implements TenantSe | ||
55 | private static final String DEFAULT_TENANT_REGION = "Global"; | 56 | private static final String DEFAULT_TENANT_REGION = "Global"; |
56 | public static final String INCORRECT_TENANT_ID = "Incorrect tenantId "; | 57 | public static final String INCORRECT_TENANT_ID = "Incorrect tenantId "; |
57 | 58 | ||
59 | + @Value("${zk.enabled}") | ||
60 | + private Boolean zkEnabled; | ||
61 | + | ||
58 | @Autowired | 62 | @Autowired |
59 | private TenantDao tenantDao; | 63 | private TenantDao tenantDao; |
60 | 64 | ||
@@ -190,6 +194,7 @@ public class TenantServiceImpl extends AbstractEntityService implements TenantSe | @@ -190,6 +194,7 @@ public class TenantServiceImpl extends AbstractEntityService implements TenantSe | ||
190 | if (!StringUtils.isEmpty(tenant.getEmail())) { | 194 | if (!StringUtils.isEmpty(tenant.getEmail())) { |
191 | validateEmail(tenant.getEmail()); | 195 | validateEmail(tenant.getEmail()); |
192 | } | 196 | } |
197 | + validateTenantProfile(tenantId, tenant); | ||
193 | } | 198 | } |
194 | 199 | ||
195 | @Override | 200 | @Override |
@@ -198,6 +203,14 @@ public class TenantServiceImpl extends AbstractEntityService implements TenantSe | @@ -198,6 +203,14 @@ public class TenantServiceImpl extends AbstractEntityService implements TenantSe | ||
198 | if (old == null) { | 203 | if (old == null) { |
199 | throw new DataValidationException("Can't update non existing tenant!"); | 204 | throw new DataValidationException("Can't update non existing tenant!"); |
200 | } | 205 | } |
206 | + validateTenantProfile(tenantId, tenant); | ||
207 | + } | ||
208 | + | ||
209 | + private void validateTenantProfile(TenantId tenantId, Tenant tenant) { | ||
210 | + TenantProfile tenantProfileById = tenantProfileService.findTenantProfileById(tenantId, tenant.getTenantProfileId()); | ||
211 | + if (!zkEnabled && (tenantProfileById.isIsolatedTbCore() || tenantProfileById.isIsolatedTbRuleEngine())) { | ||
212 | + throw new DataValidationException("Can't use isolated tenant profiles in monolith setup!"); | ||
213 | + } | ||
201 | } | 214 | } |
202 | }; | 215 | }; |
203 | 216 |
@@ -20,8 +20,12 @@ import org.junit.Assert; | @@ -20,8 +20,12 @@ import org.junit.Assert; | ||
20 | import org.junit.Test; | 20 | import org.junit.Test; |
21 | import org.thingsboard.server.common.data.Tenant; | 21 | import org.thingsboard.server.common.data.Tenant; |
22 | import org.thingsboard.server.common.data.TenantInfo; | 22 | import org.thingsboard.server.common.data.TenantInfo; |
23 | +import org.thingsboard.server.common.data.TenantProfile; | ||
24 | +import org.thingsboard.server.common.data.id.TenantId; | ||
23 | import org.thingsboard.server.common.data.page.PageData; | 25 | import org.thingsboard.server.common.data.page.PageData; |
24 | import org.thingsboard.server.common.data.page.PageLink; | 26 | import org.thingsboard.server.common.data.page.PageLink; |
27 | +import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration; | ||
28 | +import org.thingsboard.server.common.data.tenant.profile.TenantProfileData; | ||
25 | import org.thingsboard.server.dao.exception.DataValidationException; | 29 | import org.thingsboard.server.dao.exception.DataValidationException; |
26 | 30 | ||
27 | import java.util.ArrayList; | 31 | import java.util.ArrayList; |
@@ -253,4 +257,22 @@ public abstract class BaseTenantServiceTest extends AbstractServiceTest { | @@ -253,4 +257,22 @@ public abstract class BaseTenantServiceTest extends AbstractServiceTest { | ||
253 | Assert.assertTrue(pageData.getData().isEmpty()); | 257 | Assert.assertTrue(pageData.getData().isEmpty()); |
254 | 258 | ||
255 | } | 259 | } |
260 | + | ||
261 | + @Test(expected = DataValidationException.class) | ||
262 | + public void testSaveTenantWithIsolatedProfileInMonolithSetup() { | ||
263 | + TenantProfile tenantProfile = new TenantProfile(); | ||
264 | + tenantProfile.setName("Isolated Tenant Profile"); | ||
265 | + TenantProfileData profileData = new TenantProfileData(); | ||
266 | + profileData.setConfiguration(new DefaultTenantProfileConfiguration()); | ||
267 | + tenantProfile.setProfileData(profileData); | ||
268 | + tenantProfile.setDefault(false); | ||
269 | + tenantProfile.setIsolatedTbCore(true); | ||
270 | + tenantProfile.setIsolatedTbRuleEngine(true); | ||
271 | + TenantProfile isolatedTenantProfile = tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, tenantProfile); | ||
272 | + | ||
273 | + Tenant tenant = new Tenant(); | ||
274 | + tenant.setTitle("Tenant"); | ||
275 | + tenant.setTenantProfileId(isolatedTenantProfile.getId()); | ||
276 | + tenantService.saveTenant(tenant); | ||
277 | + } | ||
256 | } | 278 | } |