use of org.eclipse.hono.service.management.Id in project hono by eclipse.
the class DelegatingTenantManagementHttpEndpoint method createTenant.
private void createTenant(final RoutingContext ctx) {
final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_CREATE_TENANT, getClass().getSimpleName()).start();
final Future<String> tenantId = getRequestParameter(ctx, PARAM_TENANT_ID, getPredicate(config.getTenantIdPattern(), true));
final Future<Tenant> payload = fromPayload(ctx);
CompositeFuture.all(tenantId, payload).compose(ok -> {
final Optional<String> tid = Optional.ofNullable(tenantId.result());
tid.ifPresent(s -> TracingHelper.TAG_TENANT_ID.set(span, s));
logger.debug("creating tenant [{}]", tid.orElse("<auto>"));
return getService().createTenant(tid, payload.result(), span);
}).onSuccess(operationResult -> writeResponse(ctx, operationResult, (responseHeaders, status) -> {
if (status == HttpURLConnection.HTTP_CREATED) {
Optional.ofNullable(operationResult.getPayload()).map(Id::getId).ifPresent(id -> {
final String location = String.format("/%s/%s", getName(), id);
responseHeaders.set(HttpHeaders.LOCATION, location);
});
}
}, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
use of org.eclipse.hono.service.management.Id in project hono by eclipse.
the class DelegatingDeviceManagementHttpEndpoint method doGetDevice.
private void doGetDevice(final RoutingContext ctx) {
final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_GET_DEVICE, getClass().getSimpleName()).start();
final Future<String> tenantId = getRequestParameter(ctx, PARAM_TENANT_ID, getPredicate(config.getTenantIdPattern(), false));
final Future<String> deviceId = getRequestParameter(ctx, PARAM_DEVICE_ID, getPredicate(config.getDeviceIdPattern(), false));
CompositeFuture.all(tenantId, deviceId).compose(ok -> {
TracingHelper.setDeviceTags(span, tenantId.result(), deviceId.result());
logger.debug("retrieving device [tenant: {}, device-id: {}]", tenantId.result(), deviceId.result());
return getService().readDevice(tenantId.result(), deviceId.result(), span);
}).onSuccess(operationResult -> writeResponse(ctx, operationResult, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
use of org.eclipse.hono.service.management.Id in project hono by eclipse.
the class DelegatingDeviceManagementHttpEndpoint method doUpdateDevice.
private void doUpdateDevice(final RoutingContext ctx) {
final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_UPDATE_DEVICE, getClass().getSimpleName()).start();
final Future<String> tenantId = getRequestParameter(ctx, PARAM_TENANT_ID, getPredicate(config.getTenantIdPattern(), false));
final Future<String> deviceId = getRequestParameter(ctx, PARAM_DEVICE_ID, getPredicate(config.getDeviceIdPattern(), false));
final Future<Device> device = fromPayload(ctx);
CompositeFuture.all(tenantId, deviceId, device).compose(ok -> {
TracingHelper.setDeviceTags(span, tenantId.result(), deviceId.result());
logger.debug("updating device [tenant: {}, device-id: {}]", tenantId.result(), deviceId.result());
final Optional<String> resourceVersion = Optional.ofNullable(ctx.get(KEY_RESOURCE_VERSION));
return getService().updateDevice(tenantId.result(), deviceId.result(), device.result(), resourceVersion, span);
}).onSuccess(operationResult -> writeResponse(ctx, operationResult, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
use of org.eclipse.hono.service.management.Id in project hono by eclipse.
the class DelegatingDeviceManagementHttpEndpoint method doDeleteDevice.
private void doDeleteDevice(final RoutingContext ctx) {
final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_REMOVE_DEVICE, getClass().getSimpleName()).start();
final Future<String> tenantId = getRequestParameter(ctx, PARAM_TENANT_ID, getPredicate(config.getTenantIdPattern(), false));
final Future<String> deviceId = getRequestParameter(ctx, PARAM_DEVICE_ID, getPredicate(config.getDeviceIdPattern(), false));
CompositeFuture.all(tenantId, deviceId).compose(ok -> {
TracingHelper.setDeviceTags(span, tenantId.result(), deviceId.result());
logger.debug("removing device [tenant: {}, device-id: {}]", tenantId.result(), deviceId.result());
final Optional<String> resourceVersion = Optional.ofNullable(ctx.get(KEY_RESOURCE_VERSION));
return getService().deleteDevice(tenantId.result(), deviceId.result(), resourceVersion, span);
}).onSuccess(result -> writeResponse(ctx, result, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
use of org.eclipse.hono.service.management.Id in project hono by eclipse.
the class AbstractTenantManagementService method createTenant.
@Override
public final Future<OperationResult<Id>> createTenant(final Optional<String> tenantId, final Tenant tenantObj, final Span span) {
Objects.requireNonNull(tenantId);
Objects.requireNonNull(tenantObj);
Objects.requireNonNull(span);
final Promise<Void> tenantCheck = Promise.promise();
try {
tenantObj.assertTrustAnchorIdUniquenessAndCreateMissingIds();
tenantCheck.complete();
} catch (final IllegalStateException e) {
log.debug("error creating tenant", e);
TracingHelper.logError(span, e);
tenantCheck.fail(new ClientErrorException(tenantId.orElse("N/A"), HttpURLConnection.HTTP_BAD_REQUEST, e.getMessage()));
}
final String tenantIdValue = tenantId.orElseGet(this::createId);
return tenantCheck.future().compose(ok -> processCreateTenant(tenantIdValue, tenantObj, span)).onSuccess(result -> NotificationEventBusSupport.sendNotification(vertx, new TenantChangeNotification(LifecycleChange.CREATE, tenantIdValue, Instant.now(), tenantObj.isEnabled()))).recover(t -> DeviceRegistryUtils.mapError(t, tenantId.get()));
}
Aggregations