def create_node(protocol): node = Node("TOR_TEST", "", metadata={"Name": "Test TOR", "Type": "fabric"}) msg = WSMessage("Graph", NodeAddedMsgType, node) protocol.sendWSMessage(msg) node = Node("PORT_TEST", "", metadata={"Name": "Test port", "Type": "fabric"}) msg = WSMessage("Graph", NodeAddedMsgType, node) protocol.sendWSMessage(msg) node = Node("BAD_NODE", "", metadata={"Name": "Bad node"}) msg = WSMessage("Graph", NodeAddedMsgType, node) protocol.sendWSMessage(msg) node = Node("BAD_NETNS", "", metadata={"Name": "Bad netns", "Type": "netns"}) msg = WSMessage("Graph", NodeAddedMsgType, node) protocol.sendWSMessage(msg) edge = Edge("TOR_L2LINK", "", "TOR_TEST", "PORT_TEST", metadata={"RelationType": "layer2"}) msg = WSMessage("Graph", EdgeAddedMsgType, edge) protocol.sendWSMessage(msg) edge = Edge("BAD_LINK", "", "", "", metadata={"RelationType": "layer2"}) msg = WSMessage("Graph", EdgeAddedMsgType, edge) protocol.sendWSMessage(msg)
def onOpen(self): print("WebSocket connection open.") # create a fabric port node = Node("PORT_TEST", "host-test", metadata={"Name": "Test port !", "Type": "fabric"}) msg = WSMessage("Graph", NodeAddedMsgType, node) self.sendWSMessage(msg) # get the TOR id restclient = RESTClient("localhost:8082") nodes = restclient.lookup_nodes("G.V().Has('Name', 'TOR2')") if len(nodes) != 1: print("More than one node found") sys.exit(-1) tor_id = nodes[0].id # create a ownership + layer2 link edge = Edge(tor_id + node.id + "ownership", "host-test", tor_id, node.id, metadata={"RelationType": "ownership"}) msg = WSMessage("Graph", EdgeAddedMsgType, edge) self.sendWSMessage(msg) edge = Edge(tor_id + node.id + "layer2", "", tor_id, node.id, metadata={"RelationType": "layer2", "Type": "fabric"}) msg = WSMessage("Graph", EdgeAddedMsgType, edge) self.sendWSMessage(msg) print("Success!") self.sendClose()
def onOpen(self): module = self.factory.kwargs["module"] params = self.factory.kwargs["params"] result = self.factory.kwargs["result"] node1 = self.factory.kwargs["node1"] node2 = self.factory.kwargs["node2"] if module.check_mode: self.stop() return try: host = params["host"] metadata = params["metadata"] metadata["RelationType"] = params["relation_type"] uid = uuid.uuid5( uuid.NAMESPACE_OID, "%s:%s:%s" % (node1, node2, params["relation_type"])) edge = Edge(str(uid), host, node1, node2, metadata=metadata) msg = WSMessage("Graph", EdgeAddedMsgType, edge) self.sendWSMessage(msg) result["UUID"] = str(uid) except Exception as e: module.fail_json(msg='Error during topology update %s' % e, **result) finally: self.stop()
def onOpen(self): module = self.factory.kwargs["module"] params = self.factory.kwargs["params"] result = self.factory.kwargs["result"] edges = self.factory.kwargs["edges"] if module.check_mode: self.stop() return try: host = params["host"] uuids = [] for edge in edges: metadata = edge.get("metadata", dict()) metadata["RelationType"] = edge["relation_type"] node1 = edge["node1"] node2 = edge["node2"] uid = uuid.uuid5( uuid.NAMESPACE_OID, "%s:%s:%s" % (node1, node2, edge["relation_type"])) edge = Edge(str(uid), host, node1, node2, metadata=metadata) msg = WSMessage("Graph", EdgeAddedMsgType, edge) self.sendWSMessage(msg) uuids.append(uid) result["UUID"] = str(uuids) except Exception as e: module.fail_json(msg='Error during topology update %s' % e, **result) finally: self.factory.client.loop.call_soon(self.stop)
def onOpen(self): result = self.factory.kwargs["result"] endpoints = {} for entry in result: if entry.service_host in endpoints: endpoints[entry.service_host].add(entry.service_name) else: endpoints[entry.service_host] = set([entry.service_name]) if entry.client_host in endpoints: endpoints[entry.client_host].add(entry.client_name) else: endpoints[entry.client_host] = set([entry.client_name]) for k, v in endpoints.iteritems(): """ hid = uuid.uuid5(uuid.NAMESPACE_DNS, str(k)) node = Node(str(hid), k, metadata={"Name": k, "Type": "host"}) msg = WSMessage("Graph", NodeAddedMsgType, node) self.sendWSMessage(msg) """ for name in v: nid = uuid.uuid5(uuid.NAMESPACE_DNS, str(k + name)) node = Node(str(nid), k, metadata={ "Name": name, "Type": "service" }) msg = WSMessage("Graph", NodeAddedMsgType, node) self.sendWSMessage(msg) """ edge = Edge(str(uuid.uuid1()), k, str(hid), str(nid), metadata={"RelationType": "ownership"}) msg = WSMessage("Graph", EdgeAddedMsgType, edge) self.sendWSMessage(msg) """ for entry in result: try: nid1 = uuid.uuid5(uuid.NAMESPACE_DNS, str(entry.client_host + entry.client_name)) nid2 = uuid.uuid5(uuid.NAMESPACE_DNS, str(entry.service_host + entry.service_name)) edge = Edge(str(uuid.uuid1()), k, str(nid1), str(nid2), metadata={ "RelationType": "layer2", "Type": "Connection" }) msg = WSMessage("Graph", EdgeAddedMsgType, edge) self.sendWSMessage(msg) except Exception as e: print(e)
def addEdge(self, id1, id2, metadata): id = id1 + id2 edge = Edge(hashlib.md5(id.encode()).hexdigest(), "python-client", id1, id2, metadata=metadata) msg = WSMessage("Graph", EdgeAddedMsgType, edge) self.sendWSMessage(msg)
def edge_create(self, parent_id, child_id, edge_id=None, host=None, metadata=None): now = int(time()) if not edge_id: edge_id = str(uuid4()) if not host: host = gethostname() data = json.dumps( { "ID": edge_id, "CreatedAt": now, "UpdatedAt": now, "Host": host, "Parent": parent_id, "Child": child_id, "Metadata": metadata } ) r = self.request("/api/edge", method="POST", data=data) return Edge.from_object(r)
def create_node(protocol): node = Node("TOR_TEST", "", metadata={ "Name": "Test TOR", "Type": "fabric" }) msg = WSMessage("Graph", NodeAddedMsgType, node) protocol.sendWSMessage(msg) node = Node("PORT_TEST", "", metadata={ "Name": "Test port", "Type": "fabric" }) msg = WSMessage("Graph", NodeAddedMsgType, node) protocol.sendWSMessage(msg) edge = Edge("TOR_L2LINK", "", "TOR_TEST", "PORT_TEST", metadata={"RelationType": "layer2"}) msg = WSMessage("Graph", EdgeAddedMsgType, edge) protocol.sendWSMessage(msg) time.sleep(1) restclient = RESTClient("localhost:8082") nodes = restclient.lookup_nodes("G.V().Has('Name', 'Test port')") self.assertEqual(len(nodes), 1, "should find one an only one node") tor_id = nodes[0].id self.assertEqual(tor_id, nodes[0].id, "wrong id for node") edges = restclient.lookup_edges( "G.E().Has('RelationType', 'layer2')") self.assertEqual(len(edges), 1, "should find one an only one edge")
def edge_list(self): objs = self.request("/api/edge") return [Edge.from_object(o) for o in objs.values()]
def onOpen(self): module = self.factory.kwargs["module"] params = self.factory.kwargs["params"] result = self.factory.kwargs["result"] if "node1" and "node2" in self.factory.kwargs: node1 = self.factory.kwargs["node1"] node2 = self.factory.kwargs["node2"] if module.check_mode: self.stop() return try: host = params["host"] if params["metadata"]: metadata = module._check_type_dict(params["metadata"]) else: metadata = {} if "node_type" in params: metadata["Name"] = params["name"] metadata["Type"] = params["node_type"] seed = params["seed"] if not seed: seed = "%s:%s" % (params["name"], params["node_type"]) if (module.params["state"] == "present" or module.params["state"] == "update"): uid = str(uuid.uuid5(uuid.NAMESPACE_OID, seed)) node = Node(uid, host, metadata=metadata) if module.params["state"] == "present": msg = WSMessage("Graph", NodeAddedMsgType, node) else: msg = WSMessage("Graph", NodeUpdatedMsgType, node) else: uid = params["id"] node = Node(uid, host, metadata=metadata) msg = WSMessage("Graph", NodeDeletedMsgType, node) elif "relation_type" in params: metadata["RelationType"] = params["relation_type"] if (module.params["state"] == "present" or module.params["state"] == "update"): uid = str( uuid.uuid5( uuid.NAMESPACE_OID, "%s:%s:%s" % (node1, node2, params["relation_type"]), )) edge = Edge(uid, host, node1, node2, metadata=metadata) if module.params["state"] == "present": msg = WSMessage("Graph", EdgeAddedMsgType, edge) else: msg = WSMessage("Graph", EdgeUpdatedMsgType, edge) else: uid = module.params["id"] edge = Edge(uid, host, node1, node2, metadata=metadata) msg = WSMessage("Graph", EdgeDeletedMsgType, edge) self.sendWSMessage(msg) if uid: result["UUID"] = uid result["changed"] = True except Exception as e: module.fail_json(msg="Error during topology update %s" % e, **result) finally: self.stop()
def edge_update(self, edge_id=None, patches=[]): data = json.dumps(patches) path = "/api/edge/%s" % edge_id r = self.request(path, method="PATCH", data=data) return Edge.from_object(r)