'Convert various data formats into the metadata format expected by
Google Compute Engine and suitable for passing along to the API. Can
accept the following formats:
(a) [{\'key\': \'k1\', \'value\': \'v1\'}, ...]
(b) [{\'k1\': \'v1\'}, ...]
(c) {\'key\': \'k1\', \'value\': \'v1\'}
(d) {\'k1\': \'v1\', \'k2\': v2\', ...}
(e) {\'items\': [...]}       # does not check for valid list contents
The return value is a \'dict\' that GCE expects, e.g.
{\'fingerprint\': \'xx...\',
\'items\': [{\'key\': \'key1\', \'value\': \'val1\'},
{\'key\': \'key2\', \'value\': \'val2\'},
:param  fingerprint: Current metadata fingerprint
:type   fingerprint: ``str``
:param  metadata: Variety of input formats.
:type   metadata: ``list``, ``dict``, or ``None``
:return: GCE-friendly metadata dict
:rtype:  ``dict``'

def _format_metadata(self, fingerprint, metadata=None):

    if (not metadata):
        return {'fingerprint': fingerprint, 'items': []}
    md = {'fingerprint': fingerprint}
    if isinstance(metadata, list):
        item_list = []
        for i in metadata:
            if isinstance(i, dict):
                if (('key' in i) and ('value' in i) and (len(i) == 2)):
                    item_list.append(i)
                elif (len(i) == 1):
                    item_list.append({'key': list(i.keys())[0], 'value': list(i.values())[0]})
                else:
                    raise ValueError('Unsupported    metadata    format.')
            else:
                raise ValueError('Unsupported    metadata    format.')
        md['items'] = item_list
    if isinstance(metadata, dict):
        if (('key' in metadata) and ('value' in metadata) and (len(metadata) == 2)):
            md['items'] = [metadata]
        elif (len(metadata) == 1):
            if ('items' in metadata):
                if isinstance(metadata['items'], list):
                    md['items'] = metadata['items']
                else:
                    raise ValueError('Unsupported    metadata    format.')
            else:
                md['items'] = [{'key': list(metadata.keys())[0], 'value': list(metadata.values())[0]}]
        else:
            md['items'] = []
            for (k, v) in metadata.items():
                md['items'].append({'key': k, 'value': v})
    if ('items' not in md):
        raise ValueError('Unsupported    metadata    format.')
    return md
