Skip to content

Commit

Permalink
Add function and event to retrieve from json-schema attributes to sho…
Browse files Browse the repository at this point in the history
…w in the form
  • Loading branch information
giosava94 committed Jul 17, 2024
1 parent a4359c7 commit e652b41
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
8 changes: 6 additions & 2 deletions examples/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ class SiteAdminNameSpace(ClientNamespace):
def on_connect(self):
print(f"Connection to namespace {self.namespace} established")
self.emit("list_provider_federation_requests", {"username": ""})
self.emit("get_form")

def on_connect_error(self, data):
print(f"Failed to connect to namespace {self.namespace}", data)

def on_list_provider_federation_requests(self, data):
print(f"Message received with {data}")
self.disconnect()

def on_get_form(self, data):
print(f"Message received with {data}")

def on_disconnect(self):
print(f"Disconnected from namespace {self.namespace}")
Expand All @@ -41,4 +44,5 @@ def on_disconnect(self):
transports=["websocket", "polling"],
auth={"token": args.token},
)
sio.wait()
sio.sleep(5)
sio.disconnect()
44 changes: 44 additions & 0 deletions fed_mng/socketio/site_admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any, Literal

import requests
from socketio import AsyncNamespace

from fed_mng.socketio.utils import validate_auth_on_connect
Expand Down Expand Up @@ -75,3 +76,46 @@ async def on_delete_federated_provider(self, sid, data):
"""
print("Received data ", data)
# TODO: Start a new workflow instance to delete a provider

async def on_get_form(self, id):
"""Send a dict with the details to use to submit a new provider request."""
resp = requests.get(
"https://raw.githubusercontent.com/infn-datacloud/federation-registry-feeder/main/json-config-schemas/provider-config-schema.json"
)
idp_data = self._resolve_defs(
resp.json()["properties"].pop("trusted_idps"), resp.json()["$defs"]
)
print("Identity provider section: %r" % idp_data)
provider_data = self._resolve_defs(
resp.json()["properties"]["openstack"], resp.json()["$defs"]
)
print("Provider section: %r" % provider_data)
await self.emit("get_form", {"idp": idp_data, "provider": provider_data})

def _resolve_defs(
self, data: dict[str, Any], definitions: dict[str, dict]
) -> dict[str, dict]:
"""Convert the json schema in a more suitable dict.
Expand $ref keys with the corresponding definitions.
Move the required key inside the corresponding dict.
Return the resolved dict.
"""
resolved_data = {}

# Expand references
for key, value in data.items():
if isinstance(value, dict):
value = definitions.get(key) if value.get("$ref", None) else value
resolved_data[key] = self._resolve_defs(value, definitions)
else:
resolved_data[key] = value

# Add required flag to target item
required_keys = resolved_data.pop("required", None)
if required_keys is not None and isinstance(required_keys, list):
for k in required_keys:
resolved_data["properties"][k]["required"] = True

return resolved_data

0 comments on commit e652b41

Please sign in to comment.