salt icon indicating copy to clipboard operation
salt copied to clipboard

Provides full featured salt.states.xml

Open baby-gnu opened this issue 6 years ago • 5 comments

Is your feature request related to a problem? Please describe.

I can't add new XML nodes or remove sub trees with salt.states.xml.

Describe the solution you'd like

I would like to be able to:

  • add new elements with its value:
    add-new-element-with-simple-value:
      xml.value_present:
        - name: /tmp/test.xml
        - xpath: /some/xpath/query
        - value: some value
    
  • remove elements or subtrees:
    remove-element:
      xml.absent:
        - name: /tmp/test.xml
        - xpath: /some/xpath/spec
    
  • add new elements with its value and attributes (generalisation of the previous one):
    add-new-element-with-value-and-attributes:
      xml.present:
        - name: /tmp/test.xml
        - xpath: /some/xpath/query
        - value: some value
        - attributes:
            attr1: val1
            attr2: val2
    
  • use a dict to set values (inspired by xmltodict):
    {%- set values_dict = {'root': {
                             'subkey1': 'value1',
                             'subkey2': {
                               '@attr1': 'val1',
                               '@attr2': 'val2',
                               '#text': 'value2'
                             },
                             'subkey3': [
                               'value3.1',
                               'value3.2'
                             ]
                           }} %}
    
    add-or-set-elements-from-dict:
      xml.present:
        - name: /tmp/test.xml
        - content: {{ values_dict }}
    

Describe alternatives you've considered

I'm using salt.states.augeas.change with xml.lens:

simple-xml-value:
  augeas.change:
    - lens: xml.lns
    - context: /files/tmp/test.xml
    - changes:
      - set some/xpath/query/#text 'value'

Additional context

I would like to provide a formula to manage OPNsense configuration like this ansible role

baby-gnu avatar Aug 27 '20 12:08 baby-gnu

related to #58245

mchugh19 avatar Aug 29 '20 08:08 mchugh19

You can get surprisingly far with the changes that I added, but an 'absent' state is still needed to completely cover this request.

Timbus avatar Aug 30 '20 03:08 Timbus

Interesting @Timbus.

So with your PR I need to build the XML fragment from my source dict and then merge it with the rest of XML, right?

baby-gnu avatar Aug 31 '20 13:08 baby-gnu

Interesting @Timbus.

So with your PR I need to build the XML fragment from my source dict and then merge it with the rest of XML, right?

Yes, unfortunately I didn't have generated/dynamic data in mind when I made my changes.. I had more of a 'I need to merge/add static configs to an XML file' use case. I didn't want to approach the difficult task of mapping yaml or python dicts to XML either.

If you have a dictionary of data to dump, you might want more low-level states.. or some kind of full tree builder, for your needs.. Oh, perhaps the xmlutil in the util folder would help too?

Timbus avatar Sep 02 '20 04:09 Timbus

Hi, great addition may I suggest to handle namespaces as well ?

CapBananoid avatar Oct 08 '20 12:10 CapBananoid