Use ruamel.yaml for config and secret handling

This commit is contained in:
Stefan Reimer 2023-10-24 14:16:38 +00:00
parent ff58d43ff1
commit 260c44c49b
2 changed files with 9 additions and 3 deletions

View File

@ -15,6 +15,8 @@ from dateutil.tz import tzutc
from botocore.exceptions import ClientError from botocore.exceptions import ClientError
import ruamel.yaml
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, render_docs from .jinja import JinjaEnv, read_config_file, render_docs
@ -952,6 +954,10 @@ class Stack(object):
def set_config(self, key, value, secret): def set_config(self, key, value, secret):
"""Set a config or secret""" """Set a config or secret"""
ryaml = ruamel.yaml.YAML()
ryaml.indent(mapping=2)
ryaml.preserve_quotes = True
pulumi_stack = self._get_pulumi_stack(create=True) pulumi_stack = self._get_pulumi_stack(create=True)
pulumi_stack.set_config(key, pulumi.automation.ConfigValue(value, secret)) # Pulumi bug https://github.com/pulumi/pulumi/issues/13063 so no: , path=True) pulumi_stack.set_config(key, pulumi.automation.ConfigValue(value, secret)) # Pulumi bug https://github.com/pulumi/pulumi/issues/13063 so no: , path=True)
@ -962,7 +968,7 @@ class Stack(object):
)._serialize() )._serialize()
with open(self.path, "r") as file: with open(self.path, "r") as file:
settings = yaml.safe_load(file) settings = ryaml.load(file)
if "pulumi" not in settings: if "pulumi" not in settings:
settings["pulumi"] = {} settings["pulumi"] = {}
@ -975,7 +981,6 @@ class Stack(object):
if "parameters" not in settings: if "parameters" not in settings:
settings["parameters"] = {} settings["parameters"] = {}
# hack for bug above, we support one level of nested values for now # hack for bug above, we support one level of nested values for now
_val = pulumi_settings["config"]["{}:{}".format(self.parameters["Conglomerate"], key)] _val = pulumi_settings["config"]["{}:{}".format(self.parameters["Conglomerate"], key)]
if '.' in key: if '.' in key:
@ -988,7 +993,7 @@ class Stack(object):
settings["parameters"][key] = _val settings["parameters"][key] = _val
with open(self.path, "w") as file: with open(self.path, "w") as file:
yaml.dump(settings, stream=file) ryaml.dump(settings, stream=file)
return return

View File

@ -5,6 +5,7 @@ click
pexpect pexpect
python-minifier python-minifier
cfn-lint>=0.34 cfn-lint>=0.34
ruamel-yaml
pulumi>=3.80.0 pulumi>=3.80.0
pulumi-aws>6.0.0 pulumi-aws>6.0.0
pulumi-aws-native pulumi-aws-native