Skip to content

Commit

Permalink
Add initial support Dashboard Studio
Browse files Browse the repository at this point in the history
- Updated the 'xml-format' command to indent the JSON blobs stored in xml
  dashboards (version=2).  Consider this a beta feature. . Testing needed.
- Update change log.
  • Loading branch information
lowell80 committed Feb 10, 2023
1 parent 0411624 commit 682af4e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ Ksconf v0.10.0 (DRAFT)
Originally, meaning *Kintyre's Splunk CONFiguration tool*,
now becomes a recursive acronym: *Ksconf Splunk CONFiguration tool*.
Kintyre has been acquired by CDI LLC, and this option seemed least intrusive.
* Add support Dashboard Studio dashboards.
The JSON blobs inside of Simple XML payloads can now be formatted too.
Multiline searches are stiff difficult to diff, but it should be better than before.


More changes:
Expand All @@ -55,6 +58,9 @@ Bug fixes:
* Fixed ``sort`` bug where the user was incorrectly told that a file with errors was unexpectedly also successfully replaced.
The contradictory output messages have been cleaned up.
For clarity, this only occurred for inline replacement mode, and was purely an output issue, not a file handling problem.
* Fixed compatibility issues with ``rest-publish`` command and the splunk-sdk library around data type expectations.
A big thanks to bayeslearner (`#95 <https://github.com/Kintyre/ksconf/issues/95>`__) for the fix.
If you run into any issues, try upgrading your version of ``splunk-sdk``.


Ksconf 0.9
Expand Down
25 changes: 25 additions & 0 deletions ksconf/xmlformat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
import re
from io import BytesIO
Expand Down Expand Up @@ -126,6 +127,29 @@ def guess_indent(elem: ElementTree, default=2):
indent = default
return indent

@classmethod
def format_json(cls, elem: ElementTree, indent=2):
""" Format JSON data within a Dashboard Studio dashboard. This is
still pretty limited (for example, long searches still show up on a
single line), but this give you at least a fighting change to figure
out what's different.
"""

# Abort if this isn't Dashboard Studio embedded in Simple XML
if not elem.attrib.get("version") == "2":
return

tags = ["definition", "assets"]
for tag in tags:
for e in elem.findall(f".//{tag}"):
if e.text:
data = json.loads(e.text)
output = json.dumps(data, indent=indent, sort_keys=False)
# Multiple lines? Spread out CDATA header/trailer
if "\n" in output:
output = f"\n{output}\n"
e.text = etree.CDATA(output)

@classmethod
def format_xml(cls, src, dest, default_indent=2):
# Copied from https://stackoverflow.com/a/5649263/315892
Expand All @@ -135,6 +159,7 @@ def format_xml(cls, src, dest, default_indent=2):
i = cls.guess_indent(root, default_indent)
cls.indent_tree(root, indent=i)
cls.cdata_tags(root, ["query"])
cls.format_json(root, indent=i)
cls.expand_tags(root, cls.keep_tags)

b = BytesIO()
Expand Down

0 comments on commit 682af4e

Please sign in to comment.