saltstack-formulas / snmp-formula

http://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html
Other
17 stars 49 forks source link

Replace [].__class__ with is_list, fixes #40 #41

Closed alxwr closed 3 years ago

alxwr commented 3 years ago

PR progress checklist (to be filled in by reviewers)


What type of PR is this?

Primary type

Secondary type

Does this PR introduce a BREAKING CHANGE?

No.

Related issues and/or pull requests

Fixes #40

Describe the changes you're proposing

Fixes this error in Salt 3002.5 on FreeBSD 12.2:

----------                                                                                                            
          ID: snmp_conf                                                                                               
    Function: file.managed                                                                                            
        Name: /usr/local/etc/snmp/snmpd.conf                                                                          
      Result: False                                                                                                   
     Comment: Unable to manage file: Jinja syntax error: access to attribute '__class__' of 'list' object is unsafe.; 
line 513                                                                                                              

              ---                                                                                                     
              [...]                                                                                                   
              rwuser {{ user.username }} {{ user.get('securitylevel', 'auth') }} -V {{ user.view }}                   
              createUser {{ user.username }} {{ user.get('authproto', 'MD5') }} {{ user.get('authpassphrase', user.pas
sphrase) }} {{ user.get('privproto', 'AES') }} {{ user.get('privpassphrase', user.passphrase) }}                      
              {%- endfor %}                                                                                           

              {% for declaration, values in config.items() %}                                                         
              {%- if values.__class__ in (().__class__, [].__class__) %}    <======================                   
                  {%- for value in values %}                                                                          
              {{declaration}} {{value}}                                                                               
                  {%- endfor %}                                                                                       
              {%- else %}                                                                                             
              {{declaration}} {{values}}                                                                              
              [...]                                                                                                   
              ---                                                                                                     
     Started: 02:55:22.889450                                                                                         
    Duration: 202.968 ms                                                                                              
     Changes:

Pillar / config required to test the proposed changes

Debug log showing how the proposed changes work

% salt 'main.srv.tty1.eu' state.apply snmp.managed      
main.srv.tty1.eu:
----------
          ID: snmp
    Function: pkg.installed
        Name: net-snmp
      Result: True
     Comment: All specified packages are already installed
     Started: 03:16:05.653761
    Duration: 86.867 ms
     Changes:   
----------
          ID: snmp_conf
    Function: file.managed
        Name: /usr/local/etc/snmp/snmpd.conf
      Result: True
     Comment: File /usr/local/etc/snmp/snmpd.conf is in the correct state
     Started: 03:16:05.746414
    Duration: 211.492 ms
     Changes:   
----------
          ID: snmp
    Function: service.running
        Name: snmpd
      Result: True
     Comment: The service snmpd is already running
     Started: 03:16:05.958180
    Duration: 83.13 ms
     Changes:   

Summary for main.srv.tty1.eu
------------
Succeeded: 3
Failed:    0
------------
Total states run:     3
Total run time: 381.489 ms

Documentation checklist

Testing checklist

Additional context

myii commented 3 years ago

Appreciate the quick fix, @alxwr! Tested out locally with the latest pre-salted images.