#NEWLINE# def filter_and_format_resource_metadata( resource_type , resource_list , search_filts , metadata_type = None ) : #NEWLINE# #INDENT# "Get all metadata for a list of resources after filtering.\n\n    Search_filts is a list of dictionaries, where the values in the dictionary\n    can be string or regex string, or a list of strings/regex strings.\n\n    Let's call a dict a 'filter block' and an item in the dict\n    a 'filter'. A tag is returned if it matches ALL the filters in\n    a filter block. If more than one values are specified for a\n    filter, a tag is returned if it matches ATLEAST ONE value of the filter. If\n    more than one filter blocks are specified, the tag should match ALL the\n    filter blocks.\n\n    For example:\n\n        search_filts = [{'key': ['key1', 'key2'], 'value': 'val1'},\n                        {'value': 'val2'}]\n\n    The filter translates to 'match any tag for which':\n        ((key=key1 AND value=val1) OR (key=key2 AND value=val1)) AND\n            (value=val2)\n\n    This example filter will never match a tag.\n\n        :param resource_type: The resource type as a string, e.g. 'instance'\n        :param resource_list: List of resource objects\n        :param search_filts: Filters to filter metadata to be returned. Can be\n            dict (e.g. {'key': 'env', 'value': 'prod'}, or a list of dicts\n            (e.g. [{'key': 'env'}, {'value': 'beta'}]. Note that the values\n            of the dict can be regular expressions.\n        :param metadata_type: Provided to search for a specific metadata type\n            (e.g. 'system_metadata')\n\n        :returns: List of dicts where each dict is of the form {'key':\n            'somekey', 'value': 'somevalue', 'instance_id':\n            'some-instance-uuid-aaa'} if resource_type is 'instance'.\n    " #NEWLINE# if isinstance ( search_filts , dict ) : #NEWLINE# #INDENT# search_filts = [ search_filts ] #NEWLINE# #UNINDENT# def _get_id( resource ) : #NEWLINE# #INDENT# if ( resource_type == 'instance' ) : #NEWLINE# #INDENT# return resource . get ( 'uuid' ) #NEWLINE# #UNINDENT# #UNINDENT# def _match_any( pattern_list , string ) : #NEWLINE# #INDENT# if isinstance ( pattern_list , str ) : #NEWLINE# #INDENT# pattern_list = [ pattern_list ] #NEWLINE# #UNINDENT# return any ( [ re . match ( pattern , string ) for pattern in pattern_list ] ) #NEWLINE# #UNINDENT# def _filter_metadata( resource , search_filt , input_metadata ) : #NEWLINE# #INDENT# ids = search_filt . get ( 'resource_id' , [ ] ) #NEWLINE# keys_filter = search_filt . get ( 'key' , [ ] ) #NEWLINE# values_filter = search_filt . get ( 'value' , [ ] ) #NEWLINE# output_metadata = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# if ( ids and ( _get_id ( resource ) not in ids ) ) : #NEWLINE# #INDENT# return { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# for ( k , v ) in six . iteritems ( input_metadata ) : #NEWLINE# #INDENT# if ( keys_filter and values_filter and ( not _match_any ( keys_filter , k ) ) and ( not _match_any ( values_filter , v ) ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( keys_filter and ( not _match_any ( keys_filter , k ) ) ) or ( values_filter and ( not _match_any ( values_filter , v ) ) ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# #UNINDENT# output_metadata [ k ] = v #NEWLINE# #UNINDENT# return output_metadata #NEWLINE# #UNINDENT# formatted_metadata_list = [ ] #NEWLINE# for res in resource_list : #NEWLINE# #INDENT# if ( resource_type == 'instance' ) : #NEWLINE# #INDENT# if ( metadata_type is None ) : #NEWLINE# #INDENT# metadata_type = 'metadata' #NEWLINE# #UNINDENT# metadata = res . get ( metadata_type , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# for filt in search_filts : #NEWLINE# #INDENT# metadata = _filter_metadata ( res , filt , metadata ) #NEWLINE# #UNINDENT# for ( k , v ) in metadata . items ( ) : #NEWLINE# #INDENT# formatted_metadata_list . append ( { #NEWLINE# #INDENT# 'key' : k , #NEWLINE# 'value' : v , #NEWLINE# ( '%s_id' % resource_type ) : _get_id ( res ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# #UNINDENT# return formatted_metadata_list