use of org.apache.kafka.server.policy.AlterConfigPolicy.RequestMetadata in project kafka by apache.
the class ConfigurationControlManager method validateAlterConfig.
private ApiError validateAlterConfig(ConfigResource configResource, List<ApiMessageAndVersion> newRecords, Consumer<ConfigResource> existenceChecker) {
Map<String, String> newConfigs = new HashMap<>();
TimelineHashMap<String, String> existingConfigs = configData.get(configResource);
if (existingConfigs != null)
newConfigs.putAll(existingConfigs);
for (ApiMessageAndVersion newRecord : newRecords) {
ConfigRecord configRecord = (ConfigRecord) newRecord.message();
if (configRecord.value() == null) {
newConfigs.remove(configRecord.name());
} else {
newConfigs.put(configRecord.name(), configRecord.value());
}
}
try {
validator.validate(configResource, newConfigs);
existenceChecker.accept(configResource);
if (alterConfigPolicy.isPresent()) {
alterConfigPolicy.get().validate(new RequestMetadata(configResource, newConfigs));
}
} catch (ConfigException e) {
return new ApiError(INVALID_CONFIG, e.getMessage());
} catch (Throwable e) {
return ApiError.fromThrowable(e);
}
return ApiError.NONE;
}
use of org.apache.kafka.server.policy.AlterConfigPolicy.RequestMetadata in project kafka by apache.
the class ConfigurationControlManagerTest method testIncrementalAlterConfigsWithPolicy.
@Test
public void testIncrementalAlterConfigsWithPolicy() {
SnapshotRegistry snapshotRegistry = new SnapshotRegistry(new LogContext());
MockAlterConfigsPolicy policy = new MockAlterConfigsPolicy(asList(new RequestMetadata(MYTOPIC, Collections.emptyMap()), new RequestMetadata(BROKER0, toMap(entry("foo.bar", "123"), entry("quux", "456")))));
ConfigurationControlManager manager = new ConfigurationControlManager(new LogContext(), snapshotRegistry, CONFIGS, Optional.of(policy), ConfigurationValidator.NO_OP);
assertEquals(ControllerResult.atomicOf(asList(new ApiMessageAndVersion(new ConfigRecord().setResourceType(BROKER.id()).setResourceName("0").setName("foo.bar").setValue("123"), (short) 0), new ApiMessageAndVersion(new ConfigRecord().setResourceType(BROKER.id()).setResourceName("0").setName("quux").setValue("456"), (short) 0)), toMap(entry(MYTOPIC, new ApiError(Errors.POLICY_VIOLATION, "Expected: AlterConfigPolicy.RequestMetadata(resource=ConfigResource(" + "type=TOPIC, name='mytopic'), configs={}). Got: " + "AlterConfigPolicy.RequestMetadata(resource=ConfigResource(" + "type=TOPIC, name='mytopic'), configs={foo.bar=123})")), entry(BROKER0, ApiError.NONE))), manager.incrementalAlterConfigs(toMap(entry(MYTOPIC, toMap(entry("foo.bar", entry(SET, "123")))), entry(BROKER0, toMap(entry("foo.bar", entry(SET, "123")), entry("quux", entry(SET, "456"))))), NO_OP_EXISTENCE_CHECKER));
}
Aggregations