Skip to content

Commit

Permalink
❇️ Added command to show eHerkenning services.
Browse files Browse the repository at this point in the history
  • Loading branch information
joeribekker committed Feb 27, 2024
1 parent 970d3ed commit b88c7eb
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions digid_eherkenning/management/commands/show_services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from django.core.management import BaseCommand

import requests
from lxml import etree

SERVICE_CATALOG_URLS = {
"prod": "https://aggregator.etoegang.nl/1.13/servicecatalog.xml",
"preprod": "https://aggregator.etoegang.nl/test/1.13/servicecatalog.xml",
}
NAMESPACES = {"esc": "urn:etoegang:1.13:service-catalog"}


class Command(BaseCommand):
help = "Updates the stored metadata file and prepopulates the db fields."

def add_arguments(self, parser):
parser.add_argument(
"env",
type=str,
choices=["prod", "preprod"],
default="preprod",
help="Indicate the service catalogs to search.",
)
parser.add_argument(
"oin",
type=str,
help="The organisation OIN (government identification number).",
)

def handle(self, **options):
env = options.get("env")
oin = options.get("oin")

service_catalog_url = SERVICE_CATALOG_URLS.get(env)

response = requests.get(service_catalog_url)
if response.status_code != 200:
self.stderr.write("Could not retrieve service catalog.")
return

# etree.parse didn't like a URL directly.
tree = etree.fromstring(response.content)

service_provider = tree.xpath(
f"esc:ServiceProvider[esc:ServiceProviderID/text()='{oin}']",
namespaces=NAMESPACES,
)

if len(service_provider) > 1:
self.stderr.write("Found multiple service providers for this OIN.")
return
elif len(service_provider) < 1:
self.stderr.write("Found no service providers for this OIN.")
return

service_provider = service_provider[0]
service_definitions = service_provider.xpath(
"esc:ServiceDefinition", namespaces=NAMESPACES
)

org_name = service_provider.xpath(
"esc:OrganizationDisplayName[@xml:lang='nl']/text()", namespaces=NAMESPACES
)[0]

self.stdout.write(f"Service provider organization: {org_name}")
for sd in service_definitions:
sd_uuid = sd.xpath("esc:ServiceUUID/text()", namespaces=NAMESPACES)[0]
sd_name = sd.xpath(
"esc:ServiceName[@xml:lang='nl']/text()", namespaces=NAMESPACES
)[0]
sd_description = sd.xpath(
"esc:ServiceDescription[@xml:lang='nl']/text()", namespaces=NAMESPACES
)[0]

self.stdout.write(f"+-- Service definition: {sd_name} ({sd_description})")

service_instance_ids = service_provider.xpath(
f"esc:ServiceInstance[esc:InstanceOfService[text()='{sd_uuid}']]/esc:ServiceID/text()",
namespaces=NAMESPACES,
)

for si in service_instance_ids:
self.stdout.write(f" +-- Service ID: {si}")

0 comments on commit b88c7eb

Please sign in to comment.