def _find_application_name(artifact_dir: str, api_url: str, auth_token: str,
                           application_key: str):
    app_name = ""
    cached_results = False
    try:
        # Try searching the key on the cache
        applications = load_data(artifact_dir, APPLICATIONS_FILE)
        cached_results = True
    except FileNotFoundError:
        # Query the LT API, since there's no cache
        applications = get_applications(artifact_dir, api_url, auth_token,
                                        False)
    for app in applications:
        if app["Key"] == application_key:
            app_name = app["Name"]
            break
    # If the app name  was not found, determine if it needs to invalidate the cache or the application does not exist
    # since we explitly clear the cache, and the code is not multithreaded, it should not lead to recursion issues
    # If the cache was not used in the first place, it means the app does not exist
    if app_name == "" and not cached_results:
        raise AppDoesNotExistError(
            "Failed to retrieve the application. Please make sure the app exists in the environment. App Key: {}"
            .format(application_key))
    # If the cache was used, it needs to be cleared and re-fetched from the LT server
    elif app_name == "" and cached_results:
        clear_cache(artifact_dir, APPLICATIONS_FILE)
        return _find_application_name(artifact_dir, api_url, auth_token,
                                      application_key)
    return app_name
def _find_environment_url(artifact_dir: str, api_url: str, auth_token: str,
                          environment_name: str):
    env_url = ""
    cached_results = False
    try:
        # Try searching the key on the cache
        environments = load_data(artifact_dir, ENVIRONMENTS_FILE)
        cached_results = True
    except:
        # Query the LT API, since there's no cache
        environments = get_environments(artifact_dir, api_url, auth_token)
    for env in environments:
        if env["Name"] == environment_name:
            env_url = env["HostName"]
    # If the env key  was not found, determine if it needs to invalidate the cache or the application does not exist
    # since we explitly clear the cache, and the code is not multithreaded, it should not lead to recursion issues
    # If the cache was not used in the first place, it means the app does not exist
    if env_url == "" and not cached_results:
        raise EnvironmentNotFoundError(
            "Failed to retrieve the environment. Please make sure the environment exists. Environment name: {}"
            .format(environment_name))
    # If the cache was used, it needs to be cleared and re-fetched from the LT server
    elif env_url == "" and cached_results:
        clear_cache(artifact_dir, ENVIRONMENTS_FILE)
        return _find_environment_url(artifact_dir, api_url, auth_token,
                                     environment_name)
    return env_url
def main(artifact_dir: str, error_file_name: str, slack_hook: str,
         slack_channels: list, pipeline_type: str, pipeline_status: bool,
         msg_title: str, message: str):
    slack_message = message
    if error_file_name:
        try:
            file_contents = load_data(artifact_dir, error_file_name)
            slack_message += "\n\n*Details:*\n\n`{}`".format(file_contents)
        except FileNotFoundError:
            slack_message += "\nCould not found the file {} in the {} directory".format(
                error_file_name, artifact_dir)
        except:
            slack_message += "\nCould not load the file {} in the {} directory".format(
                error_file_name, artifact_dir)

    send_slack_message(slack_hook, slack_channels, pipeline_type, msg_title,
                       pipeline_status, slack_message)
        type=str,
        help="Name of the artifacts folder. Default: \"Artifacts\"",
        default=ARTIFACT_FOLDER)
    args = parser.parse_args()
    # Parse the artifact directory
    # Assumes the default dir = Artifacts
    artifact_dir = args.artifacts
    if len(
            sys.argv
    ) == 3:  # Workaround to clear the args to avoid messing with the unittest.main()
        sys.argv = sys.argv[:-2]

    # Load the test endpoints
    filename = os.path.join(BDD_FRAMEWORK_FOLDER,
                            BDD_FRAMEWORK_TEST_ENDPOINTS_FILE)
    test_urls = load_data(artifact_dir, filename)

    for test_endpoint in test_urls:
        test_func = bdd_check_generator(test_endpoint["URL"])
        test_name = "test_{}__{}".format(test_endpoint["TestSuite"],
                                         test_endpoint["Name"])
        setattr(BDDTestRunner, test_name, test_func)

    # Runs the test suite and stores the value in a XMN file to be used by JUNIT
    filename = os.path.join(ARTIFACT_FOLDER, JUNIT_TEST_RESULTS_FILE)
    try:
        with open(filename, 'wb') as output:
            runner = xmlrunner.XMLTestRunner(output=output,
                                             failfast=False,
                                             buffer=False)
            unittest.main(testRunner=runner)
 if lt_url.endswith("/"):
     lt_url = lt_url[:-1]
 # Parte LT API Version
 lt_version = args.lt_api_version
 # Parse the LT Token
 lt_token = args.lt_token
 # Parse Source Environment
 source_env = args.source_env
 # Parse Destination Environment
 dest_env = args.destination_env
 # Parse App list
 _apps = args.app_list
 apps = _apps.split(',')
 # Parse Manifest file if it exists
 if args.manifest_file:
     manifest_file = load_data("", args.manifest_file)
 else:
     manifest_file = None
 # Parse Deployment Message
 dep_note = args.deploy_msg
 # Parse OSP Tool path
 osp_tool_path = args.osp_tool_path
 # Parse Credentials for OSP Tool
 credentials = args.airgap_user + " " + args.airgap_pass
 # Parse the CICD Probe API endpoint
 cicd_api_endpoint = args.cicd_probe_endpoint
 # Parse the CICD Probe Url and split the CICD Probe hostname from the HTTP protocol
 # Assumes the default HTTP protocol = "https"
 cicd_http_proto = PROBE_HTTP_PROTO
 cicd_url = args.cicd_probe_url
 if cicd_url.startswith("http://"):