Skip to content

Commit

Permalink
Merge pull request #111 from StephenSorriaux/feat/add-topics-configs
Browse files Browse the repository at this point in the history
kafka_info: Add topics-configuration to kafka_info
  • Loading branch information
ryarnyah authored Aug 27, 2021
2 parents 0b97e43 + 5d0566c commit bac9c5e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,26 @@ Playbook:
}
}
```
#### Topics-Configuration
Playbook:
```yaml
- name: get topics configuration
kafka_info:
resource: "topic-config"
bootstrap_servers: "{{ ansible_ssh_host }}"
api_version: "{{ kafka_api_version }}"
register: topics
```
`topics` will be:
```json
{
"<results_key>": {
"test_1600292339": {
"retention.ms": "66574936"
}
}
}
```
#### Consumer groups
Playbook:
```yaml
Expand Down
11 changes: 9 additions & 2 deletions library/kafka_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
description:
- 'the type of resource to get information about'
required: True
choices: [topic, broker, consumer_group, acl]
choices: [topic, broker, consumer_group, acl,
topic-config]
''' + DOCUMENTATION_COMMON

EXAMPLES = '''
Expand All @@ -58,7 +59,13 @@ def main():
module = AnsibleModule(
argument_spec=dict(
resource=dict(
choices=['topic', 'broker', 'consumer_group', 'acl'],
choices=[
'topic',
'broker',
'consumer_group',
'acl',
'topic-config'
],
required=True
),
**module_commons
Expand Down
15 changes: 15 additions & 0 deletions module_utils/kafka_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,20 @@ def get_brokers_resource(self):
brokers[broker.nodeId] = broker._asdict()
return brokers

def get_topics_config(self):
"""
Return a dict object containing information about configuration
of topics and partitions, and following this structure:
{
"test_1600378061": {
"config-key": "config-value"
}
}
"""
topics = self.get_topics()
return self.get_config_for_topics({
topic: {} for topic in topics})

def get_topics_resource(self):
"""
Return a dict object containing information about topics and partitions,
Expand Down Expand Up @@ -1238,6 +1252,7 @@ def get_acls_resource(self):
def resource_to_func(self):
return {
'topic': self.get_topics_resource,
'topic-config': self.get_topics_config,
'broker': self.get_brokers_resource,
'consumer_group': self.get_consumer_groups_resource,
'acl': self.get_acls_resource
Expand Down
33 changes: 33 additions & 0 deletions molecule/default/tests/test_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,39 @@ def test_kafka_info_topic(host):
assert not partition_info['unavailable_partition']


def test_kafka_info_topics_config(host):
"""
Check if can get config on topic.
"""
# Given
topic_name = get_topic_name()
test_topic_configuration = topic_defaut_configuration.copy()
test_topic_configuration.update({
'options': {
'retention.ms': 66574936
}
})
ensure_topic(
host,
test_topic_configuration,
topic_name
)
time.sleep(0.3)
# When
results = call_kafka_info(
host,
{
'resource': 'topic-config'
}
)
# Then
for r in results:
assert topic_name in r['ansible_module_results']
for name, topic_config in r['ansible_module_results'].items():
if name == topic_name:
assert int(topic_config['retention.ms']) == 66574936


def test_kafka_info_brokers(host):
"""
Check if can get info on brokers
Expand Down

0 comments on commit bac9c5e

Please sign in to comment.