Search in sources :

Example 16 with ConfigInfos

use of org.apache.kafka.connect.runtime.rest.entities.ConfigInfos in project kafka by apache.

the class ConnectorPluginsResourceTest method testValidateConfigWithSingleErrorDueToMissingConnectorClassname.

@Test
public void testValidateConfigWithSingleErrorDueToMissingConnectorClassname() throws Throwable {
    Capture<Callback<ConfigInfos>> configInfosCallback = EasyMock.newCapture();
    herder.validateConnectorConfig(EasyMock.eq(partialProps), EasyMock.capture(configInfosCallback), EasyMock.anyBoolean());
    PowerMock.expectLastCall().andAnswer((IAnswer<Void>) () -> {
        ConfigDef connectorConfigDef = ConnectorConfig.configDef();
        List<ConfigValue> connectorConfigValues = connectorConfigDef.validate(partialProps);
        Connector connector = new ConnectorPluginsResourceTestConnector();
        Config config = connector.validate(partialProps);
        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);
        ConfigInfos configInfos = AbstractHerder.generateResult(ConnectorPluginsResourceTestConnector.class.getName(), resultConfigKeys, configValues, Collections.singletonList("Test"));
        configInfosCallback.getValue().onCompletion(null, configInfos);
        return null;
    });
    PowerMock.replayAll();
    // This call to validateConfigs does not throw a BadRequestException because we've mocked
    // validateConnectorConfig.
    ConfigInfos configInfos = connectorPluginsResource.validateConfigs(ConnectorPluginsResourceTestConnector.class.getSimpleName(), partialProps);
    assertEquals(PARTIAL_CONFIG_INFOS.name(), configInfos.name());
    assertEquals(PARTIAL_CONFIG_INFOS.errorCount(), configInfos.errorCount());
    assertEquals(PARTIAL_CONFIG_INFOS.groups(), configInfos.groups());
    assertEquals(new HashSet<>(PARTIAL_CONFIG_INFOS.values()), new HashSet<>(configInfos.values()));
    PowerMock.verifyAll();
}
Also used : TestSourceConnector(org.apache.kafka.connect.runtime.TestSourceConnector) SourceConnector(org.apache.kafka.connect.source.SourceConnector) VerifiableSourceConnector(org.apache.kafka.connect.tools.VerifiableSourceConnector) MockSinkConnector(org.apache.kafka.connect.tools.MockSinkConnector) MockSourceConnector(org.apache.kafka.connect.tools.MockSourceConnector) VerifiableSinkConnector(org.apache.kafka.connect.tools.VerifiableSinkConnector) MockConnector(org.apache.kafka.connect.tools.MockConnector) Connector(org.apache.kafka.connect.connector.Connector) SchemaSourceConnector(org.apache.kafka.connect.tools.SchemaSourceConnector) SinkConnector(org.apache.kafka.connect.sink.SinkConnector) TestSinkConnector(org.apache.kafka.connect.runtime.TestSinkConnector) Callback(org.apache.kafka.connect.util.Callback) WorkerConfig(org.apache.kafka.connect.runtime.WorkerConfig) ConnectorConfig(org.apache.kafka.connect.runtime.ConnectorConfig) Config(org.apache.kafka.common.config.Config) Arrays.asList(java.util.Arrays.asList) List(java.util.List) LinkedList(java.util.LinkedList) ConfigDef(org.apache.kafka.common.config.ConfigDef) Map(java.util.Map) HashMap(java.util.HashMap) ConfigInfos(org.apache.kafka.connect.runtime.rest.entities.ConfigInfos) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 17 with ConfigInfos

use of org.apache.kafka.connect.runtime.rest.entities.ConfigInfos in project kafka by apache.

the class StandaloneHerder method putConnectorConfig.

@Override
public synchronized void putConnectorConfig(String connName, final Map<String, String> config, boolean allowReplace, final Callback<Created<ConnectorInfo>> callback) {
    try {
        ConfigInfos validatedConfig = validateConnectorConfig(config);
        if (validatedConfig.errorCount() > 0) {
            callback.onCompletion(new BadRequestException("Connector configuration is invalid " + "(use the endpoint `/{connectorType}/config/validate` to get a full list of errors)"), null);
            return;
        }
        boolean created = false;
        if (configState.contains(connName)) {
            if (!allowReplace) {
                callback.onCompletion(new AlreadyExistsException("Connector " + connName + " already exists"), null);
                return;
            }
            worker.stopConnector(connName);
        } else {
            created = true;
        }
        if (!startConnector(config)) {
            callback.onCompletion(new ConnectException("Failed to start connector: " + connName), null);
            return;
        }
        updateConnectorTasks(connName);
        callback.onCompletion(null, new Created<>(created, createConnectorInfo(connName)));
    } catch (ConnectException e) {
        callback.onCompletion(e, null);
    }
}
Also used : AlreadyExistsException(org.apache.kafka.connect.errors.AlreadyExistsException) BadRequestException(org.apache.kafka.connect.runtime.rest.errors.BadRequestException) ConfigInfos(org.apache.kafka.connect.runtime.rest.entities.ConfigInfos) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 18 with ConfigInfos

use of org.apache.kafka.connect.runtime.rest.entities.ConfigInfos 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 19 with ConfigInfos

use of org.apache.kafka.connect.runtime.rest.entities.ConfigInfos in project apache-kafka-on-k8s by banzaicloud.

the class AbstractHerder method generateResult.

// public for testing
public static ConfigInfos generateResult(String connType, Map<String, ConfigKey> configKeys, List<ConfigValue> configValues, List<String> groups) {
    int errorCount = 0;
    List<ConfigInfo> configInfoList = new LinkedList<>();
    Map<String, ConfigValue> configValueMap = new HashMap<>();
    for (ConfigValue configValue : configValues) {
        String configName = configValue.name();
        configValueMap.put(configName, configValue);
        if (!configKeys.containsKey(configName)) {
            configValue.addErrorMessage("Configuration is not defined: " + configName);
            configInfoList.add(new ConfigInfo(null, convertConfigValue(configValue, null)));
        }
    }
    for (Map.Entry<String, ConfigKey> entry : configKeys.entrySet()) {
        String configName = entry.getKey();
        ConfigKeyInfo configKeyInfo = convertConfigKey(entry.getValue());
        Type type = entry.getValue().type;
        ConfigValueInfo configValueInfo = null;
        if (configValueMap.containsKey(configName)) {
            ConfigValue configValue = configValueMap.get(configName);
            configValueInfo = convertConfigValue(configValue, type);
            errorCount += configValue.errorMessages().size();
        }
        configInfoList.add(new ConfigInfo(configKeyInfo, configValueInfo));
    }
    return new ConfigInfos(connType, errorCount, groups, configInfoList);
}
Also used : ConfigValue(org.apache.kafka.common.config.ConfigValue) ConfigKey(org.apache.kafka.common.config.ConfigDef.ConfigKey) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConfigInfo(org.apache.kafka.connect.runtime.rest.entities.ConfigInfo) LinkedList(java.util.LinkedList) Type(org.apache.kafka.common.config.ConfigDef.Type) ConnectorType(org.apache.kafka.connect.runtime.rest.entities.ConnectorType) ConfigValueInfo(org.apache.kafka.connect.runtime.rest.entities.ConfigValueInfo) ConfigKeyInfo(org.apache.kafka.connect.runtime.rest.entities.ConfigKeyInfo) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConfigInfos(org.apache.kafka.connect.runtime.rest.entities.ConfigInfos)

Example 20 with ConfigInfos

use of org.apache.kafka.connect.runtime.rest.entities.ConfigInfos in project apache-kafka-on-k8s by banzaicloud.

the class ConnectorPluginsResourceTest method testValidateConfigWithSingleErrorDueToMissingConnectorClassname.

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

        @Override
        public ConfigInfos answer() {
            ConfigDef connectorConfigDef = ConnectorConfig.configDef();
            List<ConfigValue> connectorConfigValues = connectorConfigDef.validate(partialProps);
            Connector connector = new ConnectorPluginsResourceTestConnector();
            Config config = connector.validate(partialProps);
            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();
    // This call to validateConfigs does not throw a BadRequestException because we've mocked
    // validateConnectorConfig.
    ConfigInfos configInfos = connectorPluginsResource.validateConfigs(ConnectorPluginsResourceTestConnector.class.getSimpleName(), partialProps);
    assertEquals(PARTIAL_CONFIG_INFOS.name(), configInfos.name());
    assertEquals(PARTIAL_CONFIG_INFOS.errorCount(), configInfos.errorCount());
    assertEquals(PARTIAL_CONFIG_INFOS.groups(), configInfos.groups());
    assertEquals(new HashSet<>(PARTIAL_CONFIG_INFOS.values()), new HashSet<>(configInfos.values()));
    PowerMock.verifyAll();
}
Also used : TestSourceConnector(org.apache.kafka.connect.runtime.TestSourceConnector) SourceConnector(org.apache.kafka.connect.source.SourceConnector) VerifiableSourceConnector(org.apache.kafka.connect.tools.VerifiableSourceConnector) MockSinkConnector(org.apache.kafka.connect.tools.MockSinkConnector) MockSourceConnector(org.apache.kafka.connect.tools.MockSourceConnector) VerifiableSinkConnector(org.apache.kafka.connect.tools.VerifiableSinkConnector) MockConnector(org.apache.kafka.connect.tools.MockConnector) Connector(org.apache.kafka.connect.connector.Connector) SchemaSourceConnector(org.apache.kafka.connect.tools.SchemaSourceConnector) SinkConnector(org.apache.kafka.connect.sink.SinkConnector) TestSinkConnector(org.apache.kafka.connect.runtime.TestSinkConnector) WorkerConfig(org.apache.kafka.connect.runtime.WorkerConfig) ConnectorConfig(org.apache.kafka.connect.runtime.ConnectorConfig) Config(org.apache.kafka.common.config.Config) List(java.util.List) LinkedList(java.util.LinkedList) ConfigDef(org.apache.kafka.common.config.ConfigDef) Map(java.util.Map) HashMap(java.util.HashMap) ConfigInfos(org.apache.kafka.connect.runtime.rest.entities.ConfigInfos) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ConfigInfos (org.apache.kafka.connect.runtime.rest.entities.ConfigInfos)29 HashMap (java.util.HashMap)23 Test (org.junit.Test)20 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Map (java.util.Map)16 ConfigDef (org.apache.kafka.common.config.ConfigDef)13 ConfigValue (org.apache.kafka.common.config.ConfigValue)13 Connector (org.apache.kafka.connect.connector.Connector)13 SourceConnector (org.apache.kafka.connect.source.SourceConnector)13 List (java.util.List)12 LinkedList (java.util.LinkedList)11 ArrayList (java.util.ArrayList)10 ConfigInfo (org.apache.kafka.connect.runtime.rest.entities.ConfigInfo)9 Config (org.apache.kafka.common.config.Config)8 ConnectorConfig (org.apache.kafka.connect.runtime.ConnectorConfig)7 BadRequestException (org.apache.kafka.connect.runtime.rest.errors.BadRequestException)7 SinkConnector (org.apache.kafka.connect.sink.SinkConnector)7 MockConnector (org.apache.kafka.connect.tools.MockConnector)7 MockSinkConnector (org.apache.kafka.connect.tools.MockSinkConnector)7 MockSourceConnector (org.apache.kafka.connect.tools.MockSourceConnector)7