def update_tenant_mapping(context, networks, tenant_id, tenant_name):
    """Updates tenant_id to tenant_name mapping information.

    Tries to get tenant name to tenant id mapping from context info.
    If context tenant id is different from network tenant_id,
    then check if id to name mapping is already known (check db).
    If id to name mapping still unknown run API call to keystone to
    get all known tenants and store this mapping.
    """

    dbi.add_or_update_tenant(context.session, tenant_id, tenant_name)

    # Get unique tenants ids and check if there are unknown one
    tenant_ids = {net['tenant_id']: True for net in networks}
    if tenant_id in tenant_ids:
        tenant_ids[tenant_id] = False
    unknown_ids = _get_unknown_ids_from_dict(tenant_ids)

    # There are some unknown ids, check if there are mapping in database
    if unknown_ids:
        db_tenants = dbi.get_tenants(context.session, tenant_ids=unknown_ids)
        for tenant in db_tenants:
            tenant_ids[tenant.tenant_id] = False
        if _get_unknown_ids_from_dict(tenant_ids):
            sync_tenants_from_keystone(context)
 def test_get_tenants(self):
     tenants = {'tenant-one': 'tenant-name-1',
                'tenant-two': 'tenant-name-2',
                'tenant-three': 'tenant-name-3'}
     self._create_tenants(tenants)
     tenants = infoblox_db.get_tenants(self.ctx.session,
                                       ['tenant-one', 'tenant-three'])
     self.assertEqual(2, len(tenants))
 def test_get_tenants(self):
     tenants = {'tenant-one': 'tenant-name-1',
                'tenant-two': 'tenant-name-2',
                'tenant-three': 'tenant-name-3'}
     self._create_tenants(tenants)
     tenants = infoblox_db.get_tenants(self.ctx.session,
                                       ['tenant-one', 'tenant-three'])
     self.assertEqual(2, len(tenants))