Пример #1
0
 def test_null_to_empty_null(self):
     """Ensure None returns with empty string."""
     result = null_to_empty(None)
     self.assertEqual(result, "")
Пример #2
0
 def test_null_to_empty_val(self):
     """Ensure if not None input is returned."""
     result = null_to_empty("test")
     self.assertEqual(result, "test")
Пример #3
0
def run_compliance(  # pylint: disable=too-many-arguments,too-many-locals
    task: Task,
    logger,
    global_settings,
    backup_root_path,
    intended_root_folder,
    features,
) -> Result:
    """Prepare data for compliance task.

    Args:
        task (Task): Nornir task individual object

    Returns:
        result (Result): Result from Nornir task
    """
    obj = task.host.data["obj"]

    compliance_obj = GoldenConfiguration.objects.filter(device=obj).first()
    if not compliance_obj:
        compliance_obj = GoldenConfiguration.objects.create(device=obj)
    compliance_obj.compliance_last_attempt_date = task.host.defaults.data[
        "now"]
    compliance_obj.save()

    intended_path_template_obj = check_jinja_template(
        obj, logger, global_settings.intended_path_template)

    intended_file = os.path.join(intended_root_folder,
                                 intended_path_template_obj)

    backup_template = check_jinja_template(
        obj, logger, global_settings.backup_path_template)
    backup_file = os.path.join(backup_root_path, backup_template)

    platform = obj.platform.slug
    if not features.get(platform):
        logger.log_failure(
            obj,
            f"There is no `user` defined feature mapping for platform slug {platform}."
        )
        raise NornirNautobotException()

    if platform not in parser_map.keys():
        logger.log_failure(
            obj,
            f"There is currently no parser support for platform slug {platform}."
        )
        raise NornirNautobotException()

    feature_data = task.run(
        task=dispatcher,
        name="GET COMPLIANCE FOR CONFIG",
        method="compliance_config",
        obj=obj,
        logger=logger,
        backup_file=backup_file,
        intended_file=intended_file,
        features=features[platform],
        platform=platform,
    )[1].result["feature_data"]

    for feature, value in feature_data.items():
        defaults = {
            "actual": null_to_empty(value["actual"]),
            "intended": null_to_empty(value["intended"]),
            "missing": null_to_empty(value["missing"]),
            "extra": null_to_empty(value["extra"]),
            "compliance": value["compliant"],
            "ordered": value["ordered_compliant"],
        }
        # using update_or_create() method to conveniently update actual obj or create new one.
        ConfigCompliance.objects.update_or_create(
            device=obj,
            feature=feature,
            defaults=defaults,
        )

    compliance_obj.compliance_last_success_date = task.host.defaults.data[
        "now"]
    compliance_obj.compliance_config = "\n".join(
        diff_files(backup_file, intended_file))
    compliance_obj.save()
    logger.log_success(obj, "Successfully tested complinace.")

    return Result(host=task.host, result=feature_data)