def __init__(self, module, **kwargs): super(skydive_wsclient, self).__init__(**kwargs) class skydive_full_inject_protocol(skydive_inject_protocol, WSClientDefaultProtocol): pass kwargs["scheme"] = "ws" if "ssl" in kwargs: if kwargs["ssl"]: kwargs["scheme"] = "wss" if "insecure" not in kwargs: kwargs["insecure"] = False scheme = kwargs["scheme"] self.result = dict(changed=False) if "node_type" in module.params: self.wsclient_object = WSClient( "ansible-" + str(os.getpid()) + "-" + module.params["host"], "%s://%s/ws/publisher" % (scheme, kwargs["endpoint"]), protocol=type( "skydive_full_inject_protocol", (skydive_inject_protocol, WSClientDefaultProtocol), dict(), ), persistent=True, insecure=kwargs["insecure"], username=kwargs["username"], password=kwargs["password"], module=module, params=module.params, result=self.result, ) elif "relation_type" in module.params: self.parent_node = self.get_node_id(module.params["parent_node"]) self.child_node = self.get_node_id(module.params["child_node"]) self.wsclient_object = WSClient( "ansible-" + str(os.getpid()) + "-" + module.params["host"], "%s://%s/ws/publisher" % (scheme, kwargs["endpoint"]), protocol=type( "skydive_full_inject_protocol", (skydive_inject_protocol, WSClientDefaultProtocol), dict(), ), persistent=True, insecure=kwargs["insecure"], username=kwargs["username"], password=kwargs["password"], module=module, params=module.params, node1=self.parent_node, node2=self.child_node, result=self.result, )
def test_connection(self): self.connected = False def is_connected(protocol): self.connected = True self.wsclient = WSClient("host-test", self.schemeWS + "://localhost:8082/ws/publisher", protocol=WSTestClient, test=is_connected, username=self.username, password=self.password, insecure=True) self.wsclient.connect() if self.auth: ret = self.wsclient.login("localhost:8082", "toto") self.assertEqual(ret, False, "login() should failed") ret = self.wsclient.login("localhost:8082", "admin", "pass") self.assertEqual(ret, True, "login() failed") ret = self.wsclient.login() self.assertEqual(ret, True, "login() failed") self.wsclient.start() self.assertEqual(self.connected, True, "failed to connect")
def main(): logging.basicConfig(level=logging.INFO) wsclient = WSClient("host-test", "ws://localhost:8082/ws/publisher", protocol=WSClientInjectProtocol, persistent=True) wsclient.connect() wsclient.start()
def test_injection(self): 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) self.wsclient = WSClient("host-test2", self.schemeWS + "://localhost:8082/ws/publisher", protocol=WSTestClient, test=create_node, username=self.username, password=self.password, insecure=True) self.wsclient.connect() self.wsclient.start() time.sleep(1) restclient = RESTClient("localhost:8082", scheme=self.schemeHTTP, username=self.username, password=self.password, insecure=True) 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 new_ws_client(self, id, endpoint, test): return WSClient(id, self.schemeWS + "://localhost:8082/ws/" + endpoint, protocol=WSTestClient, test=test, username=self.username, password=self.password, insecure=self.insecure, cafile=self.cafile, certfile=self.certfile, keyfile=self.keyfile)
def skydive_output(matrix): result = matrix.get_matrix() if result is None: print("No result, please check analyzer address") return client = WSClient('localhost', 'ws://localhost:8082/ws/publisher', protocol=WSMatrixProtocol, result=result) client.connect() client.start()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--folder', required=True) args = parser.parse_args() wsclient = WSClient("host-test", "ws://localhost:8082/ws/publisher", protocol=WSClientInjectProtocol, persistent=True, folder=args.folder) wsclient.connect() wsclient.start()
def run_module(): module_args = dict( analyzer=dict(type='str', default="127.0.0.1:8082"), ssl=dict(type='bool', default=False), insecure=dict(type='bool', default=False), username=dict(type='str', default=""), password=dict(type='str', default="", no_log=True), edges=dict(type='list', required=True), host=dict(type='str', default=""), metadata=dict(type='dict', default=dict()) ) result = dict( changed=False ) module = AnsibleModule( argument_spec=module_args, supports_check_mode=True ) try: edges = module.params["edges"] except Exception as e: module.fail_json( msg='Error during topology request %s' % e, **result) scheme = "ws" if module.params["ssl"]: scheme = "wss" try: url = "%s://%s/ws/publisher" % (scheme, module.params["analyzer"]) wsclient = WSClient("ansible-" + str(os.getpid()) + "-" + module.params["host"], url, protocol=EdgeInjectProtocol, persistent=True, insecure=module.params["insecure"], username=module.params["username"], password=module.params["password"], module=module, params=module.params, edges=edges, result=result) wsclient.connect() wsclient.start() except Exception as e: module.fail_json(msg='Connection error %s' % str(e), **result) result['changed'] = True module.exit_json(**result)
def test_connection(self): self.connected = False def is_connected(protocol): self.connected = True self.wsclient = WSClient("host-test", "ws://localhost:8082/ws/publisher", protocol=WSTestClient, test=is_connected) self.wsclient.connect() self.assertEqual(self.connected, True, "failed to connect")
def main(): logging.basicConfig(level=logging.DEBUG) parser = argparse.ArgumentParser() parser.add_argument('--analyzer', type=str, default="127.0.0.1:8082", dest='analyzer', help='address of the Skydive analyzer') parser.add_argument('--host', type=str, default="Test", dest='host', help='client identifier') parser.add_argument('--username', type=str, default="", dest='username', help='client username') parser.add_argument('--password', type=str, default="", dest='password', help='client password') subparsers = parser.add_subparsers(help='sub-command help', dest='mode') parser_add = subparsers.add_parser( 'add', help='add edges and nodes in the given json files') parser_add.add_argument('file', type=str, help='topology to add') parser_delete = subparsers.add_parser( 'delete', help='delete edges and nodes in the given json files') parser_delete.add_argument('file', type=str, help='topology to delete') parser_update = subparsers.add_parser( 'update', help='update edges and nodes in the given json files') parser_update.add_argument('file', type=str, help='topology to update') parser_listen = subparsers.add_parser('listen', help='listen help') parser_listen.add_argument('--gremlin', type=str, default="", required=False, help='gremlin filter') parser_listen.add_argument('--sync-request', default=False, required=False, action='store_true', help='send a request message') args = parser.parse_args() if not args.mode: parser.print_help() sys.exit(0) if args.mode == "listen": protocol = WSClientDebugProtocol endpoint = "/ws/subscriber" file = "" gremlin_filter = args.gremlin sync_request = args.sync_request else: protocol = WSClientModifyProtocol endpoint = "/ws/publisher" file = args.file gremlin_filter = "" sync_request = False if not os.path.isfile(args.file): raise ValueError("The file %s does not exist" % args.file) client = WSClient(args.host, "ws://" + args.analyzer + endpoint, username=args.username, password=args.password, protocol=protocol, filter=gremlin_filter, sync=sync_request, mode=args.mode, file=file) client.connect() client.start()
def main(): wsclient = WSClient("host-test", "ws://localhost:8082/ws/publisher", protocol=WSClientInjectProtocol) wsclient.connect()