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

hub.exec.boto3.client.servicecatalog.accept_portfolio_share
hub.exec.boto3.client.servicecatalog.create_portfolio_share
hub.exec.boto3.client.servicecatalog.delete_portfolio_share
hub.exec.boto3.client.servicecatalog.describe_portfolio_shares
hub.exec.boto3.client.servicecatalog.reject_portfolio_share
hub.exec.boto3.client.servicecatalog.update_portfolio_share
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, portfolio_id: Text, accept_language: Text = None, account_id: Text = None, organization_node: Dict = None, share_tag_options: bool = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Shares the specified portfolio with the specified account or organization node. Shares to an organization node
    can only be created by the management account of an organization or by a delegated administrator. You can share
    portfolios to an organization, an organizational unit, or a specific account. Note that if a delegated admin is
    de-registered, they can no longer create portfolio shares.  AWSOrganizationsAccess must be enabled in order to
    create a portfolio share to an organization node. You can't share a shared resource, including portfolios that
    contain a shared product. If the portfolio share with the specified account or organization node already exists,
    this action will have no effect and will not return an error. To update an existing share, you must use the
    UpdatePortfolioShare API instead.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        accept_language(Text, optional): The language code.    en - English (default)    jp - Japanese    zh - Chinese. Defaults to None.
        portfolio_id(Text): The portfolio identifier.
        account_id(Text, optional): The AWS account ID. For example, 123456789012. Defaults to None.
        organization_node(Dict, optional): The organization node to whom you are going to share. If OrganizationNode is passed in,
            PortfolioShare will be created for the node an ListOrganizationPortfolioAccessd its children
            (when applies), and a PortfolioShareToken will be returned in the output in order for the
            administrator to monitor the status of the PortfolioShare creation process. Defaults to None.
        share_tag_options(bool, optional): Enables or disables TagOptions  sharing when creating the portfolio share. If this flag is not
            provided, TagOptions sharing is disabled. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.servicecatalog.portfolio_share.present:
                - name: value
                - portfolio_id: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.servicecatalog.portfolio_share.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.servicecatalog.describe_portfolio_shares(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.servicecatalog.create_portfolio_share(
                ctx,
                
                
                **{"AcceptLanguage": accept_language, "PortfolioId": portfolio_id, "AccountId": account_id, "OrganizationNode": organization_node, "ShareTagOptions": share_tag_options}
            )
            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.servicecatalog.describe_portfolio_shares(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, portfolio_id: Text, accept_language: Text = None, account_id: Text = None, organization_node: Dict = None)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Stops sharing the specified portfolio with the specified account or organization node. Shares to an organization
    node can only be deleted by the management account of an organization or by a delegated administrator. Note that
    if a delegated admin is de-registered, portfolio shares created from that account are removed.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        accept_language(Text, optional): The language code.    en - English (default)    jp - Japanese    zh - Chinese. Defaults to None.
        portfolio_id(Text): The portfolio identifier.
        account_id(Text, optional): The AWS account ID. Defaults to None.
        organization_node(Dict, optional): The organization node to whom you are going to stop sharing. Defaults to None.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.servicecatalog.portfolio_share.absent:
                - name: value
                - portfolio_id: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.servicecatalog.portfolio_share.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.servicecatalog.describe_portfolio_shares(name)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.servicecatalog.delete_portfolio_share(
                ctx,
                
                
                **{"AcceptLanguage": accept_language, "PortfolioId": portfolio_id, "AccountId": account_id, "OrganizationNode": organization_node}
            )
            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.servicecatalog.describe_portfolio_shares(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

