diff --git a/app/models.py b/app/models.py index e8e236222d4697de3672d20d382ab9613c0ea37f..f01bbe72ace9a7d8630df66dc23a18b9573cb395 100644 --- a/app/models.py +++ b/app/models.py @@ -821,6 +821,14 @@ class Host(CreatedMixin, db.Model): return True return False + @property + def model(self): + """Return the model of the first linked item""" + try: + return utils.format_field(self.items[0].model) + except IndexError: + return None + def __str__(self): return str(self.name) @@ -854,6 +862,7 @@ class Host(CreatedMixin, db.Model): { "name": self.name, "device_type": str(self.device_type), + "model": self.model, "description": self.description, "items": [str(item) for item in self.items], "interfaces": [str(interface) for interface in self.interfaces], diff --git a/tests/functional/test_api.py b/tests/functional/test_api.py index 7a04465e82af6f02e8e629d65a8b34ac6605b17f..f6b5ede4a20b5424b74c169a3e956a3c72dc711e 100644 --- a/tests/functional/test_api.py +++ b/tests/functional/test_api.py @@ -1148,6 +1148,24 @@ def test_get_hosts_with_ansible_vars(client, host_factory, readonly_token): assert response.json[0]["ansible_vars"] == vars +def test_get_hosts_with_model( + client, model_factory, item_factory, host_factory, readonly_token +): + host1 = host_factory() + model1 = model_factory(name="EX3400") + item_factory(model=model1, host_id=host1.id) + response = get(client, f"{API_URL}/network/hosts", token=readonly_token) + assert response.status_code == 200 + assert response.json[0]["model"] == "EX3400" + + +def test_get_hosts_with_no_model(client, host_factory, readonly_token): + host_factory() + response = get(client, f"{API_URL}/network/hosts", token=readonly_token) + assert response.status_code == 200 + assert response.json[0]["model"] is None + + def test_create_host(client, device_type_factory, user_token): device_type = device_type_factory(name="Virtual") # check that name and device_type are mandatory @@ -1172,6 +1190,7 @@ def test_create_host(client, device_type_factory, user_token): "id", "name", "device_type", + "model", "description", "items", "interfaces", diff --git a/tests/functional/test_models.py b/tests/functional/test_models.py index 62982642fe21aa678ee312bd532b8a2efaaf4b1b..2c6233b019fad68c292e427744c4522dfa3695d2 100644 --- a/tests/functional/test_models.py +++ b/tests/functional/test_models.py @@ -177,3 +177,10 @@ def test_ansible_groups(ansible_group_factory, host_factory): host1.ansible_groups.append(group2) assert host1.ansible_groups == [group1, group2] assert group2.hosts == [host1] + + +def test_host_model(model_factory, item_factory, host_factory): + host1 = host_factory() + model1 = model_factory(name="EX3400") + item_factory(model=model1, host_id=host1.id) + assert host1.model == "EX3400"