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));
}
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));
}
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()))));
}
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));
}
Aggregations