mirror of
https://github.com/ansible-collections/community.docker.git
synced 2026-07-29 11:55:04 +00:00
Rewrite the docker_containers inventory plugin (#413)
* Rewrite the docker_containers inventory plugin. * Improve error messages.
This commit is contained in:
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user