Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docs): python docs for aspect #3002

Merged
merged 4 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions examples/python/documentation/aspects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
# SPDX-License-Identifier: MPL-2.0

import jsii
from constructs import Construct
from cdktf import TerraformStack

from imports.aws.provider import AwsProvider
from imports.random.provider import RandomProvider
from imports.random.pet import Pet

# DOCS_BLOCK_START:define-aspects
from constructs import IConstruct
from cdktf import Aspects, IAspect
from constructs import Construct, IConstruct
from cdktf import TerraformStack, Aspects, IAspect
from imports.aws.instance import Instance
from imports.aws.provider import AwsProvider

@jsii.implements(IAspect)
class TagsAddingAspect:
Expand All @@ -25,6 +24,20 @@ def visit(self, node: IConstruct):
# We need to take the input value to not create a circular reference
currentTags = node.tags_input if node.tags_input is not None else {}
node.tags = {**self.tagsToAdd, **currentTags}

class MySingleStack(TerraformStack):
def __init__(self, scope: Construct, id: str):
super().__init__(scope, id)
AwsProvider(self, "aws",
region = "us-east-1"
)
Instance(self, "Hello",
ami = "ami-2757f631",
instance_type = "t2.micro"
)

# Add tags to every resource defined within `MySingleStack`.
Aspects.of(self).add(TagsAddingAspect({ "createdBy": "cdktf" }))
# DOCS_BLOCK_END:define-aspects

# DOCS_BLOCK_START:aspects-validation
Expand Down
20 changes: 18 additions & 2 deletions website/docs/cdktf/concepts/aspects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ export class AspectTaggingStack extends TerraformStack {
```

```python
from constructs import IConstruct
from cdktf import Aspects, IAspect
from constructs import Construct, IConstruct
from cdktf import TerraformStack, Aspects, IAspect
from imports.aws.instance import Instance
from imports.aws.provider import AwsProvider

@jsii.implements(IAspect)
class TagsAddingAspect:
Expand All @@ -162,6 +164,20 @@ class TagsAddingAspect:
currentTags = node.tags_input if node.tags_input is not None else {}
node.tags = {**self.tagsToAdd, **currentTags}

class MySingleStack(TerraformStack):
def __init__(self, scope: Construct, id: str):
super().__init__(scope, id)
AwsProvider(self, "aws",
region = "us-east-1"
)
Instance(self, "Hello",
ami = "ami-2757f631",
instance_type = "t2.micro"
)

# Add tags to every resource defined within `MySingleStack`.
Aspects.of(self).add(TagsAddingAspect({ "createdBy": "cdktf" }))

# Add tags to every resource defined within `myStack`.
Aspects.of(myStack).add(TagsAddingAspect({ "createdBy": "cdktf" }))
```
Expand Down
Loading