Name | aws-cdk-config JSON |
Version |
0.1.1
JSON |
| download |
home_page | |
Summary | |
upload_time | 2023-11-22 08:12:27 |
maintainer | |
docs_url | None |
author | Mathew Moon |
requires_python | >=3.11,<4.0 |
license | |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# aws-cdk-config
### Provides typed input parsing for AWS CDK (or really anything else that needs it) allowing for Python built-ins as well as casting to any other class types as well as define validation
CDK provides a method for passing arguments via Parameters, but this mechanism has certain limitations:
* Typing is limited to the same types as CloudFormation and does not support python types that you may want to use in code
* CDK Parameters are not concrete values, but rather a later resolvable value, and cannot be treated as such
* Parameters can't be configured in a flat file, but have to be passed as commandline arguments. Often developers use context instead, but this removes things such as typing
Benefits of using CdkConfig:
* Support for using yaml/json files as a config or passing a config as a `dict` from any source in code
* Input values are concrete values
* Inputs can be typed as anything that can accept one a yaml or json value (unpacked if a sequence or object) as it's initializer's arguments
* Inputs can have a callable, including lambda, as a validator
* Guaranteed immutability of inputs after parsing
### Example:
Config file inputs.yaml
```yaml
development:
GroupName: Foo
GroupMembers:
- bar
- baz
```
CDK code:
```python
#!/user/bin/env python3
from typing import List
from aws_cdk_config import CdkConfig
from aws_cdk import (
Stack,
aws_iam as iam,
)
from boto3 import client
from constructs import Construct
def group_exists(name: str) -> bool:
"""
Provides validation to ensure the iam group doesn't already exist
so we can fail fast if it does.
"""
iam_client = client("iam")
try:
iam_client.get_group(GroupName=name)
except iam_client.exceptions.NoSuchEntityException:
return True
return False
config = CdkConfig(config_file=inputs.yaml, namespace="development")
config.add_input(
name="GroupName",
type=str,
description="The name of the group to create",
validator=group_exists
)
config.add_input(
name="GroupUsers",
type=List[str],
description="A list of users to add to the group",
validator=lambda x: x != "root" # Use a lambda as the callable just to keep it simple
)
config.parse()
class InputDemo(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
group = iam.Group(self, config.GroupName.value)
for username in config.GroupUsers.value:
user = iam.User.from_user_name(self, username, username)
group.add_user(user)
```
## Typing
Inputs can be any python type, not just primitives. This means that any types that aren't both YAML, JSON, and python builtins (`str`, `list`, `int`, etc) will be cast by passing the config values to the type. The classes accept arguments in the following ways:
* If the type isn't a python builtin sequence, and yaml/json value is an array, the class must accept the value being passed "unpacked" as arguments, eg: `Foo(*myinput)`
* If the type isn't a python hashable and the yaml/json value is an object, the calss must accept the value being "unpacked" being passed as arguments, eg: `Bar(**myinput)`
Example:
```python
from aws_cdk_config import CdkConfig
class Foo:
def __init__(self, arg1, arg2): # Could also be signed as *args
pass
config = CdkConfig()
config.add_argument(
name="test",
type=Foo,
value=["arg_value_1", "arg_value_2"]
)
config.parse()
# Returns True
isinstance(config.test, Foo)
```
```python
from aws_cdk_config import CdkConfig
class Bar:
def __init__(self, *, arg1, arg2): # Could also be signed as **kwargs
pass
config = CdkConfig()
config.add_argument(
name="test",
type=Bar,
value={
"arg1": "arg_value_1",
"arg2": "arg_value_2"
}
)
config.parse()
# Returns True
isinstance(config.test, Bar)
```
See the examples directory for more examples.
Raw data
{
"_id": null,
"home_page": "",
"name": "aws-cdk-config",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.11,<4.0",
"maintainer_email": "",
"keywords": "",
"author": "Mathew Moon",
"author_email": "me@mathewmoon.net",
"download_url": "https://files.pythonhosted.org/packages/a0/2b/d274ec079986d95752acd4170e3558fe4ae19ea570f442bde32b9673fb2f/aws_cdk_config-0.1.1.tar.gz",
"platform": null,
"description": "# aws-cdk-config\n\n### Provides typed input parsing for AWS CDK (or really anything else that needs it) allowing for Python built-ins as well as casting to any other class types as well as define validation\n\nCDK provides a method for passing arguments via Parameters, but this mechanism has certain limitations:\n\n* Typing is limited to the same types as CloudFormation and does not support python types that you may want to use in code\n* CDK Parameters are not concrete values, but rather a later resolvable value, and cannot be treated as such\n* Parameters can't be configured in a flat file, but have to be passed as commandline arguments. Often developers use context instead, but this removes things such as typing\n\nBenefits of using CdkConfig:\n\n* Support for using yaml/json files as a config or passing a config as a `dict` from any source in code\n* Input values are concrete values\n* Inputs can be typed as anything that can accept one a yaml or json value (unpacked if a sequence or object) as it's initializer's arguments\n* Inputs can have a callable, including lambda, as a validator\n* Guaranteed immutability of inputs after parsing\n\n\n### Example:\n\nConfig file inputs.yaml\n```yaml\n\ndevelopment:\n GroupName: Foo\n GroupMembers:\n - bar\n - baz\n```\n\nCDK code:\n```python\n#!/user/bin/env python3\nfrom typing import List\n\nfrom aws_cdk_config import CdkConfig\nfrom aws_cdk import (\n Stack,\n aws_iam as iam,\n)\nfrom boto3 import client\nfrom constructs import Construct\n\n\ndef group_exists(name: str) -> bool:\n \"\"\"\n Provides validation to ensure the iam group doesn't already exist\n so we can fail fast if it does.\n \"\"\"\n iam_client = client(\"iam\")\n try:\n iam_client.get_group(GroupName=name)\n except iam_client.exceptions.NoSuchEntityException:\n return True\n return False\n\n\nconfig = CdkConfig(config_file=inputs.yaml, namespace=\"development\")\nconfig.add_input(\n name=\"GroupName\",\n type=str,\n description=\"The name of the group to create\",\n validator=group_exists\n)\nconfig.add_input(\n name=\"GroupUsers\",\n type=List[str],\n description=\"A list of users to add to the group\",\n validator=lambda x: x != \"root\" # Use a lambda as the callable just to keep it simple\n)\nconfig.parse()\n\nclass InputDemo(Stack):\n\n def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:\n super().__init__(scope, construct_id, **kwargs)\n\n group = iam.Group(self, config.GroupName.value)\n\n for username in config.GroupUsers.value:\n user = iam.User.from_user_name(self, username, username)\n group.add_user(user)\n\n```\n\n## Typing\nInputs can be any python type, not just primitives. This means that any types that aren't both YAML, JSON, and python builtins (`str`, `list`, `int`, etc) will be cast by passing the config values to the type. The classes accept arguments in the following ways:\n\n* If the type isn't a python builtin sequence, and yaml/json value is an array, the class must accept the value being passed \"unpacked\" as arguments, eg: `Foo(*myinput)`\n* If the type isn't a python hashable and the yaml/json value is an object, the calss must accept the value being \"unpacked\" being passed as arguments, eg: `Bar(**myinput)`\n\nExample:\n```python\nfrom aws_cdk_config import CdkConfig\n\nclass Foo:\n def __init__(self, arg1, arg2): # Could also be signed as *args\n pass\n\n\nconfig = CdkConfig()\nconfig.add_argument(\n name=\"test\",\n type=Foo,\n value=[\"arg_value_1\", \"arg_value_2\"]\n)\nconfig.parse()\n\n# Returns True\nisinstance(config.test, Foo)\n\n```\n\n```python\nfrom aws_cdk_config import CdkConfig\n\nclass Bar:\n def __init__(self, *, arg1, arg2): # Could also be signed as **kwargs\n pass\n\n\nconfig = CdkConfig()\nconfig.add_argument(\n name=\"test\",\n type=Bar,\n value={\n \"arg1\": \"arg_value_1\",\n \"arg2\": \"arg_value_2\"\n }\n)\nconfig.parse()\n\n# Returns True\nisinstance(config.test, Bar)\n```\n\nSee the examples directory for more examples.",
"bugtrack_url": null,
"license": "",
"summary": "",
"version": "0.1.1",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b775b1f501c7a2d9934bd79d48d68b46e6296f3d9babf5b7d45d8af37915eccc",
"md5": "68f08de5526028ab8dad6e69e4510541",
"sha256": "1b4af17df3f1ffc80706711e6410f96d814fd73a89c886102951e3ac21a53407"
},
"downloads": -1,
"filename": "aws_cdk_config-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "68f08de5526028ab8dad6e69e4510541",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.11,<4.0",
"size": 7668,
"upload_time": "2023-11-22T08:12:26",
"upload_time_iso_8601": "2023-11-22T08:12:26.046270Z",
"url": "https://files.pythonhosted.org/packages/b7/75/b1f501c7a2d9934bd79d48d68b46e6296f3d9babf5b7d45d8af37915eccc/aws_cdk_config-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a02bd274ec079986d95752acd4170e3558fe4ae19ea570f442bde32b9673fb2f",
"md5": "1962144716b08367c69847e698103dcb",
"sha256": "a34883e19bf254db729f0ea691640eb39228359ff9702d2ea1acb89ad6464fea"
},
"downloads": -1,
"filename": "aws_cdk_config-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "1962144716b08367c69847e698103dcb",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11,<4.0",
"size": 6314,
"upload_time": "2023-11-22T08:12:27",
"upload_time_iso_8601": "2023-11-22T08:12:27.185104Z",
"url": "https://files.pythonhosted.org/packages/a0/2b/d274ec079986d95752acd4170e3558fe4ae19ea570f442bde32b9673fb2f/aws_cdk_config-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-22 08:12:27",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "aws-cdk-config"
}