use of org.apache.pulsar.common.policies.data.AutoTopicCreationOverride in project pulsar by apache.
the class NamespacesBase method internalSetAutoTopicCreation.
protected void internalSetAutoTopicCreation(AsyncResponse asyncResponse, AutoTopicCreationOverride autoTopicCreationOverride) {
final int maxPartitions = pulsar().getConfig().getMaxNumPartitionsPerPartitionedTopic();
validateNamespacePolicyOperation(namespaceName, PolicyName.AUTO_TOPIC_CREATION, PolicyOperation.WRITE);
validatePoliciesReadOnlyAccess();
if (autoTopicCreationOverride != null) {
if (!AutoTopicCreationOverrideImpl.isValidOverride(autoTopicCreationOverride)) {
throw new RestException(Status.PRECONDITION_FAILED, "Invalid configuration for autoTopicCreationOverride");
}
if (maxPartitions > 0 && autoTopicCreationOverride.getDefaultNumPartitions() > maxPartitions) {
throw new RestException(Status.NOT_ACCEPTABLE, "Number of partitions should be less than or equal to " + maxPartitions);
}
}
// Force to read the data s.t. the watch to the cache content is setup.
namespaceResources().setPoliciesAsync(namespaceName, policies -> {
policies.autoTopicCreationOverride = autoTopicCreationOverride;
return policies;
}).thenApply(r -> {
String autoOverride = (autoTopicCreationOverride != null && autoTopicCreationOverride.isAllowAutoTopicCreation()) ? "enabled" : "disabled";
log.info("[{}] Successfully {} autoTopicCreation on namespace {}", clientAppId(), autoOverride != null ? autoOverride : "removed", namespaceName);
asyncResponse.resume(Response.noContent().build());
return null;
}).exceptionally(e -> {
log.error("[{}] Failed to modify autoTopicCreation status on namespace {}", clientAppId(), namespaceName, e.getCause());
if (e.getCause() instanceof NotFoundException) {
asyncResponse.resume(new RestException(Status.NOT_FOUND, "Namespace does not exist"));
return null;
}
asyncResponse.resume(new RestException(e.getCause()));
return null;
});
}
use of org.apache.pulsar.common.policies.data.AutoTopicCreationOverride in project pulsar by apache.
the class NamespacesImpl method getAutoTopicCreationAsync.
@Override
public CompletableFuture<AutoTopicCreationOverride> getAutoTopicCreationAsync(String namespace) {
NamespaceName ns = NamespaceName.get(namespace);
WebTarget path = namespacePath(ns, "autoTopicCreation");
final CompletableFuture<AutoTopicCreationOverride> future = new CompletableFuture<>();
asyncGetRequest(path, new InvocationCallback<AutoTopicCreationOverride>() {
@Override
public void completed(AutoTopicCreationOverride autoTopicCreationOverride) {
future.complete(autoTopicCreationOverride);
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
use of org.apache.pulsar.common.policies.data.AutoTopicCreationOverride in project pulsar by apache.
the class NamespacesImpl method setAutoTopicCreationAsync.
@Override
public CompletableFuture<Void> setAutoTopicCreationAsync(String namespace, AutoTopicCreationOverride autoTopicCreationOverride) {
NamespaceName ns = NamespaceName.get(namespace);
WebTarget path = namespacePath(ns, "autoTopicCreation");
return asyncPostRequest(path, Entity.entity(autoTopicCreationOverride, MediaType.APPLICATION_JSON));
}
use of org.apache.pulsar.common.policies.data.AutoTopicCreationOverride in project mop by streamnative.
the class MQTT5TestTopicAutoCreation method testSubscribeAllowTopicCreationByNamespacePolicy.
@Test(timeOut = TIMEOUT)
public void testSubscribeAllowTopicCreationByNamespacePolicy() throws PulsarAdminException {
AutoTopicCreationOverride param = AutoTopicCreationOverride.builder().topicType(TopicType.NON_PARTITIONED.toString()).allowAutoTopicCreation(true).build();
admin.namespaces().setAutoTopicCreation("public/default", param);
Mqtt5BlockingClient client = MQTT5ClientUtils.createMqtt5Client(getMqttBrokerPortList().get(0));
client.connect();
try {
client.subscribeWith().topicFilter("topic-not-allow-auto-creation-subscribe").qos(MqttQos.AT_LEAST_ONCE).send();
} catch (Mqtt5SubAckException ex) {
Assert.fail("Unexpected result");
}
client.disconnect();
}
use of org.apache.pulsar.common.policies.data.AutoTopicCreationOverride in project pulsar by yahoo.
the class Namespaces method setAutoTopicCreation.
@POST
@Path("/{tenant}/{namespace}/autoTopicCreation")
@ApiOperation(value = "Override broker's allowAutoTopicCreation setting for a namespace")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Tenant or cluster or namespace doesn't exist"), @ApiResponse(code = 406, message = "The number of partitions should be less than or" + " equal to maxNumPartitionsPerPartitionedTopic"), @ApiResponse(code = 400, message = "Invalid autoTopicCreation override") })
public void setAutoTopicCreation(@Suspended final AsyncResponse asyncResponse, @PathParam("tenant") String tenant, @PathParam("namespace") String namespace, @ApiParam(value = "Settings for automatic topic creation", required = true) AutoTopicCreationOverride autoTopicCreationOverride) {
validateNamespaceName(tenant, namespace);
internalSetAutoTopicCreationAsync(autoTopicCreationOverride).thenAccept(__ -> {
String autoOverride = (autoTopicCreationOverride != null && autoTopicCreationOverride.isAllowAutoTopicCreation()) ? "enabled" : "disabled";
log.info("[{}] Successfully {} autoTopicCreation on namespace {}", clientAppId(), autoOverride, namespaceName);
asyncResponse.resume(Response.noContent().build());
}).exceptionally(e -> {
Throwable ex = FutureUtil.unwrapCompletionException(e);
log.error("[{}] Failed to set autoTopicCreation status on namespace {}", clientAppId(), namespaceName, ex);
if (ex instanceof MetadataStoreException.NotFoundException) {
asyncResponse.resume(new RestException(Response.Status.NOT_FOUND, "Namespace does not exist"));
} else {
resumeAsyncResponseExceptionally(asyncResponse, ex);
}
return null;
});
}
Aggregations