org.apache.pulsar.client.admin.PulsarAdminException Java Examples

The following examples show how to use org.apache.pulsar.client.admin.PulsarAdminException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseResource.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public <T> CompletableFuture<Void> asyncPutRequest(final WebTarget target, Entity<T> entity) {
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        request(target).async().put(entity, new InvocationCallback<ErrorData>() {

            @Override
            public void completed(ErrorData response) {
                future.complete(null);
            }

            @Override
            public void failed(Throwable throwable) {
                log.warn("[{}] Failed to perform http put request: {}", target.getUri(), throwable.getMessage());
                future.completeExceptionally(getApiException(throwable.getCause()));
            }

        });
    } catch (PulsarAdminException cae) {
        future.completeExceptionally(cae);
    }
    return future;
}
 
Example #2
Source File: ZooKeeperSessionExpireRecoveryTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
/**
 * Verify we are able to recover when receiving a SessionExpired event on global ZK session
 */
@Test
public void testSessionExpired() throws Exception {
    admin.clusters().createCluster("my-cluster", new ClusterData("test-url"));

    assertTrue(Sets.newHashSet(admin.clusters().getClusters()).contains("my-cluster"));

    mockZooKeeper.failConditional(Code.SESSIONEXPIRED, (op, path) -> {
            return op == MockZooKeeper.Op.CREATE
                && path.equals("/admin/clusters/my-cluster-2");
        });

    assertTrue(Sets.newHashSet(admin.clusters().getClusters()).contains("my-cluster"));

    try {
        admin.clusters().createCluster("my-cluster-2", new ClusterData("test-url"));
        fail("Should have failed, because global zk is down");
    } catch (PulsarAdminException e) {
        // Ok
    }

    admin.clusters().createCluster("cluster-2", new ClusterData("test-url"));
}
 
Example #3
Source File: PersistentTopicsTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetPartitionedTopicsList() throws KeeperException, InterruptedException, PulsarAdminException {
    AsyncResponse response = mock(AsyncResponse.class);
    ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
    persistentTopics.createPartitionedTopic(response, testTenant, testNamespace, "test-topic1", 3);
    verify(response, timeout(5000).times(1)).resume(responseCaptor.capture());
    Assert.assertEquals(responseCaptor.getValue().getStatus(), Response.Status.NO_CONTENT.getStatusCode());

    response = mock(AsyncResponse.class);
    responseCaptor = ArgumentCaptor.forClass(Response.class);
    nonPersistentTopic.createPartitionedTopic(response, testTenant, testNamespace, "test-topic2", 3);
    verify(response, timeout(5000).times(1)).resume(responseCaptor.capture());
    Assert.assertEquals(responseCaptor.getValue().getStatus(), Response.Status.NO_CONTENT.getStatusCode());

    List<String> persistentPartitionedTopics = persistentTopics.getPartitionedTopicList(testTenant, testNamespace);

    Assert.assertEquals(persistentPartitionedTopics.size(), 1);
    Assert.assertEquals(TopicName.get(persistentPartitionedTopics.get(0)).getDomain().value(), TopicDomain.persistent.value());

    List<String> nonPersistentPartitionedTopics = nonPersistentTopic.getPartitionedTopicList(testTenant, testNamespace);
    Assert.assertEquals(nonPersistentPartitionedTopics.size(), 1);
    Assert.assertEquals(TopicName.get(nonPersistentPartitionedTopics.get(0)).getDomain().value(), TopicDomain.non_persistent.value());
}
 
Example #4
Source File: SchemasImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Void> deleteSchemaAsync(String topic) {
    TopicName tn = TopicName.get(topic);
    final CompletableFuture<Void> future = new CompletableFuture<>();
    try {
        request(schemaPath(tn)).async().delete(new InvocationCallback<DeleteSchemaResponse>() {
            @Override
            public void completed(DeleteSchemaResponse deleteSchemaResponse) {
                future.complete(null);
            }

            @Override
            public void failed(Throwable throwable) {
                future.completeExceptionally(getApiException(throwable.getCause()));
            }
        });
    } catch (PulsarAdminException cae) {
        future.completeExceptionally(cae);
    }
    return future;
}
 
Example #5
Source File: PulsarMetadataReader.java    From pulsar-flink with Apache License 2.0 6 votes vote down vote up
public List<String> getTopics() throws PulsarAdminException {
    for (Map.Entry<String, String> e : caseInsensitiveParams.entrySet()) {
        if (PulsarOptions.TOPIC_OPTION_KEYS.contains(e.getKey())) {
            String key = e.getKey();
            if (key.equals("topic")) {
                return Collections.singletonList(TopicName.get(e.getValue()).toString());
            } else if (key.equals("topics")) {
                return Arrays.asList(e.getValue().split(",")).stream()
                        .filter(s -> !s.isEmpty())
                        .map(t -> TopicName.get(t).toString())
                        .collect(Collectors.toList());
            } else { // topicspattern
                return getTopicsWithPattern(e.getValue());
            }
        }
    }
    return null;
}
 
Example #6
Source File: TopicsServiceImplTest.java    From pulsar-manager with Apache License 2.0 6 votes vote down vote up
@Test
public void topicsServiceImplTest() throws PulsarAdminException {
    Mockito.when(pulsarAdminService.topics("http://localhost:8080")).thenReturn(topics);
    Mockito.when(topics.getList("public/default")).thenReturn(
            Arrays.asList(
                    "persistent://public/default/test789",
                    "persistent://public/default/test900-partition-0",
                    "persistent://public/default/test900-partition-1",
                    "persistent://public/default/test900-partition-2"
            )
    );
    Mockito.when(topics.getPartitionedTopicList("public/default")).thenReturn(
            Arrays.asList(
                    "persistent://public/default/test900"
            )
    );
    Mockito.when(topics.getPartitionedTopicMetadata("persistent://public/default/test900")).thenReturn(
            new PartitionedTopicMetadata(3)
    );
    Map<String, Object> topicsMap = topicsService.getTopicsList(
            1, 1, "public", "default", "http://localhost:8080");
    Assert.assertEquals(2, topicsMap.get("total"));
    Assert.assertFalse((Boolean) topicsMap.get("isPage"));
    Assert.assertEquals("[{partitions=0, topic=test789, persistent=persistent}, {partitions=3, topic=test900, persistent=persistent}]", topicsMap.get("topics").toString());
}
 
Example #7
Source File: CmdResourceQuotas.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
void run() throws PulsarAdminException {
    ResourceQuota quota = new ResourceQuota();
    quota.setMsgRateIn(msgRateIn);
    quota.setMsgRateOut(msgRateOut);
    quota.setBandwidthIn(bandwidthIn);
    quota.setBandwidthOut(bandwidthOut);
    quota.setMemory(memory);
    quota.setDynamic(dynamic);

    if (bundle == null && names == null) {
        admin.resourceQuotas().setDefaultResourceQuota(quota);
    } else if (bundle != null && names != null) {
        String namespace = validateNamespace(names);
        admin.resourceQuotas().setNamespaceBundleResourceQuota(namespace, bundle, quota);
    } else {
        throw new ParameterException("namespace and bundle must be provided together.");
    }
}
 
Example #8
Source File: SimpleSchemaTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetSchemaByVersion() throws PulsarClientException, PulsarAdminException, ExecutionException, InterruptedException {
    final String topic = "persistent://my-property/my-ns/testGetSchemaByVersion";

    PulsarClientImpl httpProtocolClient = (PulsarClientImpl) PulsarClient.builder().serviceUrl(brokerUrl.toString()).build();
    PulsarClientImpl binaryProtocolClient = (PulsarClientImpl) pulsarClient;

    pulsarClient.newProducer(Schema.AVRO(V1Data.class))
        .topic(topic)
        .create();

    pulsarClient.newProducer(Schema.AVRO(V2Data.class))
        .topic(topic)
        .create();

    LookupService httpLookupService = httpProtocolClient.getLookup();
    LookupService binaryLookupService = binaryProtocolClient.getLookup();
    Assert.assertTrue(httpLookupService instanceof HttpLookupService);
    Assert.assertTrue(binaryLookupService instanceof BinaryProtoLookupService);
    Assert.assertEquals(admin.schemas().getAllSchemas(topic).size(), 2);
    Assert.assertTrue(httpLookupService.getSchema(TopicName.get(topic), ByteBuffer.allocate(8).putLong(0).array()).get().isPresent());
    Assert.assertTrue(httpLookupService.getSchema(TopicName.get(topic), ByteBuffer.allocate(8).putLong(1).array()).get().isPresent());
    Assert.assertTrue(binaryLookupService.getSchema(TopicName.get(topic), ByteBuffer.allocate(8).putLong(0).array()).get().isPresent());
    Assert.assertTrue(binaryLookupService.getSchema(TopicName.get(topic), ByteBuffer.allocate(8).putLong(1).array()).get().isPresent());
}
 
Example #9
Source File: EnvironmentCacheServiceImplTest.java    From pulsar-manager with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmptyEnvironment() throws PulsarAdminException {
    environmentsRepository.save(emptyEnvironment);
    PulsarAdminException pulsarAdminException = new PulsarAdminException("Cluster does not exist");
    Mockito.when(emptyClusters.getCluster(cluster1_0_name)).thenThrow(pulsarAdminException);
    environmentCacheService.reloadEnvironments();

    try {
        environmentCacheService.getServiceUrl(emptyEnvironment.getName(), cluster1_0_name);
        fail("Should fail to get service url if environments is empty");
    } catch (RuntimeException e) {
        // expected
        assertEquals(
            "No cluster '" + cluster1_0_name + "' found in environment '"
                + emptyEnvironment.getName() + "'",
            e.getMessage());
    }
}
 
Example #10
Source File: BaseResource.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public PulsarAdminException getApiException(Response response) {
    if (response.getStatusInfo().equals(Response.Status.OK)) {
        return null;
    }
    try {
        if (response.getStatus() >= 500) {
            throw new ServerErrorException(response);
        } else if (response.getStatus() >= 400) {
            throw new ClientErrorException(response);
        } else {
            throw new WebApplicationException(response);
        }
    } catch (Exception e) {
        return getApiException(e);
    }
}
 
Example #11
Source File: BrokersImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<Void> healthcheckAsync() {
    WebTarget path = adminBrokers.path("health");
    final CompletableFuture<Void> future = new CompletableFuture<>();
    asyncGetRequest(path,
            new InvocationCallback<String>() {
                @Override
                public void completed(String result) {
                    if (!"ok".equalsIgnoreCase(result.trim())) {
                        future.completeExceptionally(
                                new PulsarAdminException("Healthcheck returned unexpected result: " + result));
                    } else {
                        future.complete(null);
                    }
                }

                @Override
                public void failed(Throwable throwable) {
                    future.completeExceptionally(getApiException(throwable.getCause()));
                }
            });
    return future;
}
 
Example #12
Source File: CmdNamespaces.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
void run() throws PulsarAdminException {
    String namespace = validateNamespace(params);
    type = type.toLowerCase().trim();

    if (enable == disable) {
        throw new ParameterException("Need to specify either --enable or --disable");
    }
    if (enable) {
        if (!TopicType.isValidTopicType(type)) {
            throw new ParameterException("Must specify type of topic to be created. " +
                    "Possible values: (partitioned, non-partitioned)");
        }

        if (TopicType.PARTITIONED.toString().equals(type) && !(defaultNumPartitions > 0)) {
            throw new ParameterException("Must specify num-partitions > 0 for partitioned topic type.");
        }
    }
    admin.namespaces().setAutoTopicCreation(namespace, new AutoTopicCreationOverride(enable, type, defaultNumPartitions));
}
 
Example #13
Source File: CmdPersistentTopics.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
void run() throws PulsarAdminException {
    String persistentTopic = validatePersistentTopic(params);

    try {
        LongRunningProcessStatus status = persistentTopics.compactionStatus(persistentTopic);
        while (wait && status.status == LongRunningProcessStatus.Status.RUNNING) {
            Thread.sleep(1000);
            status = persistentTopics.compactionStatus(persistentTopic);
        }

        switch (status.status) {
        case NOT_RUN:
            System.out.println("Compaction has not been run for " + persistentTopic
                               + " since broker startup");
            break;
        case RUNNING:
            System.out.println("Compaction is currently running");
            break;
        case SUCCESS:
            System.out.println("Compaction was a success");
            break;
        case ERROR:
            System.out.println("Error in compaction");
            throw new PulsarAdminException("Error compacting: " + status.lastError);
        }
    } catch (InterruptedException e) {
        throw new PulsarAdminException(e);
    }
}
 
Example #14
Source File: PulsarMetadataReader.java    From pulsar-flink with Apache License 2.0 5 votes vote down vote up
public boolean topicExists(ObjectPath objectPath) throws PulsarAdminException {
    String topicName = objectPath2TopicName(objectPath);
    int partitionNum = admin.topics().getPartitionedTopicMetadata(topicName).partitions;
    if (partitionNum > 0) {
        return true;
    } else {
        admin.topics().getStats(topicName);
    }
    return true;
}
 
Example #15
Source File: ClustersServiceImplTest.java    From pulsar-manager with Apache License 2.0 5 votes vote down vote up
@Test
public void getClusterByAnyBroker() throws PulsarAdminException  {
    Mockito.when(pulsarAdminService.clusters("http://localhost:8080")).thenReturn(clusters);
    Mockito.when(pulsarAdminService.clusters("http://localhost:8080").getClusters()).thenReturn(Arrays.asList("standalone"));

    List<String> clusterList = clustersService.getClusterByAnyBroker("http://localhost:8080");
    Assert.assertEquals("standalone", clusterList.get(0));
}
 
Example #16
Source File: EnvironmentCacheServiceImplTest.java    From pulsar-manager with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws PulsarAdminException {
    // setup 3 environments
    environment1 = new EnvironmentEntity();
    environment1.setBroker("http://cluster1_0:8080");
    environment1.setName("environment1");
    environment2 = new EnvironmentEntity();
    environment2.setBroker("http://cluster2_0:8080");
    environment2.setName("environment2");
    emptyEnvironment = new EnvironmentEntity();
    emptyEnvironment.setName("emptyEnvironment");
    emptyEnvironment.setBroker("http://empty_env:8080");

    // setup 3 clusters
    cluster1_0 = new ClusterData();
    cluster1_0.setServiceUrl("http://cluster1_0:8080");

    cluster2_0 = new ClusterData();
    cluster2_0.setServiceUrl("http://cluster2_0:8080");

    cluster2_1 = new ClusterData();
    cluster2_1.setServiceUrl("http://cluster2_1:8080");

    Mockito.when(pulsarAdminService.clusters(emptyEnvironment.getBroker())).thenReturn(emptyClusters);
    Mockito.when(emptyClusters.getClusters()).thenReturn(Arrays.asList());

    Mockito.when(pulsarAdminService.clusters(cluster1_0.getServiceUrl())).thenReturn(cluster1Clusters);
    Mockito.when(cluster1Clusters.getClusters()).thenReturn(Arrays.asList(cluster1_0_name));
    Mockito.when(cluster1Clusters.getCluster(cluster1_0_name)).thenReturn(cluster1_0);

    Mockito.when(pulsarAdminService.clusters(cluster2_0.getServiceUrl())).thenReturn(cluster2Clusters);
    Mockito.when(cluster2Clusters.getClusters()).thenReturn(Arrays.asList(cluster2_0_name, cluster2_1_name));
    Mockito.when(cluster2Clusters.getCluster(cluster2_0_name)).thenReturn(cluster2_0);
    Mockito.when(cluster2Clusters.getCluster(cluster2_1_name)).thenReturn(cluster2_1);
}
 
Example #17
Source File: PulsarCatalog.java    From pulsar-flink with Apache License 2.0 5 votes vote down vote up
@Override
public boolean databaseExists(String databaseName) throws CatalogException {
    try {
        return metadataReader.namespaceExists(databaseName);
    } catch (PulsarAdminException e) {
        return false;
    }
}
 
Example #18
Source File: BrokerStatsImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public JsonObject getBrokerResourceAvailability(String namespace) throws PulsarAdminException {
    try {
        NamespaceName ns = NamespaceName.get(namespace);
        WebTarget admin = ns.isV2() ? adminV2BrokerStats : adminBrokerStats;
        String json = request(admin.path("/broker-resource-availability").path(ns.toString())).get(String.class);
        return new Gson().fromJson(json, JsonObject.class);
    } catch (Exception e) {
        throw getApiException(e);
    }
}
 
Example #19
Source File: PartitionCreationTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "topicDomainProvider")
public void testCreateConsumerForPartitionedTopicWhenEnableTopicAutoCreation(TopicDomain domain) throws PulsarAdminException, PulsarClientException {
    conf.setAllowAutoTopicCreation(true);
    final String topic = domain.value() + "://public/default/testCreateConsumerForPartitionedTopicWhenEnableTopicAutoCreation";
    admin.topics().createPartitionedTopic(topic, 3);
    Assert.assertNotNull(pulsarClient.newConsumer().topic(topic).subscriptionName("sub-1").subscribe());
}
 
Example #20
Source File: FunctionsImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<String> triggerFunctionAsync(
        String tenant, String namespace, String function,
        String topic, String triggerValue, String triggerFile) {
    final FormDataMultiPart mp = new FormDataMultiPart();
    if (triggerFile != null) {
        mp.bodyPart(new FileDataBodyPart("dataStream",
                new File(triggerFile),
                MediaType.APPLICATION_OCTET_STREAM_TYPE));
    }
    if (triggerValue != null) {
        mp.bodyPart(new FormDataBodyPart("data", triggerValue, MediaType.TEXT_PLAIN_TYPE));
    }
    if (topic != null && !topic.isEmpty()) {
        mp.bodyPart(new FormDataBodyPart("topic", topic, MediaType.TEXT_PLAIN_TYPE));
    }
    WebTarget path = functions.path(tenant).path(namespace).path(function).path("trigger");

    final CompletableFuture<String> future = new CompletableFuture<>();
    try {
        request(path).async().post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA),
                new InvocationCallback<String>() {
                    @Override
                    public void completed(String response) {
                        future.complete(response);
                    }

                    @Override
                    public void failed(Throwable throwable) {
                        log.warn("[{}] Failed to perform http post request: {}",
                                path.getUri(), throwable.getMessage());
                        future.completeExceptionally(getApiException(throwable.getCause()));
                    }
                });
    } catch (PulsarAdminException cae) {
        future.completeExceptionally(cae);
    }
    return future;
}
 
Example #21
Source File: CmdTopics.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
void run() throws PulsarAdminException {
    String persistentTopic = validatePersistentTopic(params);

    try {
        LongRunningProcessStatus status = topics.compactionStatus(persistentTopic);
        while (wait && status.status == LongRunningProcessStatus.Status.RUNNING) {
            Thread.sleep(1000);
            status = topics.compactionStatus(persistentTopic);
        }

        switch (status.status) {
        case NOT_RUN:
            System.out.println("Compaction has not been run for " + persistentTopic
                               + " since broker startup");
            break;
        case RUNNING:
            System.out.println("Compaction is currently running");
            break;
        case SUCCESS:
            System.out.println("Compaction was a success");
            break;
        case ERROR:
            System.out.println("Error in compaction");
            throw new PulsarAdminException("Error compacting: " + status.lastError);
        }
    } catch (InterruptedException e) {
        throw new PulsarAdminException(e);
    }
}
 
Example #22
Source File: PulsarMetadataReader.java    From pulsar-flink with Apache License 2.0 5 votes vote down vote up
public boolean namespaceExists(String ns) throws PulsarAdminException {
    try {
        admin.namespaces().getTopics(ns);
    } catch (PulsarAdminException.NotFoundException e) {
        return false;
    }
    return true;
}
 
Example #23
Source File: KafkaProtocolHandler.java    From kop with Apache License 2.0 5 votes vote down vote up
private String createKafkaOffsetsTopic(BrokerService service) throws PulsarServerException, PulsarAdminException {
    String offsetsTopic = kafkaConfig.getKafkaMetadataTenant() + "/" + kafkaConfig.getKafkaMetadataNamespace()
        + "/" + Topic.GROUP_METADATA_TOPIC_NAME;

    PartitionedTopicMetadata offsetsTopicMetadata =
        service.pulsar().getAdminClient().topics().getPartitionedTopicMetadata(offsetsTopic);
    if (offsetsTopicMetadata.partitions <= 0) {
        log.info("Kafka group metadata topic {} doesn't exist. Creating it ...",
                offsetsTopic);
        try {
            service.pulsar().getAdminClient().topics().createPartitionedTopic(
                    offsetsTopic,
                    kafkaConfig.getOffsetsTopicNumPartitions()
            );

            for (int i = 0; i < kafkaConfig.getOffsetsTopicNumPartitions(); i++) {
                service.pulsar().getAdminClient().topics()
                        .createNonPartitionedTopic(offsetsTopic + PARTITIONED_TOPIC_SUFFIX + i);
            }
        } catch (ConflictException e) {
            log.info("Topic {} concurrent creating and cause e: ", offsetsTopic, e);
            return offsetsTopic;
        }

        log.info("Successfully created group metadata topic {} with {} partitions.",
                offsetsTopic, kafkaConfig.getOffsetsTopicNumPartitions());
    }

    return offsetsTopic;
}
 
Example #24
Source File: PulsarContainerTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void shouldNotEnableFunctionsWorkerByDefault() throws Exception {
    try (PulsarContainer pulsar = new PulsarContainer("2.5.1")) {
        pulsar.start();

        PulsarAdmin pulsarAdmin = PulsarAdmin.builder()
            .serviceHttpUrl(pulsar.getHttpServiceUrl())
            .build();

        assertThatThrownBy(() -> pulsarAdmin.functions().getFunctions("public", "default"))
            .isInstanceOf(PulsarAdminException.class);
    }
}
 
Example #25
Source File: TenantsServiceImpl.java    From pulsar-manager with Apache License 2.0 5 votes vote down vote up
public Map<String, String> createTenant(String tenant, String role, String cluster, String requestHost) {
    TenantInfo tenantInfo = new TenantInfo(Sets.newHashSet(role), Sets.newHashSet(cluster));
    Map<String, String> result = Maps.newHashMap();
    try {
        pulsarAdminService.tenants(requestHost).createTenant(tenant, tenantInfo);
        result.put("message", "Create tenant success");
    } catch (PulsarAdminException e) {
        PulsarAdminOperationException pulsarAdminOperationException
                = new PulsarAdminOperationException("Failed to create tenant.");
        log.error(pulsarAdminOperationException.getMessage(), e);
        result.put("error", "Create tenant failed");
    }
    return result;
}
 
Example #26
Source File: CmdNamespaces.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
void run() throws PulsarAdminException {
    String namespace = validateNamespace(params);

    if (enable == disable) {
        throw new ParameterException("Need to specify either --enable or --disable");
    }
    admin.namespaces().setSchemaValidationEnforced(namespace, enable);
}
 
Example #27
Source File: ComponentImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public boolean isAuthorizedRole(String tenant, String namespace, String clientRole,
                                AuthenticationDataSource authenticationData) throws PulsarAdminException {
    if (worker().getWorkerConfig().isAuthorizationEnabled()) {
        // skip authorization if client role is super-user
        if (isSuperUser(clientRole)) {
            return true;
        }

        if (clientRole != null) {
            try {
                TenantInfo tenantInfo = worker().getBrokerAdmin().tenants().getTenantInfo(tenant);
                if (tenantInfo != null && worker().getAuthorizationService().isTenantAdmin(tenant, clientRole, tenantInfo, authenticationData).get()) {
                    return true;
                }
            } catch (PulsarAdminException.NotFoundException | InterruptedException | ExecutionException e) {

            }
        }

        // check if role has permissions granted
        if (clientRole != null && authenticationData != null) {
            return allowFunctionOps(NamespaceName.get(tenant, namespace), clientRole, authenticationData);
        } else {
            return false;
        }
    }
    return true;
}
 
Example #28
Source File: TopicsImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<MessageId> terminateTopicAsync(String topic) {
    TopicName tn = validateTopic(topic);

    final CompletableFuture<MessageId> future = new CompletableFuture<>();
    try {
        final WebTarget path = topicPath(tn, "terminate");

        request(path).async().post(Entity.entity("", MediaType.APPLICATION_JSON),
                new InvocationCallback<MessageIdImpl>() {

                    @Override
                    public void completed(MessageIdImpl messageId) {
                        future.complete(messageId);
                    }

                    @Override
                    public void failed(Throwable throwable) {
                        log.warn("[{}] Failed to perform http post request: {}", path.getUri(),
                                throwable.getMessage());
                        future.completeExceptionally(getApiException(throwable.getCause()));
                    }
                });
    } catch (PulsarAdminException cae) {
        future.completeExceptionally(cae);
    }

    return future;
}
 
Example #29
Source File: CmdTopics.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
void run() throws PulsarAdminException {
    String persistentTopic = validatePersistentTopic(params);

    try {
        LongRunningProcessStatus status = topics.offloadStatus(persistentTopic);
        while (wait && status.status == LongRunningProcessStatus.Status.RUNNING) {
            Thread.sleep(1000);
            status = topics.offloadStatus(persistentTopic);
        }

        switch (status.status) {
        case NOT_RUN:
            System.out.println("Offload has not been run for " + persistentTopic
                               + " since broker startup");
            break;
        case RUNNING:
            System.out.println("Offload is currently running");
            break;
        case SUCCESS:
            System.out.println("Offload was a success");
            break;
        case ERROR:
            System.out.println("Error in offload");
            throw new PulsarAdminException("Error offloading: " + status.lastError);
        }
    } catch (InterruptedException e) {
        throw new PulsarAdminException(e);
    }
}
 
Example #30
Source File: BaseResource.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public Builder request(final WebTarget target) throws PulsarAdminException {
    try {
        return requestAsync(target).get();
    } catch (Exception e) {
        throw new GettingAuthenticationDataException(e);
    }
}