Merge bootstack-ops:bypass-question-mark into bootstack-ops:master

Proposed by Joe Guo
Status: Merged
Approved by: Xav Paice
Approved revision: 8c20339d3f39b0683e03b10ff4789a6bfebeb277
Merged at revision: 4210b3d570fbd74905b004105343d22f3ddccecd
Proposed branch: bootstack-ops:bypass-question-mark
Merge into: bootstack-ops:master
Diff against target: 40 lines (+13/-2)
1 file modified
bootstack-ops/juju_bundle_export.py (+13/-2)
Reviewer Review Type Date Requested Status
Xav Paice (community) Approve
Review via email: mp+384084@code.launchpad.net

Commit message

juju_bundle_export.py: bypass question mark for empty key

In bindings section, option with empty key is allowed:

    bindings:
        "": internal-space

However, after read bundle with pyyaml, it will be dumped to:

    bindings:
        ? ''
        : internal-space

`?` and space is used by yaml spec to indicate complex mapping keys:

    https://yaml.org/spec/1.2/spec.html#id2760695

For some reason, empty str is also considered as complex key here, and there is no way to turn that off in pyyaml.

It's still valid yaml syntax, but may confuse people.

This patch replace empty key to a placeholder, and replace it back after
dump, to bypass this issue.

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Joe Guo (guoqiao) wrote :

I've reported a issue to github/pyyaml: https://github.com/yaml/pyyaml/issues/401
Not sure that can be fixed or not though.

Revision history for this message
Xav Paice (xavpaice) wrote :

We should potentially start to look at either converting the script to use 'juju export-bundle' rather than 'juju dump-model', but that'll have the same trouble.

The actual fix LGTM, and produces a workable bundle, unlike the version without this fix. LGTM.

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Failed to merge change (unable to merge source repository due to conflicts), setting status to needs review.

Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 4210b3d570fbd74905b004105343d22f3ddccecd

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/bootstack-ops/juju_bundle_export.py b/bootstack-ops/juju_bundle_export.py
2index 7e28440..3cefe35 100755
3--- a/bootstack-ops/juju_bundle_export.py
4+++ b/bootstack-ops/juju_bundle_export.py
5@@ -33,6 +33,7 @@ LOG_FORMAT = '%(asctime)s - %(levelname)-9s - %(module)s - %(message)s'
6 logging.basicConfig(format=LOG_FORMAT)
7 log = logging.getLogger(__name__)
8
9+EMPTY_KEY_PLACE_HOLDER = '__EMPTY_KEY_PLACE_HOLDER__'
10
11 MASK_KEYS = ('(.*(ssl-public-key|ssl[_-](ca|cert|key|chain)|secret|password|'
12 'pagerduty_key|license-file|registration-key|token|accesskey|'
13@@ -76,6 +77,16 @@ class Bundle(object):
14 else:
15 app_data['options'][option] = option_value
16 else:
17+ if param == 'bindings':
18+ # in bindings, option with empty key like `"": internal-space` is allowed.
19+ # however, yaml will consider empty key as complex mapping key
20+ # and dump it to `? ''\n: internal-space`
21+ # here we replace the empty key to a placeholder to bypass this issue
22+ # it will be replaced back after dumped
23+ # ref: https://yaml.org/spec/1.2/spec.html#id2760695
24+ if '' in param_value:
25+ param_value[EMPTY_KEY_PLACE_HOLDER] = param_value['']
26+ del param_value['']
27 app_data[param] = param_value
28 self.common_bundle['applications'][app] = app_data
29 if len(secret_data.keys()) > 0:
30@@ -311,8 +322,8 @@ def main():
31 'common': entire_bundle.get_common_bundle,
32 'secrets': entire_bundle.get_secrets_bundle,
33 }
34- print(yaml.dump(grab_out[args.output_type](), default_flow_style=False),
35- file=args.outfile)
36+ output = yaml.dump(grab_out[args.output_type](), default_flow_style=False)
37+ print(output.replace(EMPTY_KEY_PLACE_HOLDER, '""'), file=args.outfile)
38
39
40 if __name__ == "__main__":

Subscribers

People subscribed via source and target branches

to all changes: