def _override_client(ctx) -> None: """Replace the vcd client in ctx.obj with new one. New vcd client takes the CSE server_api_version as api_version param. Save profile also with 'cse_server_api_version' for subsequent commands. :param <click.core.Context> ctx: click context """ profiles = Profiles.load() # if the key CSE_SERVER_RUNNING is not present in the profiles.yaml, # we make an assumption that CSE server is running is_cse_server_running = profiles.get(CSE_SERVER_RUNNING, default=True) cse_server_api_version = profiles.get(CSE_SERVER_API_VERSION) if not is_cse_server_running: restrict_cli_to_tkg_operations() ctx.obj['profiles'] = profiles return # Get server_api_version; save it in profiles if doesn't exist if not cse_server_api_version: try: system = syst.System(ctx.obj['client']) sys_info = system.get_info() cse_server_api_version = sys_info.get(CSE_SERVER_API_VERSION) profiles.set(CSE_SERVER_API_VERSION, cse_server_api_version) profiles.set(CSE_SERVER_RUNNING, True) profiles.save() except CseResponseError: # If request to CSE server times out profiles.set(CSE_SERVER_RUNNING, False) # restrict CLI for only TKG operations restrict_cli_to_tkg_operations() ctx.obj['profiles'] = profiles profiles.save() return client = Client(profiles.get('host'), api_version=cse_server_api_version, verify_ssl_certs=profiles.get('verify'), log_file='vcd.log', log_requests=profiles.get('log_request'), log_headers=profiles.get('log_header'), log_bodies=profiles.get('log_body')) client.rehydrate_from_token(profiles.get('token'), profiles.get('is_jwt_token')) # noqa: E501 ctx.obj['client'] = client ctx.obj['profiles'] = profiles
def _override_client(ctx) -> None: """Replace the vcd client in ctx.obj with new one. New vcd client takes the CSE server_api_version as api_version param. Save profile also with 'cse_server_api_version' for subsequent commands. :param <click.core.Context> ctx: click context """ profiles = Profiles.load() # if the key CSE_SERVER_RUNNING is not present in the profiles.yaml, # we make an assumption that CSE server is running is_cse_server_running = profiles.get(CSE_SERVER_RUNNING, default=True) cse_server_api_version = profiles.get(CSE_SERVER_API_VERSION) if not is_cse_server_running: CLIENT_LOGGER.debug( "CSE server not running as per profile, restricting CLI to only TKG operations." ) # noqa: E501 restrict_cli_to_tkg_s_operations() ctx.obj['profiles'] = profiles return # Get server_api_version; save it in profiles if doesn't exist if not cse_server_api_version: try: system = syst.System(ctx.obj['client']) sys_info = system.get_info() is_pre_cse_3_1_server = False if CSE_SERVER_LEGACY_MODE not in sys_info or \ CSE_SERVER_SUPPORTED_API_VERSIONS not in sys_info: is_pre_cse_3_1_server = True if not is_pre_cse_3_1_server: is_cse_server_running_in_legacy_mode = \ sys_info.get(CSE_SERVER_LEGACY_MODE) cse_server_supported_api_versions = \ set(sys_info.get(CSE_SERVER_SUPPORTED_API_VERSIONS)) cse_client_supported_api_versions = \ set(shared_constants.SUPPORTED_VCD_API_VERSIONS) common_supported_api_versions = \ list(cse_server_supported_api_versions.intersection( cse_client_supported_api_versions)) # ToDo: Instead of float use proper version comparison if is_cse_server_running_in_legacy_mode: common_supported_api_versions = \ [float(x) for x in common_supported_api_versions if float(x) < 35.0] else: common_supported_api_versions = \ [float(x) for x in common_supported_api_versions if float(x) >= 35.0] cse_server_api_version = \ str(max(common_supported_api_versions)) CLIENT_LOGGER.debug( f"Server api versions : {cse_server_supported_api_versions}, " # noqa: E501 f"Client api versions : {cse_client_supported_api_versions}, " # noqa: E501 f"Server in Legacy mode : {is_cse_server_running_in_legacy_mode}, " # noqa: E501 f"Selected api version : {cse_server_api_version}.") else: cse_server_api_version = \ sys_info.get(CSE_SERVER_API_VERSION) CLIENT_LOGGER.debug( "Pre CSE 3.1 server detected. Selected api version : " f"{cse_server_api_version}.") profiles.set(CSE_SERVER_API_VERSION, cse_server_api_version) profiles.set(CSE_SERVER_RUNNING, True) profiles.save() except (requests.exceptions.Timeout, CseResponseError) as err: CLIENT_LOGGER.error(err, exc_info=True) CLIENT_LOGGER.debug( "Request to CSE server timed out. Restricting CLI to only TKG operations." ) # noqa: E501 profiles.set(CSE_SERVER_RUNNING, False) restrict_cli_to_tkg_s_operations() ctx.obj['profiles'] = profiles profiles.save() return client = Client(profiles.get('host'), api_version=cse_server_api_version, verify_ssl_certs=profiles.get('verify'), log_file='vcd.log', log_requests=profiles.get('log_request'), log_headers=profiles.get('log_header'), log_bodies=profiles.get('log_body')) client.rehydrate_from_token(profiles.get('token'), profiles.get('is_jwt_token')) # noqa: E501 ctx.obj['client'] = client ctx.obj['profiles'] = profiles