# Path: shpkpr/vault.py
# def resolve_secrets(vault_client, rendered_template):
#     """Parse a rendered template, extract any secret definitions, retrieve them
#     from vault and return them to the caller.
# 
#     This is used in situations where direct Vault support is not available e.g.
#     Chronos.
#     """
#     resolved_secrets = {}
# 
#     secrets = rendered_template.get("secrets", {})
#     for name, definition in secrets.items():
#         # parse the secret source and retrieve from vault
#         path, key = definition["source"].split(":")
#         secret = vault_client.read(path)
#         if secret:
#             resolved_secrets[name] = secret["data"][key]
#         else:
#             logger.info("Couldn't locate secret in Vault: {0}".format(path))
# 
#     return resolved_secrets


import mock
from shpkpr.vault import resolve_secrets



@mock.patch("shpkpr.cli.options.hvac.Client")
def test_resolve_secrets(mock_vault_client_class):
    mock_vault_data = {
        'secret/my_project/my_path': {
            'my_key': 'some_secret_info'
        }
    }
    mock_rendered_template = {
        'secrets': {
            'MY_SECRET_USING_REL_PATH': {'source': 'my_project/my_path:my_key'},
            'MY_SECRET_USING_FULL_PATH': {'source': 'secret/my_project/my_path:my_key'},
        }
    }

    def read_vault_data(path):
        secrets = mock_vault_data.get(path, None)
        return dict(data=secrets) if secrets else None

    mock_vault_client = mock_vault_client_class.return_value
    mock_vault_client.read.side_effect = read_vault_data


# Path: shpkpr/vault.py
# def resolve_secrets(vault_client, rendered_template):
#     """Parse a rendered template, extract any secret definitions, retrieve them
#     from vault and return them to the caller.
# 
#     This is used in situations where direct Vault support is not available e.g.
#     Chronos.
#     """
#     resolved_secrets = {}
# 
#     secrets = rendered_template.get("secrets", {})
#     for name, definition in secrets.items():
#         # parse the secret source and retrieve from vault
#         path, key = definition["source"].split(":")
#         secret = vault_client.read(path)
#         if secret:
#             resolved_secrets[name] = secret["data"][key]
#         else:
#             logger.info("Couldn't locate secret in Vault: {0}".format(path))
# 
#     return resolved_secrets


# Path: tests/test_resolve_secrets.py
import mock
from shpkpr.vault import resolve_secrets




@mock.patch("shpkpr.cli.options.hvac.Client")
def test_resolve_secrets(mock_vault_client_class):
    mock_vault_data = {
        'secret/my_project/my_path': {
            'my_key': 'some_secret_info'
        }
    }
    mock_rendered_template = {
        'secrets': {
            'MY_SECRET_USING_REL_PATH': {'source': 'my_project/my_path:my_key'},
            'MY_SECRET_USING_FULL_PATH': {'source': 'secret/my_project/my_path:my_key'},
        }
    }

    def read_vault_data(path):
        secrets = mock_vault_data.get(path, None)
        return dict(data=secrets) if secrets else None

    mock_vault_client = mock_vault_client_class.return_value
    mock_vault_client.read.side_effect = read_vault_data


    result = resolve_secrets(mock_vault_client, mock_rendered_template)