Skip to content

Commit 4b2ac7a

Browse files
authored
Merge pull request #229 from pierretr/master
Add a method to add outputs to a Stack
2 parents 2fa81ec + 90d1bac commit 4b2ac7a

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

src/e3/aws/troposphere/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22
from abc import ABC, abstractmethod
33
from itertools import chain
4-
from troposphere import AWSObject, Template
4+
from troposphere import AWSObject, Output, Template
55

66
from e3.aws import cfn, name_to_id, Session
77
from e3.aws.cfn.main import CFNMain
@@ -130,6 +130,13 @@ def add(self, element: AWSObject | Construct | Stack) -> Stack:
130130

131131
return self
132132

133+
def add_output(self, output: Output | list[Output]) -> None:
134+
"""Add outputs to stack template.
135+
136+
:param output: output to add to the template
137+
"""
138+
self.template.add_output(output)
139+
133140
def add_condition(self, condition_name: str, condition: ConditionFunction) -> None:
134141
"""Add condition to stack template.
135142

tests/tests_e3_aws/troposphere/stack/stack_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
"""Provide Stack tests."""
2+
import json
3+
from pathlib import Path
4+
5+
from troposphere import Output, Export
26

37
from e3.aws.troposphere.s3.bucket import Bucket
48
from e3.aws.troposphere import Stack
59

10+
TEST_DIR = Path(__file__).parent
11+
612

713
def test_instanciate() -> None:
814
"""Test stack instanciation."""
@@ -16,3 +22,29 @@ def test_add_and_get_item() -> None:
1622
stack.add(Bucket("my-bucket"))
1723
my_bucket = stack["my-bucket"]
1824
assert my_bucket
25+
26+
27+
def test_add_outputs() -> None:
28+
"""Test adding outputs to a stack."""
29+
stack = Stack("test-stack", "this is a test stack")
30+
stack.add(Bucket("my-bucket"))
31+
stack.add_output(
32+
Output("MyOutput1", Description="My first output", Value=Export(name="Output1"))
33+
)
34+
stack.add_output(
35+
[
36+
Output(
37+
"MyOutput2",
38+
Description="My second output",
39+
Value=Export(name="Output2"),
40+
),
41+
Output(
42+
"MyOutput3", Description="My third output", Value=Export(name="Output3")
43+
),
44+
]
45+
)
46+
47+
with open(TEST_DIR / "stack_with_outputs.json") as fd:
48+
expected_template = json.load(fd)
49+
50+
assert stack.export()["Resources"] == expected_template
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"MyBucket": {
3+
"Properties": {
4+
"BucketName": "my-bucket",
5+
"BucketEncryption": {
6+
"ServerSideEncryptionConfiguration": [
7+
{
8+
"ServerSideEncryptionByDefault": {
9+
"SSEAlgorithm": "AES256"
10+
}
11+
}
12+
]
13+
},
14+
"PublicAccessBlockConfiguration": {
15+
"BlockPublicAcls": true,
16+
"BlockPublicPolicy": true,
17+
"IgnorePublicAcls": true,
18+
"RestrictPublicBuckets": true
19+
},
20+
"VersioningConfiguration": {
21+
"Status": "Enabled"
22+
}
23+
},
24+
"Type": "AWS::S3::Bucket"
25+
},
26+
"MyBucketPolicy": {
27+
"Properties": {
28+
"Bucket": {
29+
"Ref": "MyBucket"
30+
},
31+
"PolicyDocument": {
32+
"Version": "2012-10-17",
33+
"Statement": [
34+
{
35+
"Effect": "Deny",
36+
"Principal": {
37+
"AWS": "*"
38+
},
39+
"Action": "s3:*",
40+
"Resource": "arn:aws:s3:::my-bucket/*",
41+
"Condition": {
42+
"Bool": {
43+
"aws:SecureTransport": "false"
44+
}
45+
}
46+
},
47+
{
48+
"Effect": "Deny",
49+
"Principal": {
50+
"AWS": "*"
51+
},
52+
"Action": "s3:PutObject",
53+
"Resource": "arn:aws:s3:::my-bucket/*",
54+
"Condition": {
55+
"StringNotEquals": {
56+
"s3:x-amz-server-side-encryption": "AES256"
57+
}
58+
}
59+
},
60+
{
61+
"Effect": "Deny",
62+
"Principal": {
63+
"AWS": "*"
64+
},
65+
"Action": "s3:PutObject",
66+
"Resource": "arn:aws:s3:::my-bucket/*",
67+
"Condition": {
68+
"Null": {
69+
"s3:x-amz-server-side-encryption": "true"
70+
}
71+
}
72+
}
73+
]
74+
}
75+
},
76+
"Type": "AWS::S3::BucketPolicy"
77+
}
78+
}

0 commit comments

Comments
 (0)