Skip to content

Commit 37f50c8

Browse files
authored
Merge pull request #337 from der-eismann/support-kafka-3
2 parents f814261 + ac072f5 commit 37f50c8

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

manifests/topic.pp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
#
1515
# @param zookeeper
1616
# The connection string for the ZooKeeper connection in the form host:port.
17-
# Multiple hosts can be given to allow fail-over.
17+
# Multiple hosts can be given to allow fail-over. Kafka < 3.0.0 only!
18+
#
19+
# @param bootstrap_server
20+
# The Kafka server to connect to in the form host:port. Kafka >= 2.2.0 only!
1821
#
1922
# @param replication_factor
2023
# The replication factor for each partition in the topic being created. If
@@ -32,17 +35,29 @@
3235
# See the Kafka documentation for full details on the topic configs.
3336
#
3437
define kafka::topic (
35-
String[1] $ensure = '',
36-
String[1] $zookeeper = '',
38+
Optional[String[1]] $ensure = undef,
39+
Optional[String[1]] $zookeeper = undef,
40+
Optional[String[1]] $bootstrap_server = undef,
3741
Integer $replication_factor = 1,
3842
Integer $partitions = 1,
3943
String[1] $bin_dir = '/opt/kafka/bin',
4044
Optional[Hash[String[1],String[1]]] $config = undef,
4145
) {
4246
$_zookeeper = "--zookeeper ${zookeeper}"
47+
$_bootstrap_server = "--bootstrap-server ${bootstrap_server}"
4348
$_replication_factor = "--replication-factor ${replication_factor}"
4449
$_partitions = "--partitions ${partitions}"
4550

51+
if !$zookeeper and !$bootstrap_server {
52+
fail('Either zookeeper or bootstrap_server parameter must be defined!')
53+
}
54+
55+
if $zookeeper {
56+
$_connection = $_zookeeper
57+
} else {
58+
$_connection = $_bootstrap_server
59+
}
60+
4661
if $config {
4762
$_config_array = $config.map |$key, $value| { "--config ${key}=${value}" }
4863
$_config = join($_config_array, ' ')
@@ -53,8 +68,8 @@
5368
if $ensure == 'present' {
5469
exec { "create topic ${name}":
5570
path => "/usr/bin:/usr/sbin/:/bin:/sbin:${bin_dir}",
56-
command => "kafka-topics.sh --create ${_zookeeper} ${_replication_factor} ${_partitions} --topic ${name} ${_config}",
57-
unless => "kafka-topics.sh --list ${_zookeeper} | grep -x ${name}",
71+
command => "kafka-topics.sh --create ${_connection} ${_replication_factor} ${_partitions} --topic ${name} ${_config}",
72+
unless => "kafka-topics.sh --list ${_connection} | grep -x ${name}",
5873
}
5974
}
6075
}

spec/defines/topic_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,39 @@
2525
}
2626
end
2727

28+
context 'when create topic demo for kafka v3' do
29+
let(:title) { 'demo' }
30+
let :params do
31+
{
32+
'ensure' => 'present',
33+
'bootstrap_server' => 'localhost:9092',
34+
'replication_factor' => 1,
35+
'partitions' => 1
36+
}
37+
end
38+
39+
it {
40+
is_expected.to contain_exec('create topic demo').with(
41+
command: 'kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic demo '
42+
)
43+
}
44+
end
45+
46+
context 'when create topic without zookeeper or bootstrap_server' do
47+
let(:title) { 'demo' }
48+
let :params do
49+
{
50+
'ensure' => 'present',
51+
'replication_factor' => 1,
52+
'partitions' => 1
53+
}
54+
end
55+
56+
it {
57+
is_expected.to compile.and_raise_error(%r{Either zookeeper or bootstrap_server parameter must be defined!})
58+
}
59+
end
60+
2861
context 'when create topic demo with config' do
2962
let(:title) { 'demo' }
3063
let :params do

0 commit comments

Comments
 (0)