"""
Autogenerated state module using `pop-create-idem <https://gitlab.com/saltstack/pop/pop-create-idem>`__

hub.exec.boto3.client.xray.create_sampling_rule
hub.exec.boto3.client.xray.delete_sampling_rule
hub.exec.boto3.client.xray.get_sampling_rules
hub.exec.boto3.client.xray.update_sampling_rule
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, sampling_rule: Dict, tags: List = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates a rule to control sampling behavior for instrumented applications. Services retrieve rules with
    GetSamplingRules, and evaluate each rule in ascending order of priority for each request. If a rule matches, the
    service records a trace, borrowing it from the reservoir size. After 10 seconds, the service reports back to
    X-Ray with GetSamplingTargets to get updated versions of each in-use rule. The updated rule contains a trace
    quota that the service can use instead of borrowing from the reservoir.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        sampling_rule(Dict): The rule definition.
        tags(List, optional): A map that contains one or more tag keys and tag values to attach to an X-Ray sampling rule. For
            more information about ways to use tags, see Tagging AWS resources in the AWS General Reference.
            The following restrictions apply to tags:   Maximum number of user-applied tags per resource: 50
            Maximum tag key length: 128 Unicode characters   Maximum tag value length: 256 Unicode
            characters   Valid values for key and value: a-z, A-Z, 0-9, space, and the following characters:
            _ . : / = + - and @   Tag keys and values are case sensitive.   Don't use aws: as a prefix for
            keys; it's reserved for AWS use. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.xray.sampling_rule.present:
                - name: value
                - sampling_rule: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.xray.sampling_rule.id(
        ctx,
        jmes_path=name
    )
    if ret["status"]:
        # name is now the first id that matched the JMES search path
        name = ret["ret"]
    

    
    before = await hub.exec.boto3.client.xray.get_sampling_rules(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.xray.create_sampling_rule(
                ctx,
                
                
                **{"SamplingRule": sampling_rule, "Tags": tags}
            )
            result["result"] = ret["status"]
            if not result["result"]:
                result["comment"] = ret["comment"]
                return result
            ret["comment"] = f"Created '{name}'"
        except hub.tool.boto3.exception.ClientError as e:
            result["comment"] = f"{e.__class__.__name__}: {e}"

    
    # TODO perform other modifications as needed here
    ...

    after = await hub.exec.boto3.client.xray.get_sampling_rules(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, rule_name: Text = None, rule_arn: Text = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes a sampling rule.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        rule_name(Text, optional): The name of the sampling rule. Specify a rule by either name or ARN, but not both. Defaults to None.
        rule_arn(Text, optional): The ARN of the sampling rule. Specify a rule by either name or ARN, but not both. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.xray.sampling_rule.absent:
                - name: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.xray.sampling_rule.id(
        ctx,
        jmes_path=name
    )
    if ret["status"]:
        # name is now the first id that matched the JMES search path
        name = ret["ret"]
    

    

    before = await hub.exec.boto3.client.xray.get_sampling_rules(name)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.xray.delete_sampling_rule(
                ctx,
                
                
                **{"RuleName": rule_name, "RuleARN": rule_arn}
            )
            result["result"] = ret["status"]
            if not result["result"]:
                result["comment"] = ret["comment"]
                return result
            result["comment"] = f"Deleted '{name}'"
        except hub.tool.boto3.exception.ClientError as e:
            result["comment"] = f"{e.__class__.__name__}: {e}"

    

    after = await hub.exec.boto3.client.xray.get_sampling_rules(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

