Search in sources :

Example 46 with Connector

use of org.apache.kafka.connect.connector.Connector in project kafka by apache.

the class StandaloneHerderTest method testCreateConnectorFailedCustomValidation.

@Test
public void testCreateConnectorFailedCustomValidation() throws Exception {
    connector = PowerMock.createMock(BogusSourceConnector.class);
    Map<String, String> config = connectorConfig(SourceSink.SOURCE);
    ConnectorFactory connectorFactoryMock = PowerMock.createMock(ConnectorFactory.class);
    EasyMock.expect(worker.getConnectorFactory()).andStubReturn(connectorFactoryMock);
    Connector connectorMock = PowerMock.createMock(Connector.class);
    EasyMock.expect(connectorFactoryMock.newConnector(EasyMock.anyString())).andReturn(connectorMock);
    ConfigDef configDef = new ConfigDef();
    configDef.define("foo.bar", ConfigDef.Type.STRING, ConfigDef.Importance.HIGH, "foo.bar doc");
    EasyMock.expect(connectorMock.config()).andReturn(configDef);
    ConfigValue validatedValue = new ConfigValue("foo.bar");
    validatedValue.addErrorMessage("Failed foo.bar validation");
    EasyMock.expect(connectorMock.validate(config)).andReturn(new Config(singletonList(validatedValue)));
    createCallback.onCompletion(EasyMock.<BadRequestException>anyObject(), EasyMock.<Herder.Created<ConnectorInfo>>isNull());
    PowerMock.expectLastCall();
    PowerMock.replayAll();
    herder.putConnectorConfig(CONNECTOR_NAME, config, false, createCallback);
    PowerMock.verifyAll();
}
Also used : SourceConnector(org.apache.kafka.connect.source.SourceConnector) Connector(org.apache.kafka.connect.connector.Connector) SinkConnector(org.apache.kafka.connect.sink.SinkConnector) ConfigValue(org.apache.kafka.common.config.ConfigValue) ConnectorFactory(org.apache.kafka.connect.runtime.ConnectorFactory) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) ConnectorConfig(org.apache.kafka.connect.runtime.ConnectorConfig) Config(org.apache.kafka.common.config.Config) TaskConfig(org.apache.kafka.connect.runtime.TaskConfig) ConfigDef(org.apache.kafka.common.config.ConfigDef) Herder(org.apache.kafka.connect.runtime.Herder) Test(org.junit.Test)

Example 47 with Connector

use of org.apache.kafka.connect.connector.Connector in project kafka by apache.

the class ConnectorPluginsResourceTest method testValidateConfig.

@Test
public void testValidateConfig() throws Throwable {
    herder.validateConnectorConfig(EasyMock.eq(props));
    PowerMock.expectLastCall().andAnswer(new IAnswer<ConfigInfos>() {

        @Override
        public ConfigInfos answer() {
            ConfigDef connectorConfigDef = ConnectorConfig.configDef();
            List<ConfigValue> connectorConfigValues = connectorConfigDef.validate(props);
            Connector connector = new ConnectorPluginsResourceTestConnector();
            Config config = connector.validate(props);
            ConfigDef configDef = connector.config();
            Map<String, ConfigDef.ConfigKey> configKeys = configDef.configKeys();
            List<ConfigValue> configValues = config.configValues();
            Map<String, ConfigDef.ConfigKey> resultConfigKeys = new HashMap<>(configKeys);
            resultConfigKeys.putAll(connectorConfigDef.configKeys());
            configValues.addAll(connectorConfigValues);
            return AbstractHerder.generateResult(ConnectorPluginsResourceTestConnector.class.getName(), resultConfigKeys, configValues, Collections.singletonList("Test"));
        }
    });
    PowerMock.replayAll();
    ConfigInfos configInfos = connectorPluginsResource.validateConfigs(ConnectorPluginsResourceTestConnector.class.getName(), props);
    assertEquals(CONFIG_INFOS.name(), configInfos.name());
    assertEquals(CONFIG_INFOS.errorCount(), configInfos.errorCount());
    assertEquals(CONFIG_INFOS.groups(), configInfos.groups());
    assertEquals(new HashSet<>(CONFIG_INFOS.values()), new HashSet<>(configInfos.values()));
    PowerMock.verifyAll();
}
Also used : Connector(org.apache.kafka.connect.connector.Connector) SchemaSourceConnector(org.apache.kafka.connect.tools.SchemaSourceConnector) SourceConnector(org.apache.kafka.connect.source.SourceConnector) VerifiableSourceConnector(org.apache.kafka.connect.tools.VerifiableSourceConnector) MockSinkConnector(org.apache.kafka.connect.tools.MockSinkConnector) SinkConnector(org.apache.kafka.connect.sink.SinkConnector) MockSourceConnector(org.apache.kafka.connect.tools.MockSourceConnector) VerifiableSinkConnector(org.apache.kafka.connect.tools.VerifiableSinkConnector) MockConnector(org.apache.kafka.connect.tools.MockConnector) Config(org.apache.kafka.common.config.Config) ConnectorConfig(org.apache.kafka.connect.runtime.ConnectorConfig) LinkedList(java.util.LinkedList) List(java.util.List) ConfigDef(org.apache.kafka.common.config.ConfigDef) HashMap(java.util.HashMap) Map(java.util.Map) ConfigInfos(org.apache.kafka.connect.runtime.rest.entities.ConfigInfos) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 48 with Connector

use of org.apache.kafka.connect.connector.Connector in project connect-utils by jcustenborder.

the class TemplateInput method fromConnector.

public static TemplateInput fromConnector(Class<? extends Connector> connectorClass) throws IllegalAccessException, InstantiationException {
    final TemplateInput result = new TemplateInput();
    Connector connector = connectorClass.newInstance();
    ConfigDef config = connector.config();
    assertNotNull(config, "config() cannot return a null.");
    populateTemplate(connectorClass, result, config);
    return result;
}
Also used : Connector(org.apache.kafka.connect.connector.Connector) ConfigDef(org.apache.kafka.common.config.ConfigDef)

Example 49 with Connector

use of org.apache.kafka.connect.connector.Connector in project apache-kafka-on-k8s by banzaicloud.

the class AbstractHerder method validateConnectorConfig.

@Override
public ConfigInfos validateConnectorConfig(Map<String, String> connectorProps) {
    String connType = connectorProps.get(ConnectorConfig.CONNECTOR_CLASS_CONFIG);
    if (connType == null)
        throw new BadRequestException("Connector config " + connectorProps + " contains no connector type");
    Connector connector = getConnector(connType);
    ClassLoader savedLoader = plugins().compareAndSwapLoaders(connector);
    try {
        ConfigDef baseConfigDef;
        if (connector instanceof SourceConnector) {
            baseConfigDef = SourceConnectorConfig.configDef();
        } else {
            baseConfigDef = SinkConnectorConfig.configDef();
            SinkConnectorConfig.validate(connectorProps);
        }
        ConfigDef enrichedConfigDef = ConnectorConfig.enrich(plugins(), baseConfigDef, connectorProps, false);
        Map<String, ConfigValue> validatedConnectorConfig = validateBasicConnectorConfig(connector, enrichedConfigDef, connectorProps);
        List<ConfigValue> configValues = new ArrayList<>(validatedConnectorConfig.values());
        Map<String, ConfigKey> configKeys = new LinkedHashMap<>(enrichedConfigDef.configKeys());
        Set<String> allGroups = new LinkedHashSet<>(enrichedConfigDef.groups());
        // do custom connector-specific validation
        Config config = connector.validate(connectorProps);
        ConfigDef configDef = connector.config();
        configKeys.putAll(configDef.configKeys());
        allGroups.addAll(configDef.groups());
        configValues.addAll(config.configValues());
        return generateResult(connType, configKeys, configValues, new ArrayList<>(allGroups));
    } finally {
        Plugins.compareAndSwapLoaders(savedLoader);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Connector(org.apache.kafka.connect.connector.Connector) SourceConnector(org.apache.kafka.connect.source.SourceConnector) ConfigValue(org.apache.kafka.common.config.ConfigValue) ConfigKey(org.apache.kafka.common.config.ConfigDef.ConfigKey) Config(org.apache.kafka.common.config.Config) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SourceConnector(org.apache.kafka.connect.source.SourceConnector) BadRequestException(org.apache.kafka.connect.runtime.rest.errors.BadRequestException) ConfigDef(org.apache.kafka.common.config.ConfigDef)

Example 50 with Connector

use of org.apache.kafka.connect.connector.Connector in project apache-kafka-on-k8s by banzaicloud.

the class AbstractHerder method getConnector.

protected Connector getConnector(String connType) {
    if (tempConnectors.containsKey(connType)) {
        return tempConnectors.get(connType);
    } else {
        Connector connector = plugins().newConnector(connType);
        tempConnectors.put(connType, connector);
        return connector;
    }
}
Also used : Connector(org.apache.kafka.connect.connector.Connector) SourceConnector(org.apache.kafka.connect.source.SourceConnector)

Aggregations

Connector (org.apache.kafka.connect.connector.Connector)76 SourceConnector (org.apache.kafka.connect.source.SourceConnector)62 Test (org.junit.Test)54 SinkConnector (org.apache.kafka.connect.sink.SinkConnector)50 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)48 ConfigDef (org.apache.kafka.common.config.ConfigDef)35 HashMap (java.util.HashMap)34 WorkerConnector (org.apache.kafka.connect.runtime.WorkerConnector)31 ConnectorInfo (org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo)31 ConfigValue (org.apache.kafka.common.config.ConfigValue)30 Herder (org.apache.kafka.connect.runtime.Herder)27 Map (java.util.Map)26 Config (org.apache.kafka.common.config.Config)26 ConnectorConfig (org.apache.kafka.connect.runtime.ConnectorConfig)24 ArrayList (java.util.ArrayList)21 ConnectorTaskId (org.apache.kafka.connect.util.ConnectorTaskId)20 BadRequestException (org.apache.kafka.connect.runtime.rest.errors.BadRequestException)19 FutureCallback (org.apache.kafka.connect.util.FutureCallback)18 List (java.util.List)15 TaskConfig (org.apache.kafka.connect.runtime.TaskConfig)15