use of org.eclipse.hono.service.management.tenant.TenantDto in project hono by eclipse.
the class MongoDbBasedTenantDao method create.
/**
* {@inheritDoc}
*/
@Override
public Future<String> create(final TenantDto tenantConfig, final SpanContext tracingContext) {
Objects.requireNonNull(tenantConfig);
final Span span = tracer.buildSpan("create Tenant").addReference(References.CHILD_OF, tracingContext).withTag(TracingHelper.TAG_TENANT_ID, tenantConfig.getTenantId()).start();
final JsonObject newTenantDtoJson = JsonObject.mapFrom(tenantConfig);
if (LOG.isTraceEnabled()) {
LOG.trace("creating tenant:{}{}", System.lineSeparator(), newTenantDtoJson.encodePrettily());
}
return validateTrustAnchors(tenantConfig, span).compose(ok -> mongoClient.insert(collectionName, newTenantDtoJson)).map(tenantObjectIdResult -> {
LOG.debug("successfully created tenant [tenant-id: {}, version: {}]", tenantConfig.getTenantId(), tenantConfig.getVersion());
span.log("successfully created tenant");
return tenantConfig.getVersion();
}).recover(error -> {
if (MongoDbBasedDao.isDuplicateKeyError(error)) {
TracingHelper.logError(span, "tenant already exists");
return Future.failedFuture(new ClientErrorException(tenantConfig.getTenantId(), HttpURLConnection.HTTP_CONFLICT, "tenant already exists"));
} else {
TracingHelper.logError(span, "error creating tenant", error);
return mapError(error);
}
}).onComplete(r -> span.finish());
}
use of org.eclipse.hono.service.management.tenant.TenantDto in project hono by eclipse.
the class MongoDbBasedTenantDao method getBySubjectDn.
/**
* {@inheritDoc}
*/
@Override
public Future<TenantDto> getBySubjectDn(final X500Principal subjectDn, final SpanContext tracingContext) {
Objects.requireNonNull(subjectDn);
final String dn = subjectDn.getName(X500Principal.RFC2253);
final Span span = tracer.buildSpan("get Tenant by subject DN").addReference(References.CHILD_OF, tracingContext).withTag(TracingHelper.TAG_SUBJECT_DN, dn).start();
return mongoClient.findWithOptions(collectionName, MongoDbDocumentBuilder.builder().withCa(dn).document(), new FindOptions().setLimit(2)).map(matchingDocuments -> {
if (matchingDocuments.size() == 0) {
LOG.debug("could not find tenant with matching trust anchor [subject DN: {}]", dn);
throw new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND, "no such tenant");
} else if (matchingDocuments.size() == 1) {
final JsonObject tenantJsonResult = matchingDocuments.get(0);
return TenantDto.forRead(tenantJsonResult.getString(Constants.JSON_FIELD_TENANT_ID), tenantJsonResult.getJsonObject(TenantDto.FIELD_TENANT).mapTo(Tenant.class), tenantJsonResult.getInstant(TenantDto.FIELD_CREATED), tenantJsonResult.getInstant(TenantDto.FIELD_UPDATED_ON), tenantJsonResult.getString(TenantDto.FIELD_VERSION));
} else {
LOG.debug("found multiple tenants with matching trust anchor [subject DN: {}]", dn);
throw new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND, "found multiple tenants with matching trust anchor");
}
}).onFailure(t -> TracingHelper.logError(span, "error retrieving tenant", t)).recover(this::mapError).onComplete(r -> span.finish());
}
use of org.eclipse.hono.service.management.tenant.TenantDto in project hono by eclipse.
the class MongoDbBasedTenantDao method update.
/**
* {@inheritDoc}
*/
@Override
public Future<String> update(final TenantDto newTenantConfig, final Optional<String> resourceVersion, final SpanContext tracingContext) {
Objects.requireNonNull(newTenantConfig);
Objects.requireNonNull(resourceVersion);
final Span span = tracer.buildSpan("update Tenant").addReference(References.CHILD_OF, tracingContext).withTag(TracingHelper.TAG_TENANT_ID, newTenantConfig.getTenantId()).start();
resourceVersion.ifPresent(v -> TracingHelper.TAG_RESOURCE_VERSION.set(span, v));
final JsonObject updateTenantQuery = MongoDbDocumentBuilder.builder().withVersion(resourceVersion).withTenantId(newTenantConfig.getTenantId()).document();
return validateTrustAnchors(newTenantConfig, span).compose(ok -> mongoClient.findOneAndReplaceWithOptions(collectionName, updateTenantQuery, JsonObject.mapFrom(newTenantConfig), new FindOptions(), new UpdateOptions().setReturningNewDocument(true))).compose(updateResult -> {
if (updateResult == null) {
return MongoDbBasedDao.checkForVersionMismatchAndFail(newTenantConfig.getTenantId(), resourceVersion, getById(newTenantConfig.getTenantId(), false, span));
} else {
LOG.debug("successfully updated tenant [tenant-id: {}]", newTenantConfig.getTenantId());
span.log("successfully updated tenant");
return Future.succeededFuture(updateResult.getString(TenantDto.FIELD_VERSION));
}
}).recover(error -> {
if (MongoDbBasedDao.isDuplicateKeyError(error)) {
LOG.debug("failed to update tenant [{}], tenant alias already in use", newTenantConfig.getTenantId(), error);
final var exception = new ClientErrorException(newTenantConfig.getTenantId(), HttpURLConnection.HTTP_CONFLICT, "tenant alias already in use");
TracingHelper.logError(span, exception);
return Future.failedFuture(exception);
} else {
TracingHelper.logError(span, "error updating tenant", error);
return mapError(error);
}
}).onComplete(r -> span.finish());
}
use of org.eclipse.hono.service.management.tenant.TenantDto in project hono by eclipse.
the class MongoDbBasedTenantManagementService method processCreateTenant.
@Override
protected Future<OperationResult<Id>> processCreateTenant(final String tenantId, final Tenant tenantObj, final Span span) {
Objects.requireNonNull(tenantId);
Objects.requireNonNull(tenantObj);
Objects.requireNonNull(span);
final TenantDto tenantDto = TenantDto.forCreation(tenantId, tenantObj, DeviceRegistryUtils.getUniqueIdentifier());
return dao.create(tenantDto, span.context()).map(resourceVersion -> {
return OperationResult.ok(HttpURLConnection.HTTP_CREATED, Id.of(tenantId), Optional.empty(), Optional.of(resourceVersion));
});
}
Aggregations