|
| 1 | +/** |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.streamnative.oidc.broker.common; |
| 15 | + |
| 16 | +import static io.streamnative.oidc.broker.common.OIDCConstants.RESOURCE_SYNC_OPERATION_TIMEOUT_SEC; |
| 17 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 18 | +import io.streamnative.oidc.broker.common.pojo.Pool; |
| 19 | +import io.streamnative.oidc.broker.common.utils.Paths; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Optional; |
| 23 | +import java.util.concurrent.CompletableFuture; |
| 24 | +import java.util.stream.Collectors; |
| 25 | +import javax.validation.constraints.NotNull; |
| 26 | +import org.apache.pulsar.broker.resources.BaseResources; |
| 27 | +import org.apache.pulsar.common.util.FutureUtil; |
| 28 | +import org.apache.pulsar.metadata.api.MetadataStore; |
| 29 | +import org.apache.pulsar.metadata.api.MetadataStoreException; |
| 30 | + |
| 31 | + |
| 32 | +@SuppressWarnings("UnstableApiUsage") |
| 33 | +public final class OIDCPoolResources extends BaseResources<Pool> { |
| 34 | + private static final String BASE_PATH = "/sn-oidc/pools"; |
| 35 | + |
| 36 | + public OIDCPoolResources(@NotNull MetadataStore metadataStore) { |
| 37 | + super(metadataStore, new TypeReference<>() { }, RESOURCE_SYNC_OPERATION_TIMEOUT_SEC); |
| 38 | + } |
| 39 | + |
| 40 | + public @NotNull Optional<Pool> getPool(@NotNull String poolName) throws MetadataStoreException { |
| 41 | + return get(joinPath(BASE_PATH, Paths.getUrlEncodedPath(poolName))); |
| 42 | + } |
| 43 | + |
| 44 | + public @NotNull CompletableFuture<Optional<Pool>> getPoolAsync(@NotNull String poolName) { |
| 45 | + return getAsync(joinPath(BASE_PATH, Paths.getUrlEncodedPath(poolName))); |
| 46 | + } |
| 47 | + |
| 48 | + public void createPool(@NotNull Pool pool) throws MetadataStoreException { |
| 49 | + create(joinPath(BASE_PATH, Paths.getUrlEncodedPath(pool.name())), pool); |
| 50 | + } |
| 51 | + |
| 52 | + public @NotNull CompletableFuture<Void> createPoolAsync(@NotNull Pool pool) { |
| 53 | + return createAsync(joinPath(BASE_PATH, Paths.getUrlEncodedPath(pool.name())), pool); |
| 54 | + } |
| 55 | + |
| 56 | + public @NotNull CompletableFuture<Boolean> existsAsync(@NotNull String poolName) { |
| 57 | + return super.existsAsync(joinPath(BASE_PATH, Paths.getUrlEncodedPath(poolName))); |
| 58 | + } |
| 59 | + |
| 60 | + public void deletePool(@NotNull String poolName) throws MetadataStoreException { |
| 61 | + super.delete(joinPath(BASE_PATH, Paths.getUrlEncodedPath(poolName))); |
| 62 | + } |
| 63 | + |
| 64 | + public @NotNull CompletableFuture<Void> deletePoolAsync(@NotNull String poolName) { |
| 65 | + return super.deleteIfExistsAsync(joinPath(BASE_PATH, Paths.getUrlEncodedPath(poolName))); |
| 66 | + } |
| 67 | + |
| 68 | + public @NotNull CompletableFuture<Void> updatePoolAsync(@NotNull Pool pool) { |
| 69 | + return super.setAsync(joinPath(BASE_PATH, Paths.getUrlEncodedPath(pool.name())), __ -> pool); |
| 70 | + } |
| 71 | + |
| 72 | + public @NotNull CompletableFuture<List<String>> listPoolNamesAsync() { |
| 73 | + return super.getChildrenAsync(joinPath(BASE_PATH)); |
| 74 | + } |
| 75 | + |
| 76 | + public @NotNull CompletableFuture<List<Pool>> listPoolsAsync() { |
| 77 | + return super.getChildrenAsync(joinPath(BASE_PATH)) |
| 78 | + .thenCompose(poolNames -> { |
| 79 | + List<CompletableFuture<Optional<Pool>>> pools = new ArrayList<>(); |
| 80 | + for (String name : poolNames) { |
| 81 | + pools.add(getAsync(joinPath(BASE_PATH, name))); |
| 82 | + } |
| 83 | + return FutureUtil.waitForAll(pools) |
| 84 | + .thenApply(__ -> pools.stream().map(f -> f.join()) |
| 85 | + .filter(f -> f.isPresent()) |
| 86 | + .map(f -> f.get()) |
| 87 | + .collect(Collectors.toList())); |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + public static boolean pathIsFromPool(String path) { |
| 92 | + return path.startsWith(BASE_PATH + "/"); |
| 93 | + } |
| 94 | + |
| 95 | + public static String poolFromPath(String path) { |
| 96 | + return path.substring(BASE_PATH.length() + 1); |
| 97 | + } |
| 98 | +} |
0 commit comments