Skip to content

Commit

Permalink
Merge pull request #288 from datamel/pass-username-to-cylc-flow
Browse files Browse the repository at this point in the history
Pass authenticated user to cylc-flow
  • Loading branch information
MetRonnie authored Jan 25, 2022
2 parents 16724e1 + 27f9d79 commit dbfd865
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 32 deletions.
33 changes: 5 additions & 28 deletions cylc/uiserver/resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ def __init__(self, data, log, **kwargs):
# Mutations
async def mutator(self, info, *m_args):
"""Mutate workflow."""
_, w_args, _ = m_args
req_meta = {}
_, w_args, _, _ = m_args
req_meta['auth_user'] = info.context.get(
'current_user', 'unknown user')
w_ids = [
flow[WORKFLOW].id
for flow in await self.get_workflows_data(w_args)]
Expand All @@ -214,7 +217,7 @@ async def mutator(self, info, *m_args):
'variables': variables,
}
return await self.workflows_mgr.multi_request(
'graphql', w_ids, graphql_args
'graphql', w_ids, graphql_args, req_meta=req_meta
)

async def service(self, info, *m_args):
Expand All @@ -224,29 +227,3 @@ async def service(self, info, *m_args):
self.workflows_mgr,
log=self.log
)

async def nodes_mutator(self, info, *m_args):
"""Mutate node items of associated workflows."""
_, _, w_args, _ = m_args
w_ids = [
flow[WORKFLOW].id
for flow in await self.get_workflows_data(w_args)]
if not w_ids:
return [{
'response': (False, 'No matching workflows')}]
# Pass the multi-node request to the workflow GraphQL endpoints
_, variables, _, _ = info.context.get('graphql_params')

# Create a modified request string,
# containing only the current mutation/field.
operation_ast = deepcopy(info.operation)
operation_ast.selection_set.selections = info.field_asts

graphql_args = {
'request_string': print_ast(operation_ast),
'variables': variables,
}
multi_args = {w_id: graphql_args for w_id in w_ids}
return await self.workflows_mgr.multi_request(
'graphql', w_ids, multi_args=multi_args
)
4 changes: 3 additions & 1 deletion cylc/uiserver/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def __init__(self):
def will_return(self, returns):
self.returns = returns

async def async_request(self, command, args=None, timeout=None):
async def async_request(
self, command, args=None, timeout=None, req_meta=None
):
if (
inspect.isclass(self.returns)
and issubclass(self.returns, Exception)
Expand Down
17 changes: 14 additions & 3 deletions cylc/uiserver/workflows_mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async def workflow_request(
req_context=None,
*,
log=None,
req_meta=None
):
"""Workflow request command.
Expand All @@ -62,6 +63,7 @@ async def workflow_request(
args (dict): Endpoint arguments.
timeout (float): Client request timeout (secs).
req_context (str): A string to identifier.
req_meta (dict): Meta data related to request, e.g. auth_user
Returns:
tuple: (req_context, result)
Expand All @@ -70,7 +72,8 @@ async def workflow_request(
if req_context is None:
req_context = command
try:
result = await client.async_request(command, args, timeout)
result = await client.async_request(
command, args, timeout, req_meta=req_meta)
return (req_context, result)
except ClientTimeout as exc:
if log:
Expand Down Expand Up @@ -271,13 +274,16 @@ async def multi_request(
workflows,
args=None,
multi_args=None,
timeout=None
timeout=None,
req_meta=None
):
"""Send requests to multiple workflows."""
if args is None:
args = {}
if multi_args is None:
multi_args = {}
if req_meta is None:
req_meta = {}
req_args = {
w_id: (
self.active[w_id]['req_client'],
Expand All @@ -288,7 +294,12 @@ async def multi_request(
if w_id in self.active
}
gathers = [
workflow_request(req_context=info, *request_args, log=self.log)
workflow_request(
req_context=info,
*request_args,
log=self.log,
req_meta=req_meta
)
for info, request_args in req_args.items()
]
results = await asyncio.gather(*gathers, return_exceptions=True)
Expand Down

0 comments on commit dbfd865

Please sign in to comment.