diff --git a/app/api/inventory.py b/app/api/inventory.py index ff573d9475763a090d6148990b2b9c471214bb40..c2b0102b0a9f5b47d97ebd9c98668d74d6e19fe9 100644 --- a/app/api/inventory.py +++ b/app/api/inventory.py @@ -269,3 +269,26 @@ def create_status(): :jsonparam description: (optional) description """ return create_generic_model(models.Status) + + +@bp.route("/macs") +@login_required +def get_macs(): + """Return mac addresses + + .. :quickref: Inventory; Get mac addresses + """ + return get_generic_model(models.Mac, order_by=models.Mac.address) + + +@bp.route("/macs", methods=["POST"]) +@login_groups_accepted("admin", "inventory") +def create_macs(): + """Create a new mac address + + .. :quickref: Inventory; Create new mac address + + :jsonparam address: MAC address + :jsonparam item_id: (optional) linked item primary key + """ + return create_generic_model(models.Mac, mandatory_fields=("address",)) diff --git a/app/api/network.py b/app/api/network.py index 706cbce9433504ca0a422ed7f9c3d26074bddc43..73c98c702b7c3f60e57f19667a3f44ca4ff1eb8e 100644 --- a/app/api/network.py +++ b/app/api/network.py @@ -232,29 +232,6 @@ def delete_host(host_id): return utils.delete_generic_model(models.Host, host_id) -@bp.route("/macs") -@login_required -def get_macs(): - """Return mac addresses - - .. :quickref: Network; Get mac addresses - """ - return utils.get_generic_model(models.Mac, order_by=models.Mac.address) - - -@bp.route("/macs", methods=["POST"]) -@login_groups_accepted("admin", "network") -def create_macs(): - """Create a new mac address - - .. :quickref: Network; Create new mac address - - :jsonparam address: MAC address - :jsonparam item_id: (optional) linked item primary key - """ - return utils.create_generic_model(models.Mac, mandatory_fields=("address",)) - - @bp.route("/domains") @login_required def get_domains(): diff --git a/tests/functional/test_api.py b/tests/functional/test_api.py index 8c2b5ae02ac178aba2b78f716f624f4d7a54bc88..12609a4db29ec8773f5806b50b0fbb8048a4d072 100644 --- a/tests/functional/test_api.py +++ b/tests/functional/test_api.py @@ -23,24 +23,24 @@ ENDPOINT_MODEL = { "inventory/locations": models.Location, "inventory/statuses": models.Status, "inventory/items": models.Item, + "inventory/macs": models.Mac, "network/networks": models.Network, "network/interfaces": models.Interface, "network/hosts": models.Host, "network/groups": models.AnsibleGroup, - "network/macs": models.Mac, "network/domains": models.Domain, "network/cnames": models.Cname, } GENERIC_GET_ENDPOINTS = [ key for key in ENDPOINT_MODEL.keys() - if key.startswith("inventory") and key != "inventory/items" + if key.startswith("inventory") and key not in ("inventory/items", "inventory/macs") ] GENERIC_CREATE_ENDPOINTS = [ key for key in ENDPOINT_MODEL.keys() if key.startswith("inventory") - and key not in ("inventory/items", "inventory/actions") + and key not in ("inventory/items", "inventory/actions", "inventory/macs") ] CREATE_AUTH_ENDPOINTS = [ key for key in ENDPOINT_MODEL.keys() if key != "inventory/actions" @@ -1191,7 +1191,7 @@ def test_get_macs(client, mac_factory, readonly_token): mac1 = mac_factory() mac2 = mac_factory() - response = get(client, f"{API_URL}/network/macs", token=readonly_token) + response = get(client, f"{API_URL}/inventory/macs", token=readonly_token) assert response.status_code == 200 assert len(response.get_json()) == 2 check_input_is_subset_of_response(response, (mac1.to_dict(), mac2.to_dict())) @@ -1200,17 +1200,17 @@ def test_get_macs(client, mac_factory, readonly_token): def test_create_mac(client, item_factory, user_token): item = item_factory() # check that address is mandatory - response = post(client, f"{API_URL}/network/macs", data={}, token=user_token) + response = post(client, f"{API_URL}/inventory/macs", data={}, token=user_token) check_response_message(response, "Missing mandatory field 'address'", 422) data = {"address": "b5:4b:7d:a4:23:43"} - response = post(client, f"{API_URL}/network/macs", data=data, token=user_token) + response = post(client, f"{API_URL}/inventory/macs", data=data, token=user_token) assert response.status_code == 201 assert {"id", "address", "item"} == set(response.get_json().keys()) assert response.get_json()["address"] == data["address"] # Check that address shall be unique - response = post(client, f"{API_URL}/network/macs", data=data, token=user_token) + response = post(client, f"{API_URL}/inventory/macs", data=data, token=user_token) check_response_message( response, "(psycopg2.IntegrityError) duplicate key value violates unique constraint", @@ -1219,7 +1219,7 @@ def test_create_mac(client, item_factory, user_token): # Check that all parameters can be passed data2 = {"address": "b5:4b:7d:a4:23:44", "item_id": item.id} - response = post(client, f"{API_URL}/network/macs", data=data2, token=user_token) + response = post(client, f"{API_URL}/inventory/macs", data=data2, token=user_token) assert response.status_code == 201 # check that all items were created @@ -1229,7 +1229,7 @@ def test_create_mac(client, item_factory, user_token): @pytest.mark.parametrize("address", ("", "foo", "b5:4b:7d:a4:23")) def test_create_mac_invalid_address(address, client, user_token): data = {"address": address} - response = post(client, f"{API_URL}/network/macs", data=data, token=user_token) + response = post(client, f"{API_URL}/inventory/macs", data=data, token=user_token) check_response_message( response, f"'{address}' does not appear to be a MAC address", 422 )