public TenantModelDao(final Tenant tenant) { this(tenant.getId(), tenant.getCreatedDate(), tenant.getUpdatedDate(), tenant.getExternalKey(), tenant.getApiKey(), tenant.getApiSecret(), null); }
@Test(groups = "slow") public void testTenant() throws Exception { final TenantData tenantdata = new DefaultTenant(UUID.randomUUID(), clock.getUTCNow(), clock.getUTCNow(), "er44TT-yy4r", "TTR445ee2", "dskjhfs^^54R"); tenantUserApi.createTenant(tenantdata, callContext); final Tenant tenant = tenantUserApi.getTenantByApiKey(tenantdata.getApiKey()); Assert.assertEquals(tenant.getApiKey(), tenantdata.getApiKey()); Assert.assertEquals(tenant.getExternalKey(), tenantdata.getExternalKey()); // The second time, the value is already in the cache so the TenantCacheLoader is not invoked final Tenant tenant2 = tenantUserApi.getTenantByApiKey(tenantdata.getApiKey()); Assert.assertEquals(tenant2.getApiKey(), tenantdata.getApiKey()); Assert.assertEquals(tenant2.getExternalKey(), tenantdata.getExternalKey()); }
public TenantJson(final Tenant tenant) { this(tenant.getId(), tenant.getExternalKey(), tenant.getApiKey(), tenant.getApiSecret()); }
public CallContext createCallContextWithAccountId(final UUID accountId, final String createdBy, final String reason, final String comment, final ServletRequest request) throws IllegalArgumentException { try { Preconditions.checkNotNull(createdBy, String.format("Header %s needs to be set", JaxrsResource.HDR_CREATED_BY)); final Tenant tenant = getTenantFromRequest(request); final UUID tenantId = tenant == null ? null : tenant.getId(); final CallContext callContext = contextFactory.createCallContext(accountId, tenantId, createdBy, origin, userType, reason, comment, getOrCreateUserToken()); populateMDCContext(callContext); return callContext; } catch (final NullPointerException e) { throw new IllegalArgumentException(e.getMessage()); } }
@Override public Tenant createTenant(final TenantData data, final CallContext context) throws TenantApiException { final Tenant tenant = new DefaultTenant(data); if (null != tenant.getExternalKey() && tenant.getExternalKey().length() > 255) { throw new TenantApiException(ErrorCode.EXTERNAL_KEY_LIMIT_EXCEEDED); } try { // Not transactional, but there is a db constraint on that column if (data.getApiKey() != null && getTenantByApiKey(data.getApiKey()) != null) { throw new TenantApiException(ErrorCode.TENANT_ALREADY_EXISTS, data.getExternalKey()); } } catch (final RuntimeException e) { if (e.getCause() instanceof IllegalStateException) { // could happen exemption, stating that the key is not found } else { throw e; } } try { tenantDao.create(new TenantModelDao(tenant), internalCallContextFactory.createInternalCallContextWithoutAccountRecordId(context)); } catch (final TenantApiException e) { throw new TenantApiException(e, ErrorCode.TENANT_CREATION_FAILED); } return tenant; }
@Test(groups = "slow") public void testTenant() throws Exception { final TenantData tenantdata = new DefaultTenant(UUID.randomUUID(), clock.getUTCNow(), clock.getUTCNow(), "er44TT-yy4r", "TTR445ee2", "dskjhfs^^54R"); tenantUserApi.createTenant(tenantdata, callContext); final Tenant tenant = tenantUserApi.getTenantByApiKey(tenantdata.getApiKey()); Assert.assertEquals(tenant.getApiKey(), tenantdata.getApiKey()); Assert.assertEquals(tenant.getExternalKey(), tenantdata.getExternalKey()); // The second time, the value is already in the cache so the TenantCacheLoader is not invoked final Tenant tenant2 = tenantUserApi.getTenantByApiKey(tenantdata.getApiKey()); Assert.assertEquals(tenant2.getApiKey(), tenantdata.getApiKey()); Assert.assertEquals(tenant2.getExternalKey(), tenantdata.getExternalKey()); }
@TimedResource @POST @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) @ApiOperation(value = "Create a tenant", response = TenantJson.class) @ApiResponses(value = {@ApiResponse(code = 201, message = "Tenant created successfully"), @ApiResponse(code = 409, message = "Tenant already exists")}) public Response createTenant(final TenantJson json, @QueryParam(QUERY_TENANT_USE_GLOBAL_DEFAULT) @DefaultValue("false") final Boolean useGlobalDefault, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final HttpServletRequest request, @javax.ws.rs.core.Context final UriInfo uriInfo) throws TenantApiException, CatalogApiException { verifyNonNullOrEmpty(json, "TenantJson body should be specified"); verifyNonNullOrEmpty(json.getApiKey(), "TenantJson apiKey needs to be set", json.getApiSecret(), "TenantJson apiSecret needs to be set"); final TenantData data = json.toTenantData(); final Tenant tenant = tenantApi.createTenant(data, context.createCallContextNoAccountId(createdBy, reason, comment, request)); if (!useGlobalDefault) { final CallContext callContext = new DefaultCallContext(null, tenant.getId(), createdBy, CallOrigin.EXTERNAL, UserType.CUSTOMER, Context.getOrCreateUserToken(), clock); catalogUserApi.createDefaultEmptyCatalog(null, callContext); } return uriBuilder.buildResponse(uriInfo, TenantResource.class, "getTenant", tenant.getId(), request); }
@Override public Tenant createTenant(final TenantData data, final CallContext context) throws TenantApiException { final Tenant tenant = new DefaultTenant(data); if (null != tenant.getExternalKey() && tenant.getExternalKey().length() > 255) { throw new TenantApiException(ErrorCode.EXTERNAL_KEY_LIMIT_EXCEEDED); } try { // Not transactional, but there is a db constraint on that column if (data.getApiKey() != null && getTenantByApiKey(data.getApiKey()) != null) { throw new TenantApiException(ErrorCode.TENANT_ALREADY_EXISTS, data.getExternalKey()); } } catch (final RuntimeException e) { if (e.getCause() instanceof IllegalStateException) { // could happen exemption, stating that the key is not found } else { throw e; } } try { tenantDao.create(new TenantModelDao(tenant), internalCallContextFactory.createInternalCallContextWithoutAccountRecordId(context)); } catch (final TenantApiException e) { throw new TenantApiException(e, ErrorCode.TENANT_CREATION_FAILED); } return tenant; }
public TenantModelDao(final Tenant tenant) { this(tenant.getId(), tenant.getCreatedDate(), tenant.getUpdatedDate(), tenant.getExternalKey(), tenant.getApiKey(), tenant.getApiSecret(), null); }
public TenantContext createTenantContextWithAccountId(final UUID accountId, final ServletRequest request) { final TenantContext tenantContext; final Tenant tenant = getTenantFromRequest(request); if (tenant == null) { // Multi-tenancy may not have been configured - default to "default" tenant (see InternalCallContextFactory) tenantContext = contextFactory.createTenantContext(accountId, null); } else { tenantContext = contextFactory.createTenantContext(accountId, tenant.getId()); } populateMDCContext(tenantContext); return tenantContext; }
private void setupTenant() throws TenantApiException { final UUID uuid = UUID.randomUUID(); final String externalKey = uuid.toString(); final String apiKey = externalKey + "-Key"; final String apiSecret = externalKey + "-$3cr3t"; // Only place where we use callContext tenant = tenantUserApi.createTenant(new DefaultTenant(uuid, init, init, externalKey, apiKey, apiSecret), callContext); testCallContext = new DefaultCallContext(null, tenant.getId(), "tester", CallOrigin.EXTERNAL, UserType.TEST, "good reason", "trust me", uuid, clock); }
@Test(groups = "slow") public void testTenantKVChange() throws Exception { final TenantData tenantData = new DefaultTenant(null, clock.getUTCNow(), clock.getUTCNow(), "MY_TENANT", "key", "s3Cr3T"); final CallContext contextWithNoTenant = new DefaultCallContext(null, null, "loulou", CallOrigin.EXTERNAL, UserType.ADMIN, "no reason", "hum", UUID.randomUUID(), clock); final Tenant tenant = tenantUserApi.createTenant(tenantData, contextWithNoTenant); final CallContext contextWithTenant = new DefaultCallContext(null, tenant.getId(), "loulou", CallOrigin.EXTERNAL, UserType.ADMIN, "no reason", "hum", UUID.randomUUID(), clock); final String tenantKey = TenantKey.PLUGIN_CONFIG_ + "FOO"; tenantUserApi.addTenantKeyValue(tenantKey, "FOO", contextWithTenant); await().atMost(10, SECONDS).until(new Callable<Boolean>() { @Override public Boolean call() throws Exception { // expecting TENANT_CONFIG_CHANGE return externalBusCount.get() == 1; } }); }