From 86619e5ae4f29d68abfc6c20e708eedb8a9a6ca4 Mon Sep 17 00:00:00 2001 From: Leandro Damascena Date: Thu, 14 Sep 2023 15:57:30 +0100 Subject: [PATCH] refactor(parameters): BaseProvider._get to also support Dict (#3090) --- aws_lambda_powertools/utilities/parameters/base.py | 2 +- examples/parameters/src/custom_provider_vault.py | 10 +++------- .../parameters/src/working_with_own_provider_vault.py | 3 +-- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/aws_lambda_powertools/utilities/parameters/base.py b/aws_lambda_powertools/utilities/parameters/base.py index e4be9d33cd..710634636d 100644 --- a/aws_lambda_powertools/utilities/parameters/base.py +++ b/aws_lambda_powertools/utilities/parameters/base.py @@ -147,7 +147,7 @@ def get( return value @abstractmethod - def _get(self, name: str, **sdk_options) -> Union[str, bytes]: + def _get(self, name: str, **sdk_options) -> Union[str, bytes, Dict[str, Any]]: """ Retrieve parameter value from the underlying parameter store """ diff --git a/examples/parameters/src/custom_provider_vault.py b/examples/parameters/src/custom_provider_vault.py index 06d0a929ff..03bab59201 100644 --- a/examples/parameters/src/custom_provider_vault.py +++ b/examples/parameters/src/custom_provider_vault.py @@ -1,5 +1,4 @@ -import json -from typing import Dict +from typing import Any, Dict from hvac import Client @@ -8,21 +7,18 @@ class VaultProvider(BaseProvider): def __init__(self, vault_url: str, vault_token: str) -> None: - super().__init__() self.vault_client = Client(url=vault_url, verify=False, timeout=10) self.vault_client.token = vault_token - def _get(self, name: str, **sdk_options) -> str: - + def _get(self, name: str, **sdk_options) -> Dict[str, Any]: # for example proposal, the mountpoint is always /secret kv_configuration = self.vault_client.secrets.kv.v2.read_secret(path=name) - return json.dumps(kv_configuration["data"]["data"]) + return kv_configuration["data"]["data"] def _get_multiple(self, path: str, **sdk_options) -> Dict[str, str]: - list_secrets = {} all_secrets = self.vault_client.secrets.kv.v2.list_secrets(path=path) diff --git a/examples/parameters/src/working_with_own_provider_vault.py b/examples/parameters/src/working_with_own_provider_vault.py index 7be9ea6024..71ad43495d 100644 --- a/examples/parameters/src/working_with_own_provider_vault.py +++ b/examples/parameters/src/working_with_own_provider_vault.py @@ -14,10 +14,9 @@ def lambda_handler(event: dict, context: LambdaContext): - try: # Retrieve a single parameter - endpoint_comments: Any = vault_provider.get("comments_endpoint", transform="json") + endpoint_comments: Any = vault_provider.get("comments_endpoint") # you can get all parameters using get_multiple and specifying vault mount point # # for testing purposes we will not use it