Rewrite the docker_containers inventory plugin (#413)

* Rewrite the docker_containers inventory plugin.

* Improve error messages.
This commit is contained in:
Felix Fontein
2022-07-06 21:48:32 +02:00
committed by GitHub
parent c3a76007d0
commit 23a90668c9
3 changed files with 33 additions and 34 deletions
@@ -93,29 +93,22 @@ def create_get_option(options, default=False):
class FakeClient(object):
def __init__(self, *hosts):
self.hosts = dict()
self.list_reply = []
self.get_results = {}
list_reply = []
for host in hosts:
self.list_reply.append({
list_reply.append({
'Id': host['Id'],
'Names': [host['Name']] if host['Name'] else [],
'Image': host['Config']['Image'],
'ImageId': host['Image'],
})
self.hosts[host['Name']] = host
self.hosts[host['Id']] = host
self.get_results['/containers/{0}/json'.format(host['Name'])] = host
self.get_results['/containers/{0}/json'.format(host['Id'])] = host
self.get_results['/containers/json'] = list_reply
def containers(self, all=False):
return list(self.list_reply)
def inspect_container(self, id):
return self.hosts[id]
def port(self, container, port):
host = self.hosts[container['Id']]
network_settings = host.get('NetworkSettings') or dict()
ports = network_settings.get('Ports') or dict()
return ports.get('{0}/tcp'.format(port)) or []
def get_json(self, url, *param, **kwargs):
url = url.format(*param)
return self.get_results[url]
def test_populate(inventory, mocker):