def test_create_from_config_file_factory_method(self): file_data = dedent("""[default] api_host = 127.0.0.1 api_port = 8080 use_ssl = True verify_ssl = False warn_ssl = True username = admin password = admin123""") tmp = tempfile.NamedTemporaryFile(delete=True) try: tmp.write(file_data.encode('utf-8')) tmp.flush() client = ContainerPlatformClient.create_from_config_file( config_file=tmp.name) self.assertEqual(client.username, 'admin') self.assertEqual(client.password, 'admin123') self.assertEqual(client.api_host, '127.0.0.1') self.assertEqual(client.api_port, 8080) self.assertEqual(client.use_ssl, True) self.assertEqual(client.verify_ssl, False) self.assertEqual(client.warn_ssl, True) finally: tmp.close()
def get_client(start_session=True): """Retrieve a reference to an authenticated client object.""" client = ContainerPlatformClient.create_from_config_file( config_file=get_config_file(), profile=get_profile(), ) if start_session: client.create_session() return client
def get_client(): """Utility function to retrieve an authenticated client object""" try: client = ContainerPlatformClient.create_from_config_file( config_file=HPECP_CONFIG_FILE, profile=PROFILE) client.create_session() return client except APIException as e: print(e.message) sys.exit(1)
def configure_cli(): controller_api_host = None controller_api_port = None controller_use_ssl = None controller_verify_ssl = None controller_warn_ssl = None controller_username = None controller_password = None config_path = os.path.join(os.path.expanduser("~"), ".hpecp.conf",) if os.path.exists(config_path): config_reader = ContainerPlatformClient.create_from_config_file() controller_api_host = config_reader.api_host controller_api_port = config_reader.api_port controller_use_ssl = config_reader.use_ssl controller_verify_ssl = config_reader.verify_ssl controller_warn_ssl = config_reader.warn_ssl controller_username = config_reader.username controller_password = config_reader.password sys.stdout.write("Controller API Host [{}]: ".format(controller_api_host)) tmp = input() if tmp != "": controller_api_host = tmp sys.stdout.write("Controller API Port [{}]: ".format(controller_api_port)) tmp = input() if tmp != "": controller_api_port = tmp sys.stdout.write( "Controller uses ssl (True|False) [{}]: ".format(controller_use_ssl) ) tmp = input() if tmp != "": controller_use_ssl = tmp sys.stdout.write( "Controller verify ssl (True|False) [{}]: ".format( controller_verify_ssl ) ) tmp = input() if tmp != "": controller_verify_ssl = tmp sys.stdout.write( "Controller warn ssl (True|False) [{}]: ".format(controller_warn_ssl) ) tmp = input() if tmp != "": controller_warn_ssl = tmp sys.stdout.write("Controller Username [{}]: ".format(controller_username)) tmp = input() if tmp != "": controller_username = tmp sys.stdout.write("Controller Password [{}]: ".format(controller_password)) tmp = input() if tmp != "": controller_password = tmp config = configparser.ConfigParser() config["default"] = OrderedDict() config["default"]["api_host"] = controller_api_host config["default"]["api_port"] = str(controller_api_port) config["default"]["use_ssl"] = str(controller_use_ssl) config["default"]["verify_ssl"] = str(controller_verify_ssl) config["default"]["warn_ssl"] = str(controller_warn_ssl) config["default"]["username"] = controller_username config["default"]["password"] = controller_password with open(config_path, "w",) as config_file: config.write(config_file)