Esempio n. 1
0
def serialize_nova_server(server):
    nc_client = openstack_clients.get_novaclient()
    if isinstance(server, six.text_type):
        server = nc_client.servers.get(server)

    LOG.debug("Serializing server %s for project %s",
              server.id, server.tenant_id)
    serialized = {k: v for k, v in server.to_dict().items()
                  if k not in BLACKLISTED_FIELDS}

    # Some enhancements
    serialized[u'owner'] = server.tenant_id
    serialized[u'project_id'] = server.tenant_id
    # Image is empty when the instance is booted from volume
    if isinstance(serialized[u'image'], dict):
        serialized[u'image'].pop(u'links', None)
    else:
        serialized.pop(u'image')
    serialized[u'flavor'].pop(u'links', None)

    sec_groups = serialized.pop(u'security_groups', [])
    serialized['security_groups'] = [s[u'name'] for s in sec_groups]

    _format_networks(server, serialized)

    utils.normalize_date_fields(serialized)

    serialized['status'] = serialized['status'].lower()

    # Pop the fault stracktrace if any - it's big
    fault = serialized.get('fault', None)
    if fault and isinstance(fault, dict):
        fault.pop('details', None)

    return serialized
Esempio n. 2
0
def serialize_nova_server(server):
    nc_client = openstack_clients.get_novaclient()
    if isinstance(server, six.text_type):
        server = nc_client.servers.get(server)

    LOG.debug("Serializing server %s for project %s",
              server.id, server.tenant_id)

    serialized = {k: v for k, v in six.iteritems(server.to_dict())
                  if k not in BLACKLISTED_FIELDS}

    # Some enhancements
    serialized[u'owner'] = server.tenant_id
    serialized[u'image'].pop(u'links', None)
    serialized[u'flavor'].pop(u'links', None)

    _format_networks(server, serialized)

    utils.normalize_date_fields(serialized)

    return serialized
Esempio n. 3
0
def serialize_glance_metadef_ns(metadef_namespace):
    def _serialize_tag(tag):
        return {'name': tag['name']}

    def _serialize_property(name, property):
        serialized_prop = copy.deepcopy(property)
        serialized_prop['name'] = name
        if 'default' in serialized_prop:
            serialized_prop['default'] = str(serialized_prop['default'])
        if 'enum' in serialized_prop:
            serialized_prop['enum'] = sorted(map(str, serialized_prop['enum']))

        return serialized_prop

    def _serialize_object(obj):
        serialized_obj = {
            'name': obj['name'],
            'description': obj['description']
        }
        serialized_obj['properties'] = sorted([
            _serialize_property(name, property)
            for name, property in six.iteritems(obj.get('properties', {}))
        ], key=operator.itemgetter('name'))
        return serialized_obj

    def _serialize_res_type(rt):
        return {
            'name': rt['name']
        }

    # TODO(sjmc7): test this better
    incomplete = 'objects' not in metadef_namespace or \
                 'properties' not in metadef_namespace or \
                 'tags' not in metadef_namespace
    if incomplete:
        LOG.debug("Retrieving metadef namespace '%s'",
                  metadef_namespace['namespace'])
        g_client = openstack_clients.get_glanceclient()
        metadef_namespace = g_client.metadefs_namespace.get(
            metadef_namespace['namespace'])

    # The CIS code specifically serialized some fields rather than indexing
    # everything; do the same.
    namespace_fields = ('namespace', 'display_name', 'description',
                        'visibility', 'owner', 'protected', 'created_at')
    document = {f: metadef_namespace.get(f, None) for f in namespace_fields}
    document['id'] = document['namespace']
    if document['display_name']:
        document['name'] = document['display_name']
    else:
        document['name'] = document['namespace']

    document['tags'] = sorted([
        _serialize_tag(tag) for tag in metadef_namespace.get('tags', [])
    ], key=operator.itemgetter('name'))
    document['properties'] = sorted([
        _serialize_property(name, property)
        for name, property in six.iteritems(
            metadef_namespace.get('properties', {}))
    ], key=operator.itemgetter('name'))
    document['objects'] = sorted([
        _serialize_object(obj) for obj in metadef_namespace.get('objects', [])
    ], key=operator.itemgetter('name'))
    document['resource_types'] = sorted([
        _serialize_res_type(rt)
        for rt in metadef_namespace.get('resource_type_associations', [])
    ], key=operator.itemgetter('name'))

    utils.normalize_date_fields(document, created_at=None,
                                updated_at='created_at')

    return document