Skip to content
Snippets Groups Projects
Commit 96094b9e authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Add model field to /networks/hosts endpoint

JIRA INFRA-414
parent dcde393a
No related branches found
No related tags found
No related merge requests found
......@@ -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],
......
......@@ -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",
......
......@@ -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"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment