File tree Expand file tree Collapse file tree 4 files changed +53
-7
lines changed Expand file tree Collapse file tree 4 files changed +53
-7
lines changed Original file line number Diff line number Diff line change 8
8
import os
9
9
import sys
10
10
11
- from jinja2 import Template
11
+ from jinja2 import Environment
12
12
13
13
from ..util import parse_cloudformation_template
14
14
from ..exceptions import InvalidConfig , UnresolvedVariable
@@ -196,17 +196,25 @@ def rendered(self):
196
196
if not self ._rendered :
197
197
template_path = get_template_path (self .raw_template_path )
198
198
if template_path :
199
- with open (template_path , 'r' ) as template :
199
+ with open (template_path , 'r' ) as template_file :
200
+ template_data = template_file .read ()
201
+
200
202
if len (os .path .splitext (template_path )) == 2 and (
201
203
os .path .splitext (template_path )[1 ] == '.j2' ):
202
- self ._rendered = Template (template .read ()).render (
204
+
205
+ ext_name = ('stacker.blueprints.templating'
206
+ '.TroposphereFnHandlerExtension' )
207
+ environment = Environment (extensions = [ext_name ])
208
+ template = environment .from_string (template_data )
209
+
210
+ self ._rendered = template .render (
203
211
context = self .context ,
204
212
mappings = self .mappings ,
205
213
name = self .name ,
206
214
variables = self .resolved_variables
207
215
)
208
216
else :
209
- self ._rendered = template . read ()
217
+ self ._rendered = template_data
210
218
else :
211
219
raise InvalidConfig (
212
220
'Could not find template %s' % self .raw_template_path
Original file line number Diff line number Diff line change
1
+ from __future__ import print_function
2
+ from __future__ import division
3
+ from __future__ import absolute_import
4
+ import json
5
+
6
+ from jinja2 .lexer import Token
7
+ from jinja2 .ext import Extension
8
+ from troposphere import AWSHelperFn
9
+
10
+
11
+ def render_troposphere_fn (obj ):
12
+ if isinstance (obj , AWSHelperFn ):
13
+ return json .dumps (obj .to_dict ())
14
+
15
+ return obj
16
+
17
+
18
+ class TroposphereFnHandlerExtension (Extension ):
19
+ def __init__ (self , environment ):
20
+ super (TroposphereFnHandlerExtension , self ).__init__ (environment )
21
+ environment .filters ['render_troposphere_fn' ] = render_troposphere_fn
22
+
23
+ def filter_stream (self , stream ):
24
+ for token in stream :
25
+ if token .type == 'variable_end' :
26
+ yield Token (token .lineno , 'pipe' , '|' )
27
+ yield Token (token .lineno , 'name' , 'render_troposphere_fn' )
28
+
29
+ yield token
Original file line number Diff line number Diff line change 5
5
import json
6
6
import unittest
7
7
8
+ import troposphere
8
9
from mock import MagicMock
9
10
10
11
from stacker .blueprints .raw import (
@@ -144,6 +145,9 @@ def test_j2_to_json(self):
144
145
"Outputs" : {
145
146
"DummyId" : {
146
147
"Value" : "dummy-bar-param1val-foo-1234"
148
+ },
149
+ "DummyTroposphereFn" : {
150
+ "Value" : {"Ref" : "AWS::Region" }
147
151
}
148
152
}
149
153
},
@@ -156,13 +160,15 @@ def test_j2_to_json(self):
156
160
extra_config_args = {'stacks' : [{'name' : 'stack1' ,
157
161
'template_path' : 'unused' ,
158
162
'variables' : {
159
- 'Param1' : 'param1val' ,
160
- 'bar' : 'foo' }}]},
163
+ 'Param1' : '' ,
164
+ 'bar' : '' ,
165
+ 'region' : '' }}]},
161
166
environment = {'foo' : 'bar' }),
162
167
raw_template_path = RAW_J2_TEMPLATE_PATH
163
168
)
164
169
blueprint .resolve_variables ([Variable ("Param1" , "param1val" ),
165
- Variable ("bar" , "foo" )])
170
+ Variable ("bar" , "foo" ),
171
+ Variable ("region" , troposphere .Region )])
166
172
self .assertEqual (
167
173
expected_json ,
168
174
blueprint .to_json ()
Original file line number Diff line number Diff line change 18
18
"Outputs": {
19
19
"DummyId": {
20
20
"Value": "dummy-{{ context.environment.foo }}-{{ variables.Param1 }}-{{ variables.bar }}-1234"
21
+ },
22
+ "DummyTroposphereFn": {
23
+ "Value": {{ variables.region }}
21
24
}
22
25
}
23
26
}
You can’t perform that action at this time.
0 commit comments