cmccabe commented on a change in pull request #9032: URL: https://github.com/apache/kafka/pull/9032#discussion_r475698813
########## File path: clients/src/main/java/org/apache/kafka/clients/admin/DescribeUserScramCredentialsResult.java ########## @@ -0,0 +1,139 @@ +/* + * 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 org.apache.kafka.clients.admin; + +import org.apache.kafka.common.KafkaFuture; +import org.apache.kafka.common.annotation.InterfaceStability; +import org.apache.kafka.common.errors.ResourceNotFoundException; + +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; + +/** + * The result of the {@link Admin#describeUserScramCredentials()} call. + * + * The API of this class is evolving, see {@link Admin} for details. + */ +@InterfaceStability.Evolving +public class DescribeUserScramCredentialsResult { + private final KafkaFuture<List<String>> usersFuture; + private final Map<String, KafkaFuture<UserScramCredentialsDescription>> perUserFutures; + + /** + * + * @param usersFuture the future indicating the users described by the call + * @param perUserFutures the required map of user names to futures representing the results of describing each + * user's SCRAM credentials. + */ + public DescribeUserScramCredentialsResult(KafkaFuture<List<String>> usersFuture, + Map<String, KafkaFuture<UserScramCredentialsDescription>> perUserFutures) { + this.usersFuture = Objects.requireNonNull(usersFuture); + this.perUserFutures = Objects.requireNonNull(perUserFutures); + } + + /** + * + * @return a future for the results of all requested (either explicitly or implicitly via describe-all) users. + * The future will complete successfully only if the users future first completes successfully and then all the + * futures for the user descriptions complete successfully. + */ + public KafkaFuture<Map<String, UserScramCredentialsDescription>> all() { + KafkaFuture<Void> succeedsOnlyIfUsersFutureSucceeds = KafkaFuture.allOf(users()); + return succeedsOnlyIfUsersFutureSucceeds.thenApply(void1 -> { + KafkaFuture<Void> succeedsOnlyIfAllDescriptionsSucceed = KafkaFuture.allOf(perUserFutures.values().toArray( + new KafkaFuture[perUserFutures.size()])); + KafkaFuture<Map<String, UserScramCredentialsDescription>> mapFuture = succeedsOnlyIfAllDescriptionsSucceed.thenApply(void2 -> + perUserFutures.entrySet().stream().collect(Collectors.toMap( + e -> e.getKey(), + e -> valueFromFutureGuaranteedToSucceedAtThisPoint(e.getValue())))); + /* At this point it is only the users future that is guaranteed to have succeeded. + * We want to return the future to the map, but we have to return a map at this point. + * We need to dereference the future while propagating any exception. + */ + return valueFromFuturePropagatingExceptionsAsUnchecked(mapFuture); + }); + } + + /** + * + * @return a future indicating the distinct users that were requested (either explicitly or implicitly via Review comment: I think it would be better to make this "a future indicating the users that were listed" (rather than "requested"). It's maybe a bit of a subtle distinction but think about things like requesting the null user, or the empty string user. It's awkward to put that here. I think if we explicitly request a user but it doesn't exist, it should be omitted from here as well. That gives us more flexibility in the future with the API. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org