feat: improve docs task to render pulumi docs with live outputs

This commit is contained in:
Stefan Reimer 2022-07-01 14:56:53 +02:00
parent adc92bf24a
commit 1e7665f2bb
3 changed files with 37 additions and 8 deletions

View File

@ -200,6 +200,12 @@ def JinjaEnv(template_locations=[]):
return jenv return jenv
def render_docs(docs, outputs):
jenv = jinja2.Environment(undefined=jinja2.ChainableUndefined)
return jenv.from_string(docs).render(outputs)
def read_config_file(path, variables={}): def read_config_file(path, variables={}):
"""reads yaml config file, passes it through jinja and returns data structre """reads yaml config file, passes it through jinja and returns data structre

View File

@ -31,6 +31,20 @@ def get_pulumi_version():
return None return None
def resolve_outputs(outputs):
my_outputs = {}
for k,v in outputs.items():
if type(v) == pulumi.automation._output.OutputValue:
if v.secret:
my_outputs[k] = "***"
else:
my_outputs[k] = v.value
else:
my_outputs[k] = v
return my_outputs
def pulumi_ws(func): def pulumi_ws(func):
@wraps(func) @wraps(func)
def decorated(self, *args, **kwargs): def decorated(self, *args, **kwargs):

View File

@ -17,11 +17,11 @@ from botocore.exceptions import ClientError
from .utils import dict_merge, search_refs, ensure_dir, get_s3_url from .utils import dict_merge, search_refs, ensure_dir, get_s3_url
from .connection import BotoConnection from .connection import BotoConnection
from .jinja import JinjaEnv, read_config_file from .jinja import JinjaEnv, read_config_file, render_docs
from . import __version__ from . import __version__
from .exceptions import ParameterNotFound, ParameterIllegalValue, ChecksumError from .exceptions import ParameterNotFound, ParameterIllegalValue, ChecksumError
from .hooks import exec_hooks from .hooks import exec_hooks
from .pulumi import pulumi_ws from .pulumi import pulumi_ws, resolve_outputs
import cfnlint.core import cfnlint.core
import cfnlint.template import cfnlint.template
@ -581,22 +581,31 @@ class Stack(object):
""" """
if self.mode == "pulumi": if self.mode == "pulumi":
try:
pulumi_stack = self._get_pulumi_stack()
outputs=pulumi_stack.outputs()
except pulumi.automation.errors.StackNotFoundError:
outputs = {}
pass
if vars(self._pulumi_code)["__doc__"]: if vars(self._pulumi_code)["__doc__"]:
print(vars(self._pulumi_code)["__doc__"]) output= render_docs(vars(self._pulumi_code)["__doc__"], resolve_outputs(outputs))
else: else:
print("No template documentation found.") output = "No template documentation found."
# collect all __doc__ from available _execute_ functions # collect all __doc__ from available _execute_ functions
_help = "" headerAdded = False
for k in vars(self._pulumi_code).keys(): for k in vars(self._pulumi_code).keys():
if k.startswith("_execute_"): if k.startswith("_execute_"):
if not headerAdded:
output = output + "\n# Available `execute` functions: \n"
headerAdded = True
docstring = vars(self._pulumi_code)[k].__doc__ docstring = vars(self._pulumi_code)[k].__doc__
_help = _help + "## {}\n{}\n".format( output = output + "\n* {}\n{}".format(
k.lstrip("_execute_"), docstring k.lstrip("_execute_"), docstring
) )
if _help: print(output)
print(f"# Available `execute` functions: \n\n{_help}")
else: else:
try: try: