Skip to content

Commit

Permalink
Merge pull request #171 from canonical/server-get-agent-data
Browse files Browse the repository at this point in the history
Add api to allow retrieving agent data
  • Loading branch information
plars authored Dec 12, 2023
2 parents aa83f42 + aa914df commit d37d069
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
16 changes: 16 additions & 0 deletions server/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions server/src/api/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down
8 changes: 8 additions & 0 deletions server/src/api/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<agent_name>")
@v1.input(schemas.AgentIn, location="json")
def agents_post(agent_name, json_data):
Expand Down
22 changes: 21 additions & 1 deletion server/tests/test_v1.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

0 comments on commit d37d069

Please sign in to comment.