New version of salt-formula from Saltstack
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.7KB

  1. from collections import OrderedDict
  2. def __virtual__():
  3. return True
  4. def _set_subtree(node, relationships):
  5. return {
  6. v: _set_subtree(v, relationships)
  7. for v in [x['id'] for x in relationships if node in x['require']]
  8. }
  9. def _traverse_subtree(output, data):
  10. for key, value in data.items():
  11. output.append(key)
  12. _traverse_subtree(output, value)
  13. return output
  14. def order_by_requisites(data):
  15. '''
  16. Returns dictionary ordered by require and require_by
  17. CLI Examples:
  18. .. code-block:: bash
  19. salt-call modelutils.order_by_requisites "{'dict':'value'}""
  20. Sample data
  21. passed_data:
  22. syslog2:
  23. pattern: 'syslog.*'
  24. syslog_tele1:
  25. type: parser
  26. require:
  27. - syslog1
  28. syslog1:
  29. pattern: 'syslog.*'
  30. require_in:
  31. - syslog2
  32. syslog_tele2:
  33. require:
  34. - syslog_tele1
  35. '''
  36. raw_key_list = []
  37. ordered_key_list = []
  38. output_dict = OrderedDict()
  39. for datum_id, datum in data.items():
  40. if 'require_in' in datum:
  41. for req in datum['require_in']:
  42. if 'require' not in data[req]:
  43. data[req]['require'] = []
  44. data[req]['require'].append(datum_id)
  45. datum.pop('require_in')
  46. for datum_id, datum in data.items():
  47. if 'require' not in datum:
  48. datum['require'] = ['top']
  49. datum['id'] = datum_id
  50. raw_key_list.append(datum)
  51. tree_data = _set_subtree('top', raw_key_list)
  52. _traverse_subtree(ordered_key_list, tree_data)
  53. for key in ordered_key_list:
  54. output_dict[key] = data[key]
  55. return output_dict