use of org.apache.pulsar.broker.resources.TenantResources in project pulsar by apache.
the class PulsarClusterMetadataSetup method createTenantIfAbsent.
static void createTenantIfAbsent(PulsarResources resources, String tenant, String cluster) throws IOException, InterruptedException, ExecutionException {
TenantResources tenantResources = resources.getTenantResources();
if (!tenantResources.tenantExists(tenant)) {
TenantInfoImpl publicTenant = new TenantInfoImpl(Collections.emptySet(), Collections.singleton(cluster));
tenantResources.createTenant(tenant, publicTenant);
} else {
// Update existing public tenant with new cluster
tenantResources.updateTenantAsync(tenant, ti -> {
ti.getAllowedClusters().add(cluster);
return ti;
}).get();
}
}
use of org.apache.pulsar.broker.resources.TenantResources in project pulsar by apache.
the class ClusterMetadataSetupTest method testInitialNamespaceSetup.
@Test
public void testInitialNamespaceSetup() throws Exception {
// missing arguments
assertEquals(PulsarInitialNamespaceSetup.doMain(new String[] {}), 1);
// invalid namespace
assertEquals(PulsarInitialNamespaceSetup.doMain(new String[] { "--cluster", "testInitialNamespaceSetup-cluster", "--configuration-store", "127.0.0.1:" + localZkS.getZookeeperPort(), "a/b/c/d" }), 1);
String[] args = { "--cluster", "testInitialNamespaceSetup-cluster", "--configuration-store", "127.0.0.1:" + localZkS.getZookeeperPort(), "test/a", "test/b", "test/c" };
assertEquals(PulsarInitialNamespaceSetup.doMain(args), 0);
try (MetadataStoreExtended store = MetadataStoreExtended.create("127.0.0.1:" + localZkS.getZookeeperPort(), MetadataStoreConfig.builder().build())) {
TenantResources tenantResources = new TenantResources(store, PulsarResources.DEFAULT_OPERATION_TIMEOUT_SEC);
List<String> namespaces = tenantResources.getListOfNamespaces("test");
assertEquals(new HashSet<>(namespaces), new HashSet<>(Arrays.asList("test/a", "test/b", "test/c")));
}
}
use of org.apache.pulsar.broker.resources.TenantResources in project pulsar by yahoo.
the class PulsarStandalone method createNameSpace.
@VisibleForTesting
void createNameSpace(String cluster, String publicTenant, NamespaceName ns) throws Exception {
ClusterResources cr = broker.getPulsarResources().getClusterResources();
TenantResources tr = broker.getPulsarResources().getTenantResources();
NamespaceResources nsr = broker.getPulsarResources().getNamespaceResources();
if (!cr.clusterExists(cluster)) {
cr.createCluster(cluster, ClusterData.builder().serviceUrl(broker.getWebServiceAddress()).serviceUrlTls(broker.getWebServiceAddressTls()).brokerServiceUrl(broker.getBrokerServiceUrl()).brokerServiceUrlTls(broker.getBrokerServiceUrlTls()).build());
}
if (!tr.tenantExists(publicTenant)) {
tr.createTenant(publicTenant, TenantInfo.builder().adminRoles(Sets.newHashSet(config.getSuperUserRoles())).allowedClusters(Sets.newHashSet(cluster)).build());
}
if (!nsr.namespaceExists(ns)) {
Policies nsp = new Policies();
nsp.replication_clusters = Collections.singleton(config.getClusterName());
nsr.createPolicies(ns, nsp);
}
}
use of org.apache.pulsar.broker.resources.TenantResources in project pulsar by yahoo.
the class PulsarStandaloneTest method testCreateNameSpace.
@Test
public void testCreateNameSpace() throws Exception {
final String cluster = "cluster1";
final String tenant = "tenant1";
final NamespaceName ns = NamespaceName.get(tenant, "ns1");
ClusterResources cr = mock(ClusterResources.class);
when(cr.clusterExists(cluster)).thenReturn(false).thenReturn(true);
doNothing().when(cr).createCluster(eq(cluster), any());
TenantResources tr = mock(TenantResources.class);
when(tr.tenantExists(tenant)).thenReturn(false).thenReturn(true);
doNothing().when(tr).createTenant(eq(tenant), any());
NamespaceResources nsr = mock(NamespaceResources.class);
when(nsr.namespaceExists(ns)).thenReturn(false).thenReturn(true);
doNothing().when(nsr).createPolicies(eq(ns), any());
PulsarResources resources = mock(PulsarResources.class);
when(resources.getClusterResources()).thenReturn(cr);
when(resources.getTenantResources()).thenReturn(tr);
when(resources.getNamespaceResources()).thenReturn(nsr);
PulsarService broker = mock(PulsarService.class);
when(broker.getPulsarResources()).thenReturn(resources);
when(broker.getWebServiceAddress()).thenReturn("pulsar://localhost:8080");
when(broker.getWebServiceAddressTls()).thenReturn(null);
when(broker.getBrokerServiceUrl()).thenReturn("pulsar://localhost:6650");
when(broker.getBrokerServiceUrlTls()).thenReturn(null);
ServiceConfiguration config = new ServiceConfiguration();
config.setClusterName(cluster);
PulsarStandalone standalone = new PulsarStandalone();
standalone.setBroker(broker);
standalone.setConfig(config);
standalone.createNameSpace(cluster, tenant, ns);
standalone.createNameSpace(cluster, tenant, ns);
verify(cr, times(1)).createCluster(eq(cluster), any());
verify(tr, times(1)).createTenant(eq(tenant), any());
verify(nsr, times(1)).createPolicies(eq(ns), any());
}
use of org.apache.pulsar.broker.resources.TenantResources in project pulsar by yahoo.
the class ClusterMetadataSetupTest method testInitialNamespaceSetup.
@Test
public void testInitialNamespaceSetup() throws Exception {
// missing arguments
assertEquals(PulsarInitialNamespaceSetup.doMain(new String[] {}), 1);
// invalid namespace
assertEquals(PulsarInitialNamespaceSetup.doMain(new String[] { "--cluster", "testInitialNamespaceSetup-cluster", "--configuration-store", "127.0.0.1:" + localZkS.getZookeeperPort(), "a/b/c/d" }), 1);
String[] args = { "--cluster", "testInitialNamespaceSetup-cluster", "--configuration-store", "127.0.0.1:" + localZkS.getZookeeperPort(), "test/a", "test/b", "test/c" };
assertEquals(PulsarInitialNamespaceSetup.doMain(args), 0);
try (MetadataStoreExtended store = MetadataStoreExtended.create("127.0.0.1:" + localZkS.getZookeeperPort(), MetadataStoreConfig.builder().build())) {
TenantResources tenantResources = new TenantResources(store, PulsarResources.DEFAULT_OPERATION_TIMEOUT_SEC);
List<String> namespaces = tenantResources.getListOfNamespaces("test");
assertEquals(new HashSet<>(namespaces), new HashSet<>(Arrays.asList("test/a", "test/b", "test/c")));
}
}
Aggregations