Skip to content

Commit a4ffa86

Browse files
committed
Added msk_bootstrap function. Documented functions
1 parent e4a2aee commit a4ffa86

File tree

5 files changed

+178
-1
lines changed

5 files changed

+178
-1
lines changed

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ docker-compose
8787
installation
8888
input
8989
features
90+
jinja2_functions_filters
9091
examples
9192
modules
9293
contributing

docs/jinja2_functions_filters.rst

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
.. meta::
2+
:description: ECS Files Composer input config
3+
:keywords: AWS, ECS, Configuration, Jinja
4+
5+
====================================
6+
Custom Jinja2 filters & Functions
7+
====================================
8+
9+
These filters come in addition to existing `Jinja2 Filters`_ & Functions.
10+
11+
------------
12+
Functions
13+
------------
14+
15+
AWS ECS
16+
=================
17+
18+
ecs_container_metadata
19+
------------------------
20+
21+
Filter that returns the ECS Container Metadata information
22+
23+
Parameters:
24+
25+
* ``property_key``: the property in the metadata you want to retrieve
26+
* ``fallback_value``: a value to use if case the property is missing.
27+
28+
29+
ecs_task_metadata
30+
------------------
31+
32+
Filter that returns the ECS Task Metadata information
33+
34+
Parameters:
35+
36+
* ``property_key``: the property in the metadata you want to retrieve
37+
* ``fallback_value``: a value to use if case the property is missing.
38+
39+
40+
41+
AWS Specific Filters
42+
=====================
43+
44+
These filters use AWS API to retrieve specific properties.
45+
46+
msk_bootstrap
47+
---------------
48+
49+
Parameters:
50+
* ``cluster_arn``: the ARN of the MSK cluster to use
51+
* ``broker_type``: the type of Broker endpoints to use.
52+
53+
54+
For valid values for ``broker_type``, refer to `boto3.kafka.get_bootstrap_brokers`_ documentation,
55+
and see the ``Response Syntax`` section.
56+
57+
58+
Example to retrieve the Bootstrap servers for SASL + IAM for privately addressed cluster.
59+
60+
.. code-block:: yaml
61+
62+
files:
63+
/tmp/conduktor-cdk.yaml:
64+
context: jinja2
65+
content: |
66+
organization:
67+
name: ${ORG_NAME:-testing}
68+
clusters:
69+
- id: amazon-msk-iam
70+
name: Amazon MSK IAM
71+
color: #FF9900
72+
bootstrapServers: {{ msk_bootstrap(env_var('BOOTSTRAP_ARN'), 'BootstrapBrokerStringSaslIam') }}
73+
properties: |
74+
security.protocol=SASL_SSL
75+
sasl.mechanism=AWS_MSK_IAM
76+
sasl.jaas.config=software.amazon.msk.auth.iam.IAMLoginModule required;
77+
sasl.client.callback.handler.class=io.conduktor.aws.IAMClientCallbackHandler
78+
79+
from_ssm
80+
----------
81+
82+
Returns a value pulled from a SSM parameter.
83+
84+
Parameters
85+
86+
* ``parameter_name``: The name of the SSM parameter to get the value from.
87+
88+
Example
89+
90+
.. code-block:: yaml
91+
92+
files:
93+
testing:
94+
content: |
95+
my_value: {{ from_ssm('my/ssm/parameter') }}
96+
97+
98+
Generic Functions
99+
====================
100+
101+
Simple functions to gap missing ones from Jinja2
102+
103+
env_var
104+
-----------
105+
106+
Retrieves a value from environment variable. Can set a default value.
107+
108+
Parameters
109+
110+
* ``key``: Name of the environment variable
111+
* ``value``: Default value in case the environment variable is not set.
112+
113+
114+
Example
115+
116+
.. code-block:: yaml
117+
118+
files:
119+
testing:
120+
content: |
121+
my_config_from_env_var: {{ env_var('ENV_VAR_NAME', "a default value") }}
122+
123+
124+
---------------
125+
Filters
126+
---------------
127+
128+
Generic filters
129+
=================
130+
131+
env_override
132+
-------------
133+
134+
Similar to `env_var`_, it sets a value from environment variable, but expect a value to be already set.
135+
136+
to_yaml
137+
---------
138+
139+
Renders an input into YAML
140+
141+
to_json
142+
--------
143+
144+
Renders an input into JSON
145+
146+
147+
.. _boto3.kafka.get_bootstrap_brokers: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/kafka.html#Kafka.Client.get_bootstrap_brokers
148+
.. _Jinja2 Filters: https://jinja.palletsprojects.com/en/3.1.x/templates/#id11

ecs_files_composer/input.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# generated by datamodel-codegen:
22
# filename: ecs-files-input.json
3-
# timestamp: 2022-10-15T11:30:13+00:00
3+
# timestamp: 2022-11-16T09:12:19+00:00
44

55
from __future__ import annotations
66

ecs_files_composer/jinja2_filters/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import yaml
1313
from boto3.session import Session
1414

15+
from .aws_filters import msk_bootstrap
16+
1517

1618
def env_override(value, key):
1719
"""
@@ -174,6 +176,7 @@ def from_ssm_json(parameter_name: str) -> dict:
174176
"env_var": env_var,
175177
"from_ssm": from_ssm,
176178
"from_ssm_json": from_ssm_json,
179+
"msk_bootstrap": msk_bootstrap,
177180
}
178181

179182
JINJA_FILTERS = {"to_yaml": to_yaml, "to_json": to_json, "env_override": env_override}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# SPDX-License-Identifier: MPL-2.0
2+
# Copyright 2020-2022 John Mille<[email protected]>
3+
4+
"""
5+
AWS Based filters
6+
"""
7+
8+
from __future__ import annotations
9+
10+
from boto3.session import Session
11+
from compose_x_common.compose_x_common import keyisset
12+
13+
14+
def msk_bootstrap(msk_arn: str, broker_type: str) -> str:
15+
"""
16+
Uses the ARN of a MSK cluster,
17+
and returns the list of BootStrap endpoints for a private MSK cluster using SASL IAM.
18+
If failed, returns the ARN.
19+
"""
20+
session = Session()
21+
client = session.client("kafka")
22+
brokers_r = client.get_bootstrap_brokers(ClusterArn=msk_arn)
23+
if keyisset(broker_type, brokers_r):
24+
return brokers_r[broker_type]
25+
return msk_arn

0 commit comments

Comments
 (0)