Search in sources :

Example 1 with CreateAclsResponse

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

the class KafkaAdminClientTest method testCreateAcls.

@Test
public void testCreateAcls() throws Exception {
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareMetadataUpdate(env.cluster(), Collections.<String>emptySet());
        env.kafkaClient().setNode(env.cluster().controller());
        // Test a call where we successfully create two ACLs.
        env.kafkaClient().prepareResponse(new CreateAclsResponse(0, asList(new AclCreationResponse(ApiError.NONE), new AclCreationResponse(ApiError.NONE))));
        CreateAclsResult results = env.adminClient().createAcls(asList(ACL1, ACL2));
        assertCollectionIs(results.values().keySet(), ACL1, ACL2);
        for (KafkaFuture<Void> future : results.values().values()) future.get();
        results.all().get();
        // Test a call where we fail to create one ACL.
        env.kafkaClient().prepareResponse(new CreateAclsResponse(0, asList(new AclCreationResponse(new ApiError(Errors.SECURITY_DISABLED, "Security is disabled")), new AclCreationResponse(ApiError.NONE))));
        results = env.adminClient().createAcls(asList(ACL1, ACL2));
        assertCollectionIs(results.values().keySet(), ACL1, ACL2);
        assertFutureError(results.values().get(ACL1), SecurityDisabledException.class);
        results.values().get(ACL2).get();
        assertFutureError(results.all(), SecurityDisabledException.class);
    }
}
Also used : AclCreationResponse(org.apache.kafka.common.requests.CreateAclsResponse.AclCreationResponse) ApiError(org.apache.kafka.common.requests.ApiError) CreateAclsResponse(org.apache.kafka.common.requests.CreateAclsResponse) Test(org.junit.Test)

Example 2 with CreateAclsResponse

use of org.apache.kafka.common.requests.CreateAclsResponse 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)

Aggregations

CreateAclsResponse (org.apache.kafka.common.requests.CreateAclsResponse)2 AclCreationResponse (org.apache.kafka.common.requests.CreateAclsResponse.AclCreationResponse)2 ArrayList (java.util.ArrayList)1 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 AclBinding (org.apache.kafka.common.acl.AclBinding)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 ApiError (org.apache.kafka.common.requests.ApiError)1 CreateAclsRequest (org.apache.kafka.common.requests.CreateAclsRequest)1 AclCreation (org.apache.kafka.common.requests.CreateAclsRequest.AclCreation)1 Test (org.junit.Test)1