def test_transform_connections__p2p(all_agents, p2p_connections): assert transform.transform_connections(all_agents, p2p_connections, "P2P") == { "de-hetzner-db01": { "type": "endpoint", "id": 1, "state": "present", "services": [], "connect_to": { "de-aws-be01": { "type": "endpoint", "services": [], "id": 2, }, }, }, "de-aws-lb01": { "type": "endpoint", "state": "present", "services": [], "id": 3, "connect_to": { "kr-aws-dns01": { "type": "endpoint", "services": [], "id": 4, }, }, }, }
def test_transform_connections__p2m(all_agents, p2m_connections): assert transform.transform_connections(all_agents, p2m_connections, "P2M") == { "auto gen 1": { "type": "endpoint", "id": 1, "state": "present", "services": [], "connect_to": { "auto gen 4": { "state": "present", "id": 4, "type": "endpoint", "services": [], }, "auto gen 5": { "state": "present", "id": 5, "type": "endpoint", "services": [], }, "auto gen 6": { "state": "present", "id": 6, "type": "endpoint", "services": [], }, }, }, }
def test_transform_connections__mesh_tagged_multiple(all_agents, mesh_connections): connections = [ { **i, "agent_1": { **i["agent_1"], "agent_tags": [ {"agent_tag_name": "test"}, {"agent_tag_name": "TEST"}, ], }, "agent_2": { **i["agent_2"], "agent_tags": [ {"agent_tag_name": "test"}, {"agent_tag_name": "TEST"}, ], }, } for i in mesh_connections ] update_all_tags(all_agents, connections) assert transform.transform_connections(all_agents, connections, "MESH") == { "test": { "type": "tag", "state": "present", "services": [], }, "TEST": { "type": "tag", "state": "present", "services": [], }, }
def test_transform_connections__mesh(all_agents, mesh_connections): assert transform.transform_connections(all_agents, mesh_connections, "MESH") == { "auto gen 10": { "type": "endpoint", "state": "present", "services": [], "id": 10, }, "auto gen 11": { "type": "endpoint", "state": "present", "services": [], "id": 11, }, "auto gen 12": { "type": "endpoint", "state": "present", "services": [], "id": 12, }, "auto gen 13": { "type": "endpoint", "state": "present", "services": [], "id": 13, }, }
def test_transform_connections__p2m_tagged_multiple(all_agents, p2m_connections): connections = [ { **i, "agent_2": { **i["agent_2"], "agent_tags": [ {"agent_tag_name": "test"}, {"agent_tag_name": "TEST"}, ], }, } for i in p2m_connections ] update_all_tags(all_agents, connections) assert transform.transform_connections(all_agents, connections, "P2M") == { "auto gen 1": { "type": "endpoint", "id": 1, "state": "present", "services": [], "connect_to": { "test": { "state": "present", "type": "tag", "services": [], }, "TEST": { "state": "present", "type": "tag", "services": [], }, }, }, }
def test_transform_connections__p2m_reversed(all_agents, p2m_connections): connections = [ { **i, "agent_1": i["agent_2"], "agent_2": i["agent_1"], } for i in p2m_connections ] assert transform.transform_connections(all_agents, connections, "P2M") == { "auto gen 1": { "type": "endpoint", "id": 1, "state": "present", "services": [], "connect_to": { "auto gen 4": { "state": "present", "id": 4, "type": "endpoint", "services": [], }, "auto gen 5": { "state": "present", "id": 5, "type": "endpoint", "services": [], }, "auto gen 6": { "state": "present", "id": 6, "type": "endpoint", "services": [], }, }, }, }
def export_connections(api, all_agents, network, net_agents, connections, topology): topology = Topology.P2M if not topology else topology.upper() ids = [ connection["agent_connection_group_id"] for connection in connections ] if ids: connections_services = sdk.utils.BatchedRequestFilter( sdk.ConnectionsApi(api).v1_network_connections_services_get, MAX_QUERY_FIELD_SIZE, )(filter=ids, _preload_content=False)["data"] connection_services = { connection["agent_connection_group_id"]: connection for connection in connections_services } net_connections = [{ **connection, "agent_connection_services": connection_services.get(connection["agent_connection_group_id"], {}), } for connection in connections] transformed_connections = transform.transform_connections( all_agents, net_connections, topology if topology else network[fields.ConfigFields.TOPOLOGY], ) if transformed_connections: network[fields.ConfigFields.CONNECTIONS] = transformed_connections network[fields.ConfigFields.TOPOLOGY] = topology # NOTE: Currently, SDN is disabled. if fields.ConfigFields.USE_SDN in network: del network[fields.ConfigFields.USE_SDN] # Filter out unused endpoints used_endpoints = [ con[agent]["agent_id"] for con in net_connections for agent in ("agent_1", "agent_2") ] unused_endpoints = [id for id in net_agents if id not in used_endpoints] if unused_endpoints: agents_services = sdk.utils.BatchedRequestFilter( sdk.AgentsApi(api).v1_network_agents_services_get, MAX_QUERY_FIELD_SIZE, )(filter=unused_endpoints, _preload_content=False)["data"] agent_services = defaultdict(list) for agent_id, agent in zip(unused_endpoints, agents_services): id = (agent["agent_id"] if "agent_id" in agent and agent["agent_id"] else agent_id) agent_services[id].append(agent) network[fields.ConfigFields.ENDPOINTS] = { all_agents[id]["agent_name"]: { fields.ConfigFields.ID: id, fields.ConfigFields.SERVICES: [ service["agent_service_name"] for service in agent_services[id] ], fields.ConfigFields.TAGS: [ tag["agent_tag_name"] for tag in all_agents[id].get("agent_tags", []) ], } for id in unused_endpoints } return network