# Copyright 2022-2025 Ping Identity Corporation. All Rights Reserved
#
# This code is to be used exclusively in connection with Ping Identity
# Corporation software or services. Ping Identity Corporation only offers
# such software or services to legal entities who have entered into a
# binding license agreement with Ping Identity Corporation.
# -*- coding: utf-8 -*-

# Copy template over to the server

# Python imports
import os

# Framework imports
from pyrock.lib.PyRockRun import get_pyrock_run
from pyrock.lib.scheduler.tasks.StepTask import StepTask
from pyrock.tasks.scenario.ds_sdk import DSAddRateTask
from pyrock.lib.scheduler.tasks.ScaleTask import ScaleTask


class CopyFile(StepTask):
    pod = None
    template_name = "token.template"
    destination = f"/results/{template_name}"
    template_path = ""

    def pre(self):
        self.template_path = os.path.join(get_pyrock_run().get_test_path(), "config", self.template_name)

    def step1(self):
        """Send template to overseer pod"""
        self.source.pods[0].copy_to_pod(source=self.template_path, target=self.destination)


class ConfigureInstance(StepTask):
    pod = None

    def pre(self):
        self.pod = self.source.pods[0]
        self.purge_delay = int(get_pyrock_run().global_duration_in_seconds / 3)

    def step1(self):
        """Configure purge-delay"""
        cmd = 'set-synchronization-provider-prop --provider-name "Multimaster Synchronization"'
        cmd += f' --set "replication-purge-delay:{self.purge_delay}s" '

        self.pod.component.dsconfig(pod=self.pod, command=cmd)

    def step2(self):
        """Add users"""
        entry = [
            f"dn: ou=user,{self.pod.component.base_dn}",
            "objectClass: organizationalUnit",
            "objectClass: top",
            "ou: user",
            "",
            f"dn: cn=admin,ou=user,{self.pod.component.base_dn}",
            "objectClass: person",
            "objectClass: inetorgperson",
            "objectClass: organizationalperson",
            "objectClass: top",
            "uid: admin",
            "userPassword: str0ngPa55w#ord",
            "givenName: admin",
            "cn: admin",
            "sn: admin",
            "ds-privilege-name: proxied-auth",
        ]

        self.pod.component.ldapmodify(pod=self.pod, entry=entry)
