diff --git a/shotgun/config.py b/shotgun/config.py index d2a36a5..dbecf9a 100644 --- a/shotgun/config.py +++ b/shotgun/config.py @@ -17,6 +17,8 @@ import copy import logging import time +import six + from shotgun import settings @@ -30,11 +32,13 @@ class Config(object): self.offline_hosts = set() self.objs = deque() self.try_again = deque() - for properties in self.data.get("dump", {}).itervalues(): - for host in properties.get("hosts", []): - for object_ in properties.get("objects", []): - object_["host"] = host - self.objs.append(copy.copy(object_)) + for properties in six.itervalues(self.data.get('dump', {})): + hosts = properties.get('hosts') or [{}] + for obj in properties.get('objects', []): + for h in hosts: + obj_new = copy.deepcopy(obj) + obj_new['host'] = h + self.objs.append(obj_new) def _timestamp(self, name): return "{0}-{1}".format( @@ -68,7 +72,7 @@ class Config(object): @staticmethod def get_network_address(obj): """Returns network address of object.""" - return obj["host"].get('address', '127.0.0.1') + return obj['host'].get('address') or obj['host'].get('hostname') def on_network_error(self, obj): """Lets the object to have another attempt for being proccessed.""" diff --git a/shotgun/test/test_config.py b/shotgun/test/test_config.py index b4a8bdf..39e7cee 100644 --- a/shotgun/test/test_config.py +++ b/shotgun/test/test_config.py @@ -94,7 +94,8 @@ class TestConfig(base.BaseTestCase): obj = conf.objects.next() self.assertEqual('10.109.2.2', conf.get_network_address(obj)) - def test_get_network_address_default(self): + def test_get_network_address_hostname(self): + hostname = "fuel.tld" data = { "dump": { "master": { @@ -103,12 +104,39 @@ class TestConfig(base.BaseTestCase): "type": "dir"}, ], "hosts": [{"ssh-key": "/root/.ssh/id_rsa", - "hostname": "fuel.tld"}]}, + "hostname": hostname}]}, } } conf = Config(data) obj = conf.objects.next() - self.assertEqual('127.0.0.1', conf.get_network_address(obj)) + self.assertEqual(hostname, conf.get_network_address(obj)) + + def test_get_network_address_absent_address_and_hostname(self): + data = { + "dump": { + "master": { + "objects": + [{"path": "/etc/nailgun", + "type": "dir"}]}, + } + } + conf = Config(data) + obj = conf.objects.next() + self.assertIsNone(conf.get_network_address(obj)) + + def test_obj_without_hosts(self): + data = { + "dump": { + "fake_role1": { + "objects": + [{"fake_obj_1": '1'}, {"fake_obj_2": '2'}]}, + } + } + conf = Config(data) + expected_objs = [ + {'host': {}, 'fake_obj_1': '1'}, + {'host': {}, 'fake_obj_2': '2'}] + self.assertItemsEqual(expected_objs, conf.objs) def test_init(self): data = {