|
2 | 2 | AWS SSM Parameter retrieval and caching utility
|
3 | 3 | """
|
4 | 4 |
|
5 |
| - |
6 |
| -from typing import TYPE_CHECKING, Any, Dict, Optional, Union |
| 5 | +from typing import TYPE_CHECKING, Any, Dict, Optional, Union, overload |
7 | 6 |
|
8 | 7 | import boto3
|
9 | 8 | from botocore.config import Config
|
| 9 | +from typing_extensions import Literal |
10 | 10 |
|
11 | 11 | from .base import DEFAULT_MAX_AGE_SECS, DEFAULT_PROVIDERS, BaseProvider
|
| 12 | +from .types import TransformOptions |
12 | 13 |
|
13 | 14 | if TYPE_CHECKING:
|
14 | 15 | from mypy_boto3_ssm import SSMClient
|
@@ -207,7 +208,7 @@ def get_parameter(
|
207 | 208 | force_fetch: bool = False,
|
208 | 209 | max_age: int = DEFAULT_MAX_AGE_SECS,
|
209 | 210 | **sdk_options
|
210 |
| -) -> Union[str, list, dict, bytes]: |
| 211 | +) -> Union[str, dict, bytes]: |
211 | 212 | """
|
212 | 213 | Retrieve a parameter value from AWS Systems Manager (SSM) Parameter Store
|
213 | 214 |
|
@@ -344,3 +345,107 @@ def get_parameters(
|
344 | 345 | force_fetch=force_fetch,
|
345 | 346 | **sdk_options
|
346 | 347 | )
|
| 348 | + |
| 349 | + |
| 350 | +@overload |
| 351 | +def get_parameters_by_name( |
| 352 | + parameters: Dict[str, Dict], |
| 353 | + transform: None = None, |
| 354 | + decrypt: bool = False, |
| 355 | + force_fetch: bool = False, |
| 356 | + max_age: int = DEFAULT_MAX_AGE_SECS, |
| 357 | +) -> Dict[str, str]: |
| 358 | + ... |
| 359 | + |
| 360 | + |
| 361 | +@overload |
| 362 | +def get_parameters_by_name( |
| 363 | + parameters: Dict[str, Dict], |
| 364 | + transform: Literal["binary"], |
| 365 | + decrypt: bool = False, |
| 366 | + force_fetch: bool = False, |
| 367 | + max_age: int = DEFAULT_MAX_AGE_SECS, |
| 368 | +) -> Dict[str, bytes]: |
| 369 | + ... |
| 370 | + |
| 371 | + |
| 372 | +@overload |
| 373 | +def get_parameters_by_name( |
| 374 | + parameters: Dict[str, Dict], |
| 375 | + transform: Literal["json"], |
| 376 | + decrypt: bool = False, |
| 377 | + force_fetch: bool = False, |
| 378 | + max_age: int = DEFAULT_MAX_AGE_SECS, |
| 379 | +) -> Dict[str, Dict[str, Any]]: |
| 380 | + ... |
| 381 | + |
| 382 | + |
| 383 | +@overload |
| 384 | +def get_parameters_by_name( |
| 385 | + parameters: Dict[str, Dict], |
| 386 | + transform: Literal["auto"], |
| 387 | + decrypt: bool = False, |
| 388 | + force_fetch: bool = False, |
| 389 | + max_age: int = DEFAULT_MAX_AGE_SECS, |
| 390 | +) -> Union[Dict[str, str], Dict[str, dict]]: |
| 391 | + ... |
| 392 | + |
| 393 | + |
| 394 | +def get_parameters_by_name( |
| 395 | + parameters: Dict[str, Any], |
| 396 | + transform: TransformOptions = None, |
| 397 | + decrypt: bool = False, |
| 398 | + force_fetch: bool = False, |
| 399 | + max_age: int = DEFAULT_MAX_AGE_SECS, |
| 400 | +) -> Union[Dict[str, str], Dict[str, bytes], Dict[str, dict]]: |
| 401 | + """ |
| 402 | + Retrieve multiple parameter values by name from AWS Systems Manager (SSM) Parameter Store |
| 403 | +
|
| 404 | + Parameters |
| 405 | + ---------- |
| 406 | + parameters: List[Dict[str, Dict]] |
| 407 | + List of parameter names, and any optional overrides |
| 408 | + transform: str, optional |
| 409 | + Transforms the content from a JSON object ('json') or base64 binary string ('binary') |
| 410 | + decrypt: bool, optional |
| 411 | + If the parameter values should be decrypted |
| 412 | + force_fetch: bool, optional |
| 413 | + Force update even before a cached item has expired, defaults to False |
| 414 | + max_age: int |
| 415 | + Maximum age of the cached value |
| 416 | + sdk_options: dict, optional |
| 417 | + Dictionary of options that will be passed to the Parameter Store get_parameter API call |
| 418 | +
|
| 419 | + Raises |
| 420 | + ------ |
| 421 | + GetParameterError |
| 422 | + When the parameter provider fails to retrieve a parameter value for |
| 423 | + a given name. |
| 424 | + TransformParameterError |
| 425 | + When the parameter provider fails to transform a parameter value. |
| 426 | + """ |
| 427 | + |
| 428 | + # NOTE: Need a param for hard failure mode on parameter retrieval |
| 429 | + # by default, we should return an empty string on failure (ask customer for desired behaviour) |
| 430 | + |
| 431 | + # NOTE: Check costs of threads to assess when it's worth the overhead. |
| 432 | + # for threads, assess failure mode to absorb OR raise/cancel futures |
| 433 | + |
| 434 | + ret: Dict[str, Any] = {} |
| 435 | + |
| 436 | + for parameter, options in parameters.items(): |
| 437 | + if isinstance(options, dict): |
| 438 | + transform = options.get("transform") or transform |
| 439 | + decrypt = options.get("decrypt") or decrypt |
| 440 | + max_age = options.get("max_age") or max_age |
| 441 | + force_fetch = options.get("force_fetch") or force_fetch |
| 442 | + |
| 443 | + ret[parameter] = get_parameter( |
| 444 | + name=parameter, |
| 445 | + transform=transform, |
| 446 | + decrypt=decrypt, |
| 447 | + max_age=max_age, |
| 448 | + force_fetch=force_fetch, |
| 449 | + ) |
| 450 | + |
| 451 | + return ret |
0 commit comments