Merge pull request #2625 from abedwardsw/feature/vmware_groupby_custom_field_excludes

update to latest vmware_inventory.py support for groupby_custom_field_excludes

Reviewed-by: https://github.com/softwarefactory-project-zuul[bot]
This commit is contained in:
softwarefactory-project-zuul[bot]
2018-11-07 21:03:05 +00:00
committed by GitHub

View File

@@ -9,7 +9,7 @@
# TODO: # TODO:
# * more jq examples # * more jq examples
# * optional folder heriarchy # * optional folder hierarchy
""" """
$ jq '._meta.hostvars[].config' data.json | head $ jq '._meta.hostvars[].config' data.json | head
@@ -38,9 +38,8 @@ import sys
import uuid import uuid
from time import time from time import time
import six
from jinja2 import Environment from jinja2 import Environment
from six import integer_types, string_types from six import integer_types, PY3
from six.moves import configparser from six.moves import configparser
try: try:
@@ -99,6 +98,7 @@ class VMWareInventory(object):
host_filters = [] host_filters = []
skip_keys = [] skip_keys = []
groupby_patterns = [] groupby_patterns = []
groupby_custom_field_excludes = []
safe_types = [bool, str, float, None] + list(integer_types) safe_types = [bool, str, float, None] + list(integer_types)
iter_types = [dict, list] iter_types = [dict, list]
@@ -230,10 +230,11 @@ class VMWareInventory(object):
'groupby_patterns': '{{ guest.guestid }},{{ "templates" if config.template else "guests"}}', 'groupby_patterns': '{{ guest.guestid }},{{ "templates" if config.template else "guests"}}',
'lower_var_keys': True, 'lower_var_keys': True,
'custom_field_group_prefix': 'vmware_tag_', 'custom_field_group_prefix': 'vmware_tag_',
'groupby_custom_field_excludes': '',
'groupby_custom_field': False} 'groupby_custom_field': False}
} }
if six.PY3: if PY3:
config = configparser.ConfigParser() config = configparser.ConfigParser()
else: else:
config = configparser.SafeConfigParser() config = configparser.SafeConfigParser()
@@ -304,8 +305,12 @@ class VMWareInventory(object):
groupby_pattern += "}}" groupby_pattern += "}}"
self.groupby_patterns.append(groupby_pattern) self.groupby_patterns.append(groupby_pattern)
self.debugl('groupby patterns are %s' % self.groupby_patterns) self.debugl('groupby patterns are %s' % self.groupby_patterns)
temp_groupby_custom_field_excludes = config.get('vmware', 'groupby_custom_field_excludes')
self.groupby_custom_field_excludes = [x.strip('"') for x in [y.strip("'") for y in temp_groupby_custom_field_excludes.split(",")]]
self.debugl('groupby exclude strings are %s' % self.groupby_custom_field_excludes)
# Special feature to disable the brute force serialization of the # Special feature to disable the brute force serialization of the
# virtulmachine objects. The key name for these properties does not # virtual machine objects. The key name for these properties does not
# matter because the values are just items for a larger list. # matter because the values are just items for a larger list.
if config.has_section('properties'): if config.has_section('properties'):
self.guest_props = [] self.guest_props = []
@@ -397,7 +402,7 @@ class VMWareInventory(object):
cfm = content.customFieldsManager cfm = content.customFieldsManager
if cfm is not None and cfm.field: if cfm is not None and cfm.field:
for f in cfm.field: for f in cfm.field:
if f.managedObjectType == vim.VirtualMachine: if not f.managedObjectType or f.managedObjectType == vim.VirtualMachine:
self.custom_fields[f.key] = f.name self.custom_fields[f.key] = f.name
self.debugl('%d custom fields collected' % len(self.custom_fields)) self.debugl('%d custom fields collected' % len(self.custom_fields))
except vmodl.RuntimeFault as exc: except vmodl.RuntimeFault as exc:
@@ -494,16 +499,15 @@ class VMWareInventory(object):
for k, v in inventory['_meta']['hostvars'].items(): for k, v in inventory['_meta']['hostvars'].items():
if 'customvalue' in v: if 'customvalue' in v:
for tv in v['customvalue']: for tv in v['customvalue']:
if not isinstance(tv['value'], string_types):
continue
newkey = None newkey = None
field_name = self.custom_fields[tv['key']] if tv['key'] in self.custom_fields else tv['key'] field_name = self.custom_fields[tv['key']] if tv['key'] in self.custom_fields else tv['key']
if field_name in self.groupby_custom_field_excludes:
continue
values = [] values = []
keylist = map(lambda x: x.strip(), tv['value'].split(',')) keylist = map(lambda x: x.strip(), tv['value'].split(','))
for kl in keylist: for kl in keylist:
try: try:
newkey = self.config.get('vmware', 'custom_field_group_prefix') + str(field_name) + '_' + kl newkey = "%s%s_%s" % (self.config.get('vmware', 'custom_field_group_prefix'), str(field_name), kl)
newkey = newkey.strip() newkey = newkey.strip()
except Exception as e: except Exception as e:
self.debugl(e) self.debugl(e)
@@ -521,7 +525,6 @@ class VMWareInventory(object):
def create_template_mapping(self, inventory, pattern, dtype='string'): def create_template_mapping(self, inventory, pattern, dtype='string'):
''' Return a hash of uuid to templated string from pattern ''' ''' Return a hash of uuid to templated string from pattern '''
mapping = {} mapping = {}
for k, v in inventory['_meta']['hostvars'].items(): for k, v in inventory['_meta']['hostvars'].items():
t = self.env.from_string(pattern) t = self.env.from_string(pattern)
@@ -557,7 +560,15 @@ class VMWareInventory(object):
if '.' not in prop: if '.' not in prop:
# props without periods are direct attributes of the parent # props without periods are direct attributes of the parent
rdata[key] = getattr(vm, prop) vm_property = getattr(vm, prop)
if isinstance(vm_property, vim.CustomFieldsManager.Value.Array):
temp_vm_property = []
for vm_prop in vm_property:
temp_vm_property.append({'key': vm_prop.key,
'value': vm_prop.value})
rdata[key] = temp_vm_property
else:
rdata[key] = vm_property
else: else:
# props with periods are subkeys of parent attributes # props with periods are subkeys of parent attributes
parts = prop.split('.') parts = prop.split('.')