use of org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride in project pulsar by apache.
the class BrokerServiceAutoSubscriptionCreationTest method testAutoSubscriptionCreationNamespaceAllowOverridesBroker.
@Test
public void testAutoSubscriptionCreationNamespaceAllowOverridesBroker() throws Exception {
final String topic = "persistent://prop/ns-abc/test-subtopic-" + testId.getAndIncrement();
final String subscriptionName = "test-subtopic-sub-2";
final TopicName topicName = TopicName.get(topic);
admin.topics().createNonPartitionedTopic(topicName.toString());
pulsar.getConfiguration().setAllowAutoSubscriptionCreation(false);
AutoSubscriptionCreationOverride autoSubscriptionCreationOverride = AutoSubscriptionCreationOverride.builder().allowAutoSubscriptionCreation(true).build();
pulsar.getAdminClient().namespaces().setAutoSubscriptionCreation(topicName.getNamespace(), autoSubscriptionCreationOverride);
Assert.assertEquals(pulsar.getAdminClient().namespaces().getAutoSubscriptionCreation(topicName.getNamespace()), autoSubscriptionCreationOverride);
// Subscribe operation should be successful
pulsarClient.newConsumer().topic(topicName.toString()).subscriptionName(subscriptionName).subscribe();
assertTrue(admin.topics().getSubscriptions(topicName.toString()).contains(subscriptionName));
}
use of org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride in project pulsar by apache.
the class BrokerServiceAutoSubscriptionCreationTest method testAutoSubscriptionCreationNamespaceDisallowOverridesBroker.
@Test
public void testAutoSubscriptionCreationNamespaceDisallowOverridesBroker() throws Exception {
final String topic = "persistent://prop/ns-abc/test-subtopic-" + testId.getAndIncrement();
final String subscriptionName = "test-subtopic-sub-3";
final TopicName topicName = TopicName.get(topic);
admin.topics().createNonPartitionedTopic(topicName.toString());
pulsar.getConfiguration().setAllowAutoSubscriptionCreation(true);
AutoSubscriptionCreationOverride autoSubscriptionCreationOverride = AutoSubscriptionCreationOverride.builder().allowAutoSubscriptionCreation(false).build();
pulsar.getAdminClient().namespaces().setAutoSubscriptionCreation(topicName.getNamespace(), autoSubscriptionCreationOverride);
Assert.assertEquals(pulsar.getAdminClient().namespaces().getAutoSubscriptionCreation(topicName.getNamespace()), autoSubscriptionCreationOverride);
try {
pulsarClient.newConsumer().topic(topicName.toString()).subscriptionName(subscriptionName).subscribe();
fail("Subscribe operation should have failed");
} catch (Exception e) {
assertTrue(e instanceof PulsarClientException);
}
assertFalse(admin.topics().getSubscriptions(topicName.toString()).contains(subscriptionName));
}
use of org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride in project pulsar by apache.
the class NamespacesBase method internalSetAutoSubscriptionCreation.
protected void internalSetAutoSubscriptionCreation(AsyncResponse asyncResponse, AutoSubscriptionCreationOverride autoSubscriptionCreationOverride) {
validateNamespacePolicyOperation(namespaceName, PolicyName.AUTO_SUBSCRIPTION_CREATION, PolicyOperation.WRITE);
validatePoliciesReadOnlyAccess();
// Force to read the data s.t. the watch to the cache content is setup.
namespaceResources().setPoliciesAsync(namespaceName, policies -> {
policies.autoSubscriptionCreationOverride = autoSubscriptionCreationOverride;
return policies;
}).thenApply(r -> {
if (autoSubscriptionCreationOverride != null) {
String autoOverride = autoSubscriptionCreationOverride.isAllowAutoSubscriptionCreation() ? "enabled" : "disabled";
log.info("[{}] Successfully {} autoSubscriptionCreation on namespace {}", clientAppId(), autoOverride != null ? autoOverride : "removed", namespaceName);
}
asyncResponse.resume(Response.noContent().build());
return null;
}).exceptionally(e -> {
log.error("[{}] Failed to modify autoSubscriptionCreation 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.AutoSubscriptionCreationOverride in project pulsar by yahoo.
the class BrokerServiceAutoSubscriptionCreationTest method testAutoSubscriptionCreationNamespaceDisallowOverridesBroker.
@Test
public void testAutoSubscriptionCreationNamespaceDisallowOverridesBroker() throws Exception {
final String topic = "persistent://prop/ns-abc/test-subtopic-" + testId.getAndIncrement();
final String subscriptionName = "test-subtopic-sub-3";
final TopicName topicName = TopicName.get(topic);
admin.topics().createNonPartitionedTopic(topicName.toString());
pulsar.getConfiguration().setAllowAutoSubscriptionCreation(true);
AutoSubscriptionCreationOverride autoSubscriptionCreationOverride = AutoSubscriptionCreationOverride.builder().allowAutoSubscriptionCreation(false).build();
pulsar.getAdminClient().namespaces().setAutoSubscriptionCreation(topicName.getNamespace(), autoSubscriptionCreationOverride);
Assert.assertEquals(pulsar.getAdminClient().namespaces().getAutoSubscriptionCreation(topicName.getNamespace()), autoSubscriptionCreationOverride);
try {
pulsarClient.newConsumer().topic(topicName.toString()).subscriptionName(subscriptionName).subscribe();
fail("Subscribe operation should have failed");
} catch (Exception e) {
assertTrue(e instanceof PulsarClientException);
}
assertFalse(admin.topics().getSubscriptions(topicName.toString()).contains(subscriptionName));
}
use of org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride in project pulsar by yahoo.
the class NamespacesBase method internalSetAutoSubscriptionCreation.
protected void internalSetAutoSubscriptionCreation(AsyncResponse asyncResponse, AutoSubscriptionCreationOverride autoSubscriptionCreationOverride) {
validateNamespacePolicyOperation(namespaceName, PolicyName.AUTO_SUBSCRIPTION_CREATION, PolicyOperation.WRITE);
validatePoliciesReadOnlyAccess();
// Force to read the data s.t. the watch to the cache content is setup.
namespaceResources().setPoliciesAsync(namespaceName, policies -> {
policies.autoSubscriptionCreationOverride = autoSubscriptionCreationOverride;
return policies;
}).thenApply(r -> {
if (autoSubscriptionCreationOverride != null) {
String autoOverride = autoSubscriptionCreationOverride.isAllowAutoSubscriptionCreation() ? "enabled" : "disabled";
log.info("[{}] Successfully {} autoSubscriptionCreation on namespace {}", clientAppId(), autoOverride != null ? autoOverride : "removed", namespaceName);
}
asyncResponse.resume(Response.noContent().build());
return null;
}).exceptionally(e -> {
log.error("[{}] Failed to modify autoSubscriptionCreation 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;
});
}
Aggregations