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

Add a method to add outputs to a Stack #229

Merged
merged 1 commit into from
Oct 2, 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
9 changes: 8 additions & 1 deletion src/e3/aws/troposphere/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations
from abc import ABC, abstractmethod
from itertools import chain
from troposphere import AWSObject, Template
from troposphere import AWSObject, Output, Template

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

return self

def add_output(self, output: Output | list[Output]) -> None:
"""Add outputs to stack template.

:param output: output to add to the template
"""
self.template.add_output(output)

def add_condition(self, condition_name: str, condition: ConditionFunction) -> None:
"""Add condition to stack template.

Expand Down
32 changes: 32 additions & 0 deletions tests/tests_e3_aws/troposphere/stack/stack_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
"""Provide Stack tests."""
import json
from pathlib import Path

from troposphere import Output, Export

from e3.aws.troposphere.s3.bucket import Bucket
from e3.aws.troposphere import Stack

TEST_DIR = Path(__file__).parent


def test_instanciate() -> None:
"""Test stack instanciation."""
Expand All @@ -16,3 +22,29 @@ def test_add_and_get_item() -> None:
stack.add(Bucket("my-bucket"))
my_bucket = stack["my-bucket"]
assert my_bucket


def test_add_outputs() -> None:
"""Test adding outputs to a stack."""
stack = Stack("test-stack", "this is a test stack")
stack.add(Bucket("my-bucket"))
stack.add_output(
Output("MyOutput1", Description="My first output", Value=Export(name="Output1"))
)
stack.add_output(
[
Output(
"MyOutput2",
Description="My second output",
Value=Export(name="Output2"),
),
Output(
"MyOutput3", Description="My third output", Value=Export(name="Output3")
),
]
)

with open(TEST_DIR / "stack_with_outputs.json") as fd:
expected_template = json.load(fd)

assert stack.export()["Resources"] == expected_template
78 changes: 78 additions & 0 deletions tests/tests_e3_aws/troposphere/stack/stack_with_outputs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"MyBucket": {
"Properties": {
"BucketName": "my-bucket",
"BucketEncryption": {
"ServerSideEncryptionConfiguration": [
{
"ServerSideEncryptionByDefault": {
"SSEAlgorithm": "AES256"
}
}
]
},
"PublicAccessBlockConfiguration": {
"BlockPublicAcls": true,
"BlockPublicPolicy": true,
"IgnorePublicAcls": true,
"RestrictPublicBuckets": true
},
"VersioningConfiguration": {
"Status": "Enabled"
}
},
"Type": "AWS::S3::Bucket"
},
"MyBucketPolicy": {
"Properties": {
"Bucket": {
"Ref": "MyBucket"
},
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Deny",
"Principal": {
"AWS": "*"
},
"Action": "s3:*",
"Resource": "arn:aws:s3:::my-bucket/*",
"Condition": {
"Bool": {
"aws:SecureTransport": "false"
}
}
},
{
"Effect": "Deny",
"Principal": {
"AWS": "*"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::my-bucket/*",
"Condition": {
"StringNotEquals": {
"s3:x-amz-server-side-encryption": "AES256"
}
}
},
{
"Effect": "Deny",
"Principal": {
"AWS": "*"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::my-bucket/*",
"Condition": {
"Null": {
"s3:x-amz-server-side-encryption": "true"
}
}
}
]
}
},
"Type": "AWS::S3::BucketPolicy"
}
}
Loading