def handle_list_products(args): init_logger(args.verbose if 'verbose' in args else None) protocol, host, port = split_server_url(args.server_url) client = setup_product_client(protocol, host, port) products = client.getProducts(None, None) if args.output_format == 'json': results = [] for product in products: results.append({product.endpoint: product}) print(CmdLineOutputEncoder().encode(results)) else: # plaintext, csv header = ['Database status', 'Endpoint', 'Name', 'Description'] rows = [] for product in products: name = base64.b64decode(product.displayedName_b64) \ if product.displayedName_b64 else '' description = base64.b64decode(product.description_b64) \ if product.description_b64 else '' if not product.accessible: db_status_msg = 'No access.' else: db_status = product.databaseStatus db_status_msg = database_status.db_status_msg.get( db_status, 'Unknown database status') rows.append((db_status_msg, product.endpoint, name, description)) print(twodim_to_str(args.output_format, header, rows))
def testFullServerURL(self): """ Whole server URL understanding. """ def test(host, port, protocol=None): url = ''.join( [protocol + "://" if protocol else "", host, ":", str(port)]) sprotocol, shost, sport = split_server_url(url) self.assertEqual(sprotocol, expected_protocol(protocol, port)) self.assertEqual(shost, host) self.assertEqual(sport, expected_port(protocol, port)) test("localhost", 8001) test("localhost", 8002) test("1hostname.can.begin.with.digits", 9999) test("another.server", 80, "http") test("very-secure.another.server", 443, 'https') sprotocol, shost, sport = \ split_server_url('https://someserver:1234/Product') self.assertEqual(sprotocol, 'https') self.assertEqual(shost, 'someserver') self.assertEqual(sport, 1234)
def handle_login(args): init_logger(args.verbose if 'verbose' in args else None) protocol, host, port = split_server_url(args.server_url) handle_auth(protocol, host, port, args.username, login='******' not in args)
def test(host, protocol=None): url = ''.join([protocol + "://" if protocol else "", host]) sprotocol, shost, sport = split_server_url(url) self.assertEqual(sprotocol, expected_protocol(protocol, None)) self.assertEqual(shost, host) self.assertEqual(sport, expected_port(protocol, None))
def handle_list_products(args): init_logger(args.verbose if 'verbose' in args else None) protocol, host, port = split_server_url(args.server_url) client = setup_product_client(protocol, host, port) products = client.getProducts(None, None) if args.output_format == 'json': results = [] for product in products: results.append({product.endpoint: product}) print(CmdLineOutputEncoder().encode(results)) else: # plaintext, csv header = ['Database status', 'Endpoint', 'Name', 'Description'] rows = [] for product in products: name = base64.b64decode(product.displayedName_b64) \ if product.displayedName_b64 else '' description = base64.b64decode(product.description_b64) \ if product.description_b64 else '' if not product.accessible: db_status_msg = 'No access.' else: db_status = product.databaseStatus db_status_msg = database_status.db_status_msg.get( db_status, 'Unknown database status') rows.append((db_status_msg, product.endpoint, name, description)) print(twodim_to_str(args.output_format, header, rows))
def handle_login(args): init_logger(args.verbose if 'verbose' in args else None) protocol, host, port = split_server_url(args.server_url) handle_auth(protocol, host, port, args.username, login='******' not in args)
def handle_add_product(args): init_logger(args.verbose if 'verbose' in args else None) protocol, host, port = split_server_url(args.server_url) client = setup_product_client(protocol, host, port) # Put together the database connection's descriptor. if 'postgresql' in args: db_engine = 'postgresql' db_host = args.dbaddress db_port = args.dbport db_user = args.dbusername db_pass = args.dbpassword db_name = args.dbname else: db_engine = 'sqlite' db_host = "" db_port = 0 db_user = "" db_pass = "" db_name = args.sqlite dbc = DatabaseConnection( engine=db_engine, host=db_host, port=db_port, username_b64=base64.b64encode(db_user), password_b64=base64.b64encode(db_pass), database=db_name) # Put together the product configuration. name = base64.b64encode(args.display_name) \ if 'display_name' in args else None desc = base64.b64encode(args.description) \ if 'description' in args else None prod = ProductConfiguration( endpoint=args.endpoint, displayedName_b64=name, description_b64=desc, connection=dbc) LOG.debug("Sending request to add product...") success = client.addProduct(prod) if success: LOG.info("Product added successfully.") else: LOG.error("Adding the product has failed.") sys.exit(1)
def handle_add_token(args): """ Creates a new personal access token for the logged in user based on the arguments. """ init_logger(args.verbose if 'verbose' in args else None) protocol, host, port = split_server_url(args.server_url) client, _ = setup_auth_client(protocol, host, port) description = args.description if 'description' in args else None session = client.newToken(description) print("The following access token has been generated for your account: " + session.token)
def handle_add_token(args): """ Creates a new personal access token for the logged in user based on the arguments. """ init_logger(args.verbose if 'verbose' in args else None) protocol, host, port = split_server_url(args.server_url) client, _ = setup_auth_client(protocol, host, port) description = args.description if 'description' in args else None session = client.newToken(description) print("The following access token has been generated for your account: " + session.token)
def handle_add_product(args): init_logger(args.verbose if 'verbose' in args else None) protocol, host, port = split_server_url(args.server_url) client = setup_product_client(protocol, host, port) # Put together the database connection's descriptor. if 'postgresql' in args: db_engine = 'postgresql' db_host = args.dbaddress db_port = args.dbport db_user = args.dbusername db_pass = args.dbpassword db_name = args.dbname else: db_engine = 'sqlite' db_host = "" db_port = 0 db_user = "" db_pass = "" db_name = args.sqlite dbc = DatabaseConnection(engine=db_engine, host=db_host, port=db_port, username_b64=base64.b64encode(db_user), password_b64=base64.b64encode(db_pass), database=db_name) # Put together the product configuration. name = base64.b64encode(args.display_name) \ if 'display_name' in args else None desc = base64.b64encode(args.description) \ if 'description' in args else None prod = ProductConfiguration(endpoint=args.endpoint, displayedName_b64=name, description_b64=desc, connection=dbc) LOG.debug("Sending request to add product...") success = client.addProduct(prod) if success: LOG.info("Product added successfully.") else: LOG.error("Adding the product has failed.") sys.exit(1)
def testBadServerURLs(self): """ Parser throws on bad server URLs? """ with self.assertRaises(ValueError): split_server_url("in:valid:format") with self.assertRaises(ValueError): split_server_url("localhost:12PortIsANumber34") with self.assertRaises(ValueError): split_server_url("whatever://whatev.er")
def handle_del_token(args): """ Removes a personal access token of the currently logged in user. """ init_logger(args.verbose if 'verbose' in args else None) protocol, host, port = split_server_url(args.server_url) client, _ = setup_auth_client(protocol, host, port) token = args.token try: success = client.removeToken(token) if success: print("'" + token + "' has been successfully removed.") else: print("Error: '" + token + "' can not be removed.") except Exception as ex: LOG.error("Failed to remove the token!") LOG.error(ex)
def handle_del_token(args): """ Removes a personal access token of the currently logged in user. """ init_logger(args.verbose if 'verbose' in args else None) protocol, host, port = split_server_url(args.server_url) client, _ = setup_auth_client(protocol, host, port) token = args.token try: success = client.removeToken(token) if success: print("'" + token + "' has been successfully removed.") else: print("Error: '" + token + "' can not be removed.") except Exception as ex: LOG.error("Failed to remove the token!") LOG.error(ex)
def handle_list_tokens(args): """ List personal access tokens of the currently logged in user. """ init_logger(args.verbose if 'verbose' in args else None) protocol, host, port = split_server_url(args.server_url) client, curr_token = setup_auth_client(protocol, host, port) tokens = client.getTokens() if args.output_format == 'json': print(CmdLineOutputEncoder().encode(tokens)) else: # plaintext, csv header = ['Token', 'Description', 'Last access'] rows = [] for res in tokens: rows.append((res.token, res.description if res.description else '', res.lastAccess)) print(twodim_to_str(args.output_format, header, rows))
def handle_list_tokens(args): """ List personal access tokens of the currently logged in user. """ init_logger(args.verbose if 'verbose' in args else None) protocol, host, port = split_server_url(args.server_url) client, curr_token = setup_auth_client(protocol, host, port) tokens = client.getTokens() if args.output_format == 'json': print(CmdLineOutputEncoder().encode(tokens)) else: # plaintext, csv header = ['Token', 'Description', 'Last access'] rows = [] for res in tokens: rows.append((res.token, res.description if res.description else '', res.lastAccess)) print(twodim_to_str(args.output_format, header, rows))
def handle_del_product(args): init_logger(args.verbose if 'verbose' in args else None) protocol, host, port = split_server_url(args.server_url) client = setup_product_client(protocol, host, port) # Endpoints substring-match. products = client.getProducts(args.endpoint, None) products = [product for product in products if product.endpoint == args.endpoint] if len(products) == 0: LOG.error("The product '%s' does not exist!", args.endpoint) return success = client.removeProduct(products[0].id) if success: LOG.info("Product removed.") else: LOG.error("An error occurred in product removal.") sys.exit(1)
def handle_del_product(args): init_logger(args.verbose if 'verbose' in args else None) protocol, host, port = split_server_url(args.server_url) client = setup_product_client(protocol, host, port) # Endpoints substring-match. products = client.getProducts(args.endpoint, None) products = [ product for product in products if product.endpoint == args.endpoint ] if len(products) == 0: LOG.error("The product '%s' does not exist!", args.endpoint) return success = client.removeProduct(products[0].id) if success: LOG.info("Product removed.") else: LOG.error("An error occurred in product removal.") sys.exit(1)