From 3814017c0e78084aecf8363cac31fce037c5c830 Mon Sep 17 00:00:00 2001 From: xiang hu Date: Sun, 19 Mar 2023 18:17:54 +0800 Subject: [PATCH] clusters rest api --- .../io/github/protocol/pulsar/Clusters.java | 28 ++++++++++ .../github/protocol/pulsar/ClustersImpl.java | 52 +++++++++++++++++++ .../github/protocol/pulsar/PulsarAdmin.java | 2 + .../protocol/pulsar/PulsarAdminImpl.java | 8 +++ .../io/github/protocol/pulsar/UrlConst.java | 2 + .../github/protocol/pulsar/ClustersTest.java | 50 ++++++++++++++++++ 6 files changed, 142 insertions(+) create mode 100644 src/main/java/io/github/protocol/pulsar/Clusters.java create mode 100644 src/main/java/io/github/protocol/pulsar/ClustersImpl.java create mode 100644 src/test/java/io/github/protocol/pulsar/ClustersTest.java diff --git a/src/main/java/io/github/protocol/pulsar/Clusters.java b/src/main/java/io/github/protocol/pulsar/Clusters.java new file mode 100644 index 0000000..66f117b --- /dev/null +++ b/src/main/java/io/github/protocol/pulsar/Clusters.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.github.protocol.pulsar; + +import java.util.List; + +public interface Clusters { + + List getClusters() throws PulsarAdminException; + +} diff --git a/src/main/java/io/github/protocol/pulsar/ClustersImpl.java b/src/main/java/io/github/protocol/pulsar/ClustersImpl.java new file mode 100644 index 0000000..312bab2 --- /dev/null +++ b/src/main/java/io/github/protocol/pulsar/ClustersImpl.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.github.protocol.pulsar; + +import com.fasterxml.jackson.core.type.TypeReference; + +import java.io.IOException; +import java.net.http.HttpResponse; +import java.util.List; + +public class ClustersImpl implements Clusters{ + + private final InnerHttpClient httpClient; + + public ClustersImpl(InnerHttpClient httpClient) { + this.httpClient = httpClient; + } + + @Override + public List getClusters() throws PulsarAdminException { + try { + HttpResponse response = httpClient.get(UrlConst.CLUSTERS); + if (response.statusCode() != 200) { + throw new PulsarAdminException( + String.format("failed to get list of clusters, " + + "status code %s, body : %s", response.statusCode(), response.body())); + } + return JacksonService.toRefer(response.body(), new TypeReference>() { + }); + } catch (IOException | InterruptedException e) { + throw new PulsarAdminException(e); + } + } + +} diff --git a/src/main/java/io/github/protocol/pulsar/PulsarAdmin.java b/src/main/java/io/github/protocol/pulsar/PulsarAdmin.java index c8d596b..8cc1f76 100644 --- a/src/main/java/io/github/protocol/pulsar/PulsarAdmin.java +++ b/src/main/java/io/github/protocol/pulsar/PulsarAdmin.java @@ -23,6 +23,8 @@ static PulsarAdminBuilder builder() { return new PulsarAdminBuilderImpl(); } + Clusters clusters(); + Brokers brokers(); Tenants tenants(); diff --git a/src/main/java/io/github/protocol/pulsar/PulsarAdminImpl.java b/src/main/java/io/github/protocol/pulsar/PulsarAdminImpl.java index 915d2a4..7e306ab 100644 --- a/src/main/java/io/github/protocol/pulsar/PulsarAdminImpl.java +++ b/src/main/java/io/github/protocol/pulsar/PulsarAdminImpl.java @@ -20,6 +20,8 @@ public class PulsarAdminImpl implements PulsarAdmin { + private final Clusters clusters; + private final Brokers brokers; private final Tenants tenants; @@ -32,6 +34,7 @@ public class PulsarAdminImpl implements PulsarAdmin { PulsarAdminImpl(Configuration conf) { InnerHttpClient innerHttpClient = new InnerHttpClient(conf); + this.clusters = new ClustersImpl(innerHttpClient); this.brokers = new BrokersImpl(innerHttpClient); this.tenants = new TenantsImpl(innerHttpClient); this.namespaces = new NamespacesImpl(innerHttpClient); @@ -39,6 +42,11 @@ public class PulsarAdminImpl implements PulsarAdmin { this.nonPersistentTopics = new NonPersistentTopicsImpl(innerHttpClient); } + @Override + public Clusters clusters() { + return clusters; + } + @Override public Brokers brokers() { return brokers; diff --git a/src/main/java/io/github/protocol/pulsar/UrlConst.java b/src/main/java/io/github/protocol/pulsar/UrlConst.java index 5eee3b4..c2a064b 100644 --- a/src/main/java/io/github/protocol/pulsar/UrlConst.java +++ b/src/main/java/io/github/protocol/pulsar/UrlConst.java @@ -22,6 +22,8 @@ public class UrlConst { public static final String BASE_URL_V2 = "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/admin/v2"; + public static final String CLUSTERS = BASE_URL_V2 + "/clusters"; + public static final String BROKERS = BASE_URL_V2 + "/brokers"; public static final String TENANTS = BASE_URL_V2 + "/tenants"; diff --git a/src/test/java/io/github/protocol/pulsar/ClustersTest.java b/src/test/java/io/github/protocol/pulsar/ClustersTest.java new file mode 100644 index 0000000..b3d32c2 --- /dev/null +++ b/src/test/java/io/github/protocol/pulsar/ClustersTest.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.github.protocol.pulsar; + +import io.github.embedded.pulsar.core.EmbeddedPulsarServer; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.List; + +public class ClustersTest { + + private static final EmbeddedPulsarServer SERVER = new EmbeddedPulsarServer(); + + @BeforeAll + public static void setup() throws Exception { + SERVER.start(); + } + + @AfterAll + public static void teardown() throws Exception { + SERVER.close(); + } + + @Test + public void getClustersTest() throws PulsarAdminException { + Assertions.assertEquals(List.of("standalone"), + PulsarAdmin.builder().port(SERVER.getWebPort()).build().clusters().getClusters()); + } + +}