|
7 | 7 | from abc import ABC, abstractmethod
|
8 | 8 | from collections import namedtuple
|
9 | 9 | from datetime import datetime, timedelta
|
10 |
| -from typing import Any, Dict, Optional, Tuple, Union |
| 10 | +from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple, Type, Union |
| 11 | + |
| 12 | +import boto3 |
| 13 | +from botocore.config import Config |
11 | 14 |
|
12 | 15 | from .exceptions import GetParameterError, TransformParameterError
|
13 | 16 |
|
| 17 | +if TYPE_CHECKING: |
| 18 | + from mypy_boto3_appconfig import AppConfigClient |
| 19 | + from mypy_boto3_dynamodb import DynamoDBServiceResource |
| 20 | + from mypy_boto3_secretsmanager import SecretsManagerClient |
| 21 | + from mypy_boto3_ssm import SSMClient |
| 22 | + |
| 23 | + |
14 | 24 | DEFAULT_MAX_AGE_SECS = 5
|
15 | 25 | ExpirableValue = namedtuple("ExpirableValue", ["value", "ttl"])
|
16 | 26 | # These providers will be dynamically initialized on first use of the helper functions
|
17 | 27 | DEFAULT_PROVIDERS: Dict[str, Any] = {}
|
18 | 28 | TRANSFORM_METHOD_JSON = "json"
|
19 | 29 | TRANSFORM_METHOD_BINARY = "binary"
|
20 | 30 | SUPPORTED_TRANSFORM_METHODS = [TRANSFORM_METHOD_JSON, TRANSFORM_METHOD_BINARY]
|
| 31 | +ParameterClients = Union["AppConfigClient", "SecretsManagerClient", "SSMClient"] |
21 | 32 |
|
22 | 33 |
|
23 | 34 | class BaseProvider(ABC):
|
@@ -180,6 +191,72 @@ def _get_multiple(self, path: str, **sdk_options) -> Dict[str, str]:
|
180 | 191 | def clear_cache(self):
|
181 | 192 | self.store.clear()
|
182 | 193 |
|
| 194 | + @staticmethod |
| 195 | + def _build_boto3_client( |
| 196 | + service_name: str, |
| 197 | + client: Optional[ParameterClients] = None, |
| 198 | + session: Optional[Type[boto3.Session]] = None, |
| 199 | + config: Optional[Type[Config]] = None, |
| 200 | + ) -> Type[ParameterClients]: |
| 201 | + """Builds a low level boto3 client with session and config provided |
| 202 | +
|
| 203 | + Parameters |
| 204 | + ---------- |
| 205 | + service_name : str |
| 206 | + AWS service name to instantiate a boto3 client, e.g. ssm |
| 207 | + client : Optional[ParameterClients], optional |
| 208 | + boto3 client instance, by default None |
| 209 | + session : Optional[Type[boto3.Session]], optional |
| 210 | + boto3 session instance, by default None |
| 211 | + config : Optional[Type[Config]], optional |
| 212 | + botocore config instance to configure client with, by default None |
| 213 | +
|
| 214 | + Returns |
| 215 | + ------- |
| 216 | + Type[ParameterClients] |
| 217 | + Instance of a boto3 client for Parameters feature (e.g., ssm, appconfig, secretsmanager, etc.) |
| 218 | + """ |
| 219 | + if client is not None: |
| 220 | + return client |
| 221 | + |
| 222 | + session = session or boto3.Session() |
| 223 | + config = config or Config() |
| 224 | + return session.client(service_name=service_name, config=config) |
| 225 | + |
| 226 | + # maintenance: change DynamoDBServiceResource type to ParameterResourceClients when we expand |
| 227 | + @staticmethod |
| 228 | + def _build_boto3_resource_client( |
| 229 | + service_name: str, |
| 230 | + client: Optional["DynamoDBServiceResource"] = None, |
| 231 | + session: Optional[Type[boto3.Session]] = None, |
| 232 | + config: Optional[Type[Config]] = None, |
| 233 | + endpoint_url: Optional[str] = None, |
| 234 | + ) -> "DynamoDBServiceResource": |
| 235 | + """Builds a high level boto3 resource client with session, config and endpoint_url provided |
| 236 | +
|
| 237 | + Parameters |
| 238 | + ---------- |
| 239 | + service_name : str |
| 240 | + AWS service name to instantiate a boto3 client, e.g. ssm |
| 241 | + client : Optional[DynamoDBServiceResource], optional |
| 242 | + boto3 client instance, by default None |
| 243 | + session : Optional[Type[boto3.Session]], optional |
| 244 | + boto3 session instance, by default None |
| 245 | + config : Optional[Type[Config]], optional |
| 246 | + botocore config instance to configure client, by default None |
| 247 | +
|
| 248 | + Returns |
| 249 | + ------- |
| 250 | + Type[DynamoDBServiceResource] |
| 251 | + Instance of a boto3 resource client for Parameters feature (e.g., dynamodb, etc.) |
| 252 | + """ |
| 253 | + if client is not None: |
| 254 | + return client |
| 255 | + |
| 256 | + session = session or boto3.Session() |
| 257 | + config = config or Config() |
| 258 | + return session.resource(service_name=service_name, config=config, endpoint_url=endpoint_url) |
| 259 | + |
183 | 260 |
|
184 | 261 | def get_transform_method(key: str, transform: Optional[str] = None) -> Optional[str]:
|
185 | 262 | """
|
|
0 commit comments