diff --git a/server/README.rst b/server/README.rst index 317f8a7f..1cb08b7b 100644 --- a/server/README.rst +++ b/server/README.rst @@ -305,3 +305,19 @@ server will only return one job. $ curl http://localhost:8000/v1/job/00000000-0000-0000-0000-000000000000/action \ -X POST --header "Content-Type: application/json" \ --data '{ "action":"cancel" }' + +**[GET] /v1/agents/data** - retrieve all agent data + +- Status Codes: + + - HTTP 200 (OK) + +- Returns: + + JSON data for all known agents, useful for external systems that need to gather this information + +- Example: + + .. code-block:: console + + $ curl -X GET http://localhost:8000/v1/agents/data diff --git a/server/src/api/schemas.py b/server/src/api/schemas.py index 705dc5ef..fed68f39 100644 --- a/server/src/api/schemas.py +++ b/server/src/api/schemas.py @@ -31,6 +31,15 @@ class AgentIn(Schema): log = fields.List(fields.String(), required=False) +class AgentOut(Schema): + """Agent data input schema""" + + state = fields.String(required=False) + queues = fields.List(fields.String(), required=False) + location = fields.String(required=False) + job_id = fields.String(required=False) + + class ActionIn(Schema): """Action data input schema""" diff --git a/server/src/api/v1.py b/server/src/api/v1.py index eff95759..42e74d69 100644 --- a/server/src/api/v1.py +++ b/server/src/api/v1.py @@ -365,6 +365,14 @@ def images_post(): return "OK" +@v1.get("/agents/data") +@v1.output(schemas.AgentOut) +def agents_get_all(): + """Get all agent data""" + agents = mongo.db.agents.find({}, {"_id": False, "log": False}) + return jsonify(list(agents)) + + @v1.post("/agents/data/") @v1.input(schemas.AgentIn, location="json") def agents_post(agent_name, json_data): diff --git a/server/tests/test_v1.py b/server/tests/test_v1.py index d0e19b78..7fd87575 100644 --- a/server/tests/test_v1.py +++ b/server/tests/test_v1.py @@ -1,4 +1,4 @@ -# Copyright (C) 2016-2022 Canonical +# Copyright (C) 2016-2023 Canonical # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -401,3 +401,23 @@ def test_agents_post_bad(mongo_app): assert 422 == output.status_code assert "Validation error" in output.text + + +def test_get_agents_data(mongo_app): + """Test api to retrieve agent data""" + app, _ = mongo_app + agent_name = "agent1" + agent_data = { + "state": "provision", + "queues": ["q1", "q2"], + "location": "here", + } + output = app.post(f"/v1/agents/data/{agent_name}", json=agent_data) + assert 200 == output.status_code + + # Get the agent data + output = app.get("/v1/agents/data") + assert 200 == output.status_code + assert len(output.json) == 1 + for key, value in agent_data.items(): + assert output.json[0][key] == value