Skip to content

Commit

Permalink
Merge pull request #501 from vantage6/feature/endpoint-vpn-addresses
Browse files Browse the repository at this point in the history
Added new endpoint /vpn/algorithm/addresses to retrieve the IP addres…
  • Loading branch information
frankcorneliusmartin committed Feb 9, 2023
2 parents e173328 + 569f495 commit e6d4a15
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
2 changes: 2 additions & 0 deletions vantage6-server/vantage6/server/resource/__init__.py
Expand Up @@ -91,6 +91,8 @@ def decorator(*args, **kwargs):
# log.debug(f"Endpoint accessed as {g.type}")

if g.type not in types:
# FIXME BvB 23-10-19: user gets a 500 error, would be better to
# get an error message with 400 code
msg = f"{g.type}s are not allowed to access {request.url} " \
f"({request.method})"
log.warning(msg)
Expand Down
106 changes: 105 additions & 1 deletion vantage6-server/vantage6/server/resource/port.py
Expand Up @@ -16,6 +16,7 @@
only_for,
ServicesResources
)
from vantage6.server import db
from vantage6.server.resource.pagination import Pagination
from vantage6.server.resource.common._schema import PortSchema
from vantage6.server.model import (
Expand All @@ -25,7 +26,7 @@
Task
)
from vantage6.server.model.base import DatabaseSessionManager

from vantage6.server.resource import with_container

module_name = logger_name(__name__)
log = logging.getLogger(module_name)
Expand All @@ -50,6 +51,13 @@ def setup(api, api_base, services):
methods=('GET',),
resource_class_kwargs=services
)
api.add_resource(
VPNAddress,
api_base + '/vpn/algorithm/addresses',
endpoint='vpn_address',
methods=('GET',),
resource_class_kwargs=services
)


# Schemas
Expand Down Expand Up @@ -354,3 +362,99 @@ def get(self, id):
s = port_schema

return s.dump(port, many=False).data, HTTPStatus.OK


class VPNAddress(ServicesResources):

@with_container
def get(self):
"""
Get a list of the addresses (IP + port) and labels of algorithm
containers in the same task as the authenticating container.
---
description: >-
Returns a dictionary of addresses of algorithm containers in the same
task.\n
### Permission Table\n
|Rule name|Scope|Operation|Assigned to node|Assigned to container|
Description|\n
|--|--|--|--|--|--|\n
|Port|Global|View|❌|❌|View any result|\n
|Port|Organization|View|❌|✅|View the ports of your
organizations collaborations|\n
Not accessible to users.
parameters:
- in: path
name: label
schema:
type: string
description: Algorithm port label to filter by
- in: path
name: include_children
schema:
type: boolean
description: Include the addresses of subtasks
- in: path
name: include_parent
schema:
type: boolean
description: Include the addresses of parent tasks
responses:
200:
description: Ok
security:
- bearerAuth: []
tags: ["VPN"]
"""
task_id = g.container['task_id']
task_ids = [task_id]

task = db.Task.get(task_id)

# include child tasks if requested
if request.args.get('include_children', False):
subtasks = g.session.query(db.Task).filter(
db.Task.parent_id == task_id
).all()
task_ids.extend([t.id for t in subtasks])

# include parent task if requested
if request.args.get('include_parent', False):
parent = g.session.query(db.Task).filter(
db.Task.id == task.parent_id
).one_or_none()
if parent:
task_ids.append(parent.id)

# get all ports for the tasks requested
q = g.session.query(AlgorithmPort)\
.join(Result)\
.filter(Result.task_id.in_(task_ids))\

# filter by label if requested
filter_label = request.args.get('label')
if filter_label:
q = q.filter(AlgorithmPort.label == filter_label)

ports = q.all()

# combine data from ports and nodes
addresses = []
for port in ports:
d = {
'port': port.port,
'label': port.label,
'ip': port.result.node.ip,
'organization_id': port.result.organization_id,
'task_id': port.result.task_id
}
addresses.append(d)

return {'addresses': addresses}, HTTPStatus.OK

0 comments on commit e6d4a15

Please sign in to comment.