Theo Chatzimichos 02fadf
#!/usr/bin/python3
Theo Chatzimichos 02fadf
Theo Chatzimichos 02fadf
# Validates if the pillar/id/$FQDN.sls has correct custom grains
Theo Chatzimichos 02fadf
Theo Chatzimichos 02fadf
import yaml
Theo Chatzimichos 02fadf
import os
Theo Chatzimichos 02fadf
import sys
Theo Chatzimichos 02fadf
Christian Boltz 25cb76
from get_roles import read_file_skip_jinja
Theo Chatzimichos 7cceb8
from get_valid_custom_grains import get_valid_global_grains, get_all_valid_localized_grains
Theo Chatzimichos 7cceb8
Theo Chatzimichos 02fadf
Theo Chatzimichos 02fadf
def error_msg(sls, key, valid_values):
Theo Chatzimichos 02fadf
    if type(valid_values) == str:
Theo Chatzimichos 02fadf
        msg = ' is'
Theo Chatzimichos 02fadf
        result = valid_values
Theo Chatzimichos 02fadf
    else:
Theo Chatzimichos 02fadf
        msg = 's are'
Theo Chatzimichos 02fadf
        result = ', '.join(valid_values)
Theo Chatzimichos 02fadf
    print('pillar/id/%s has invalid value for the "%s" key. Valid value%s: %s' % (sls, key, msg, result))
Theo Chatzimichos 02fadf
    return 1
Theo Chatzimichos 02fadf
Theo Chatzimichos 02fadf
Theo Chatzimichos 02fadf
def test_custom_grain(mygrains, sls, key, valid_values, status):
Theo Chatzimichos 02fadf
    try:
Theo Chatzimichos 02fadf
        value = mygrains[key]
Theo Chatzimichos 02fadf
    except KeyError:
Theo Chatzimichos 02fadf
        print('pillar/id/%s is missing the "%s" key' % (sls, key))
Theo Chatzimichos 02fadf
        return 1
Theo Chatzimichos 02fadf
Theo Chatzimichos 02fadf
    if type(valid_values) == str:
Theo Chatzimichos 02fadf
        if value != valid_values:
Theo Chatzimichos 02fadf
            status = error_msg(sls, key, valid_values)
Theo Chatzimichos 02fadf
    else:
Theo Chatzimichos df416d
        if valid_values and value not in valid_values:
Christian Boltz e1b0b7
            status = error_msg(sls, key, map(str, valid_values))
Theo Chatzimichos 02fadf
Theo Chatzimichos 02fadf
    return status
Theo Chatzimichos 02fadf
Theo Chatzimichos 02fadf
Theo Chatzimichos 02fadf
status = 0
Theo Chatzimichos 02fadf
Theo Chatzimichos 7cceb8
valid_global_grains = get_valid_global_grains()
Theo Chatzimichos 7cceb8
all_valid_localized_grains = get_all_valid_localized_grains()
Theo Chatzimichos 02fadf
Theo Chatzimichos 02fadf
all_ids = sorted(os.listdir('pillar/id'))
Theo Chatzimichos 02fadf
for sls in all_ids:
Christian Boltz 25cb76
    content = read_file_skip_jinja("pillar/id/%s" % sls)
Christian Boltz 25cb76
    mygrains = yaml.load(content)['grains']
Theo Chatzimichos 02fadf
Christian Boltz 25cb76
    for key, valid_values in valid_global_grains.items():
Christian Boltz 25cb76
        status = test_custom_grain(mygrains, sls, key, valid_values, status)
Theo Chatzimichos 02fadf
Christian Boltz 25cb76
    try:
Theo Chatzimichos 7cceb8
        valid_localized_grains = all_valid_localized_grains[mygrains['country']]
Theo Chatzimichos 12b8f9
        for ignored_key in ['domains', 'default_domain', 'default_virt_cluster']:
Theo Chatzimichos 12b8f9
            valid_localized_grains.pop(ignored_key, None)
Christian Boltz 25cb76
        for key, valid_values in valid_localized_grains.items():
Christian Boltz 25cb76
            status = test_custom_grain(mygrains, sls, key, valid_values, status)
Christian Boltz 25cb76
    except KeyError:
Theo Chatzimichos 7cceb8
        status = error_msg(sls, 'country', all_valid_localized_grains.keys())
Theo Chatzimichos 02fadf
Theo Chatzimichos 02fadf
sys.exit(status)