Search in sources :

Example 1 with AclCreation

use of org.apache.kafka.common.requests.CreateAclsRequest.AclCreation in project apache-kafka-on-k8s by banzaicloud.

the class KafkaAdminClient method createAcls.

@Override
public CreateAclsResult createAcls(Collection<AclBinding> acls, CreateAclsOptions options) {
    final long now = time.milliseconds();
    final Map<AclBinding, KafkaFutureImpl<Void>> futures = new HashMap<>();
    final List<AclCreation> aclCreations = new ArrayList<>();
    for (AclBinding acl : acls) {
        if (futures.get(acl) == null) {
            KafkaFutureImpl<Void> future = new KafkaFutureImpl<>();
            futures.put(acl, future);
            String indefinite = acl.toFilter().findIndefiniteField();
            if (indefinite == null) {
                aclCreations.add(new AclCreation(acl));
            } else {
                future.completeExceptionally(new InvalidRequestException("Invalid ACL creation: " + indefinite));
            }
        }
    }
    runnable.call(new Call("createAcls", calcDeadlineMs(now, options.timeoutMs()), new LeastLoadedNodeProvider()) {

        @Override
        AbstractRequest.Builder createRequest(int timeoutMs) {
            return new CreateAclsRequest.Builder(aclCreations);
        }

        @Override
        void handleResponse(AbstractResponse abstractResponse) {
            CreateAclsResponse response = (CreateAclsResponse) abstractResponse;
            List<AclCreationResponse> responses = response.aclCreationResponses();
            Iterator<AclCreationResponse> iter = responses.iterator();
            for (AclCreation aclCreation : aclCreations) {
                KafkaFutureImpl<Void> future = futures.get(aclCreation.acl());
                if (!iter.hasNext()) {
                    future.completeExceptionally(new UnknownServerException("The broker reported no creation result for the given ACL."));
                } else {
                    AclCreationResponse creation = iter.next();
                    if (creation.error().isFailure()) {
                        future.completeExceptionally(creation.error().exception());
                    } else {
                        future.complete(null);
                    }
                }
            }
        }

        @Override
        void handleFailure(Throwable throwable) {
            completeAllExceptionally(futures.values(), throwable);
        }
    }, now);
    return new CreateAclsResult(new HashMap<AclBinding, KafkaFuture<Void>>(futures));
}
Also used : HashMap(java.util.HashMap) ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) ArrayList(java.util.ArrayList) CreateAclsResponse(org.apache.kafka.common.requests.CreateAclsResponse) CreateAclsRequest(org.apache.kafka.common.requests.CreateAclsRequest) Iterator(java.util.Iterator) InvalidRequestException(org.apache.kafka.common.errors.InvalidRequestException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) AclBinding(org.apache.kafka.common.acl.AclBinding) AclCreation(org.apache.kafka.common.requests.CreateAclsRequest.AclCreation) KafkaFuture(org.apache.kafka.common.KafkaFuture) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) AclCreationResponse(org.apache.kafka.common.requests.CreateAclsResponse.AclCreationResponse)

Example 2 with AclCreation

use of org.apache.kafka.common.requests.CreateAclsRequest.AclCreation in project apache-kafka-on-k8s by banzaicloud.

the class RequestResponseTest method createCreateAclsRequest.

private CreateAclsRequest createCreateAclsRequest() {
    List<AclCreation> creations = new ArrayList<>();
    creations.add(new AclCreation(new AclBinding(new Resource(ResourceType.TOPIC, "mytopic"), new AccessControlEntry("User:ANONYMOUS", "127.0.0.1", AclOperation.READ, AclPermissionType.ALLOW))));
    creations.add(new AclCreation(new AclBinding(new Resource(ResourceType.GROUP, "mygroup"), new AccessControlEntry("User:ANONYMOUS", "*", AclOperation.WRITE, AclPermissionType.DENY))));
    return new CreateAclsRequest.Builder(creations).build();
}
Also used : ArrayList(java.util.ArrayList) Resource(org.apache.kafka.common.resource.Resource) AccessControlEntry(org.apache.kafka.common.acl.AccessControlEntry) AclCreation(org.apache.kafka.common.requests.CreateAclsRequest.AclCreation) AclBinding(org.apache.kafka.common.acl.AclBinding)

Aggregations

ArrayList (java.util.ArrayList)2 AclBinding (org.apache.kafka.common.acl.AclBinding)2 AclCreation (org.apache.kafka.common.requests.CreateAclsRequest.AclCreation)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 KafkaFuture (org.apache.kafka.common.KafkaFuture)1 AccessControlEntry (org.apache.kafka.common.acl.AccessControlEntry)1 InvalidRequestException (org.apache.kafka.common.errors.InvalidRequestException)1 UnknownServerException (org.apache.kafka.common.errors.UnknownServerException)1 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)1 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)1 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)1 CreateAclsRequest (org.apache.kafka.common.requests.CreateAclsRequest)1 CreateAclsResponse (org.apache.kafka.common.requests.CreateAclsResponse)1 AclCreationResponse (org.apache.kafka.common.requests.CreateAclsResponse.AclCreationResponse)1 Resource (org.apache.kafka.common.resource.Resource)1