import example_ardoq_client import argparse import json import urllib.request import urllib.parse def get_BIAN(what): req = urllib.request.Request( "https://bian-modelapisandbox.azurewebsites.net/" + what ) with urllib.request.urlopen(req) as resp: return json.loads(resp.read().decode("utf-8")) parser = argparse.ArgumentParser() parser.add_argument( "--host", metavar="ARDOQ_API_HOST", type=str, help="Required if you are using a custom domain", ) parser.add_argument( "--org", metavar="ARDOQ_ORG_LABEL", type=str, help="The label associated with your org", ) parser.add_argument( "--token", metavar="ARDOQ_API_TOKEN", type=str, help="Your secret API token" ) parser.add_argument( "workspace", metavar="Ardoq Identifier", type=str, help="Ardoq identifier of your workspace", ) args = parser.parse_args() print("Using workspace {}".format(args.workspace)) business_areas = get_BIAN("BusinessAreas") business_domains = get_BIAN("BusinessDomains") api = example_ardoq_client.API( ardoq_api_host=args.host, ardoq_org_label=args.org, ardoq_api_token=args.token ) workspace_context = api.read_workspace_context(args.workspace) typeId_for_name = {} for componentType in workspace_context["componentTypes"]: typeId_for_name[componentType["name"]] = componentType["typeId"] business_area_typeId = typeId_for_name["Business Area"] business_domain_typeId = typeId_for_name["Business Domain"] batch = example_ardoq_client.Batch() batch_ids = set() for business_area in business_areas: business_area_name = business_area.get("name") assert business_area_name not in batch_ids, "A Batch Id must be unique" batch_ids.add(business_area_name) batch.create_component( { "rootWorkspace": args.workspace, "typeId": business_area_typeId, "name": business_area_name, "description": business_area.get("description"), }, batchId=business_area_name, ) for business_domain in business_domains: business_area_name = business_domain.get("businessArea", {}).get("name") if business_area_name not in batch_ids: batch.create_component( { "rootWorkspace": args.workspace, "typeId": business_area_typeId, "name": business_area_name, "description": "Business Area used by Business Domain, but does not exist.", }, batchId=business_area_name, ) batch_ids.add(business_area_name) batch.create_component( { "rootWorkspace": args.workspace, "typeId": business_domain_typeId, "parent": business_area_name, "name": business_domain.get("name"), "description": business_domain.get("description"), } ) print("Sending batch request") response = api.batch(batch.body) components_created = response.get("components", {}).get("created", []) print("Created: {} components".format(len(components_created)))