Search in sources :

Example 1 with CreateConnector

use of io.confluent.ksql.parser.tree.CreateConnector in project ksql by confluentinc.

the class ConnectExecutorTest method shouldReturnErrorIfConnectorTypeIsEmpty.

@Test
public void shouldReturnErrorIfConnectorTypeIsEmpty() {
    // Given:
    final CreateConnector createConnectorEmptyType = new CreateConnector("foo", ImmutableMap.of("connector.class", new StringLiteral(" ")), Type.SOURCE, false);
    final ConfiguredStatement<CreateConnector> createConnectorEmptyTypeConfigured = ConfiguredStatement.of(PreparedStatement.of("CREATE SOURCE CONNECTOR foo WITH ('connector.class'=' ');", createConnectorEmptyType), SessionConfig.of(CONFIG, ImmutableMap.of()));
    // When:
    final Optional<KsqlEntity> entity = EXECUTOR.execute(createConnectorEmptyTypeConfigured, mock(SessionProperties.class), null, serviceContext).getEntity();
    // Then:
    assertThat("Expected non-empty response", entity.isPresent());
    assertThat(entity.get(), instanceOf(ErrorEntity.class));
    final String expectedError = "Validation error: Connector type cannot be empty";
    assertThat(((ErrorEntity) entity.get()).getErrorMessage(), is(expectedError));
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) ErrorEntity(io.confluent.ksql.rest.entity.ErrorEntity) CreateConnector(io.confluent.ksql.parser.tree.CreateConnector) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) Test(org.junit.Test)

Example 2 with CreateConnector

use of io.confluent.ksql.parser.tree.CreateConnector in project ksql by confluentinc.

the class ConnectExecutorTest method shouldReturnErrorIfConnectorTypeIsMissing.

@Test
public void shouldReturnErrorIfConnectorTypeIsMissing() {
    // Given:
    final CreateConnector createConnectorMissingType = new CreateConnector("connector-name", ImmutableMap.of("foo", new StringLiteral("bar")), Type.SOURCE, false);
    final ConfiguredStatement<CreateConnector> createConnectorMissingTypeConfigured = ConfiguredStatement.of(PreparedStatement.of("CREATE SOURCE CONNECTOR foo WITH ('foo'='bar');", createConnectorMissingType), SessionConfig.of(CONFIG, ImmutableMap.of()));
    // When:
    final Optional<KsqlEntity> entity = EXECUTOR.execute(createConnectorMissingTypeConfigured, mock(SessionProperties.class), null, serviceContext).getEntity();
    // Then:
    assertThat("Expected non-empty response", entity.isPresent());
    assertThat(entity.get(), instanceOf(ErrorEntity.class));
    final String expectedError = "Validation error: " + "Connector config {name=connector-name, foo=bar} contains no connector type";
    assertThat(((ErrorEntity) entity.get()).getErrorMessage(), is(expectedError));
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) ErrorEntity(io.confluent.ksql.rest.entity.ErrorEntity) CreateConnector(io.confluent.ksql.parser.tree.CreateConnector) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) Test(org.junit.Test)

Example 3 with CreateConnector

use of io.confluent.ksql.parser.tree.CreateConnector in project ksql by confluentinc.

the class CommandParser method getCreateConnectorStatement.

private static SqlCreateConnectorStatement getCreateConnectorStatement(final String sql, final Map<String, String> variables) {
    final CreateConnector createConnector;
    try {
        final String substituted = VariableSubstitutor.substitute(KSQL_PARSER.parse(sql).get(0), variables);
        createConnector = (CreateConnector) new AstBuilder(TypeRegistry.EMPTY).buildStatement(KSQL_PARSER.parse(substituted).get(0).getStatement());
    } catch (ParseFailedException e) {
        throw new MigrationException(String.format("Failed to parse CREATE CONNECTOR statement. Statement: %s. Reason: %s", sql, e.getMessage()));
    }
    return new SqlCreateConnectorStatement(sql, preserveCase(createConnector.getName()), createConnector.getType() == Type.SOURCE, createConnector.getConfig().entrySet().stream().collect(Collectors.toMap(Entry::getKey, e -> toFieldType(e.getValue()))));
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) AstBuilder(io.confluent.ksql.parser.AstBuilder) Entry(java.util.Map.Entry) ParseFailedException(io.confluent.ksql.parser.exception.ParseFailedException) CreateConnector(io.confluent.ksql.parser.tree.CreateConnector)

Example 4 with CreateConnector

use of io.confluent.ksql.parser.tree.CreateConnector in project ksql by confluentinc.

the class ConnectExecutor method execute.

public StatementExecutorResponse execute(final ConfiguredStatement<CreateConnector> statement, final SessionProperties sessionProperties, final KsqlExecutionContext executionContext, final ServiceContext serviceContext) {
    final CreateConnector createConnector = statement.getStatement();
    final ConnectClient client = serviceContext.getConnectClient();
    final List<String> errors = validate(createConnector, client);
    if (!errors.isEmpty()) {
        final String errorMessage = "Validation error: " + String.join("\n", errors);
        return StatementExecutorResponse.handled(Optional.of(new ErrorEntity(statement.getStatementText(), errorMessage)));
    }
    final Optional<KsqlEntity> connectorsResponse = handleIfNotExists(statement, createConnector, client);
    if (connectorsResponse.isPresent()) {
        return StatementExecutorResponse.handled(connectorsResponse);
    }
    final ConnectResponse<ConnectorInfo> response = client.create(createConnector.getName(), buildConnectorConfig(createConnector));
    if (response.datum().isPresent()) {
        return StatementExecutorResponse.handled(Optional.of(new CreateConnectorEntity(statement.getStatementText(), response.datum().get())));
    }
    if (createConnector.ifNotExists()) {
        final Optional<KsqlEntity> connectors = handleIfNotExists(statement, createConnector, client);
        if (connectors.isPresent()) {
            return StatementExecutorResponse.handled(connectors);
        }
    }
    return StatementExecutorResponse.handled(connectErrorHandler.handle(statement, response));
}
Also used : CreateConnectorEntity(io.confluent.ksql.rest.entity.CreateConnectorEntity) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) ConnectClient(io.confluent.ksql.services.ConnectClient) ErrorEntity(io.confluent.ksql.rest.entity.ErrorEntity) CreateConnector(io.confluent.ksql.parser.tree.CreateConnector) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity)

Aggregations

CreateConnector (io.confluent.ksql.parser.tree.CreateConnector)4 ErrorEntity (io.confluent.ksql.rest.entity.ErrorEntity)3 KsqlEntity (io.confluent.ksql.rest.entity.KsqlEntity)3 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)2 Test (org.junit.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 AstBuilder (io.confluent.ksql.parser.AstBuilder)1 ParseFailedException (io.confluent.ksql.parser.exception.ParseFailedException)1 CreateConnectorEntity (io.confluent.ksql.rest.entity.CreateConnectorEntity)1 ConnectClient (io.confluent.ksql.services.ConnectClient)1 MigrationException (io.confluent.ksql.tools.migrations.MigrationException)1 Entry (java.util.Map.Entry)1 ConnectorInfo (org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo)1