diff --git a/inventory_provider/gap.py b/inventory_provider/gap.py index 400463b6f5b6128be8b50ec8107b32893283d512..d8847243c6b98757bea9e8958a26525a2851e8e4 100644 --- a/inventory_provider/gap.py +++ b/inventory_provider/gap.py @@ -31,17 +31,17 @@ def get_token(aai_config: dict) -> str: return response.json()['access_token'] -def make_request(body: dict) -> dict: +def make_request(body: dict, token: str) -> dict: """Make a request to the orchestrator using the given body.""" config = current_app.config['INVENTORY_PROVIDER_CONFIG'] api_url = f'{config["orchestrator"]["url"]}/api/graphql' - headers = {'Authorization': f'Bearer {get_token(config["aai"])}'} + headers = {'Authorization': f'Bearer {token}'} response = requests.post(api_url, headers=headers, json=body) response.raise_for_status() return response.json() -def extract_router_info(device: dict) -> dict or None: +def extract_router_info(device: dict, token: str) -> dict or None: tag_to_key_map = { "RTR": "router", "OFFICE_ROUTER": "officeRouter", @@ -71,7 +71,7 @@ def extract_router_info(device: dict) -> dict or None: }} """ - response = make_request(body={'query': query}) + response = make_request(body={'query': query}, token=token) page_data = response.get('data', {}).get('subscriptions', {}).get('page') if not page_data: @@ -92,7 +92,7 @@ def extract_router_info(device: dict) -> dict or None: def load_routers_from_orchestrator() -> dict: """Gets devices from the orchestrator and returns a dictionary of FQDNs and vendors.""" - + token = get_token(current_app.config['INVENTORY_PROVIDER_CONFIG']['aai']) query = """ { subscriptions( @@ -110,14 +110,14 @@ def load_routers_from_orchestrator() -> dict: } """ routers = {} - response = make_request(body={'query': query}) + response = make_request(body={'query': query}, token=token) try: devices = response['data']['subscriptions']['page'] except (TypeError, KeyError): devices = [] with concurrent.futures.ThreadPoolExecutor() as executor: - futures = [executor.submit(extract_router_info, device) for device in devices] + futures = [executor.submit(extract_router_info, device, token) for device in devices] for future in concurrent.futures.as_completed(futures): router_info = future.result() if router_info is not None: