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 sync_tenants_from_keystone(context):
    tenants = get_all_tenants()
    for tenant in tenants:
        LOG.info("Tenants obtained from keystone: %s", tenant)
        # tenants from keystone have 'id' and 'name' comparing to
        # db cache where 'tenant_id' and 'tenant_name' are used
        dbi.add_or_update_tenant(context.session, tenant.id, tenant.name)
    return len(tenants)
    def test_add_or_update_tenant(self):
        tenants = {'tenant-id1': 'tenant-name1'}
        self._create_tenants(tenants)
        new_tenant_name = 'tenant-name-updated'
        infoblox_db.add_or_update_tenant(self.ctx.session,
                                         'tenant-id1', new_tenant_name)
        tenant = infoblox_db.get_tenant(self.ctx.session, 'tenant-id1')
        self.assertEqual(new_tenant_name, tenant.tenant_name)

        infoblox_db.add_or_update_tenant(self.ctx.session,
                                         'tenant-id2', 'tenant-name2')
        tenant = infoblox_db.get_tenant(self.ctx.session, 'tenant-id2')
        self.assertEqual('tenant-name2', tenant.tenant_name)
예제 #4
0
    def test_add_or_update_tenant(self):
        tenants = {'tenant-id1': 'tenant-name1'}
        self._create_tenants(tenants)
        new_tenant_name = 'tenant-name-updated'
        infoblox_db.add_or_update_tenant(self.ctx.session, 'tenant-id1',
                                         new_tenant_name)
        tenant = infoblox_db.get_tenant(self.ctx.session, 'tenant-id1')
        self.assertEqual(new_tenant_name, tenant.tenant_name)

        infoblox_db.add_or_update_tenant(self.ctx.session, 'tenant-id2',
                                         'tenant-name2')
        tenant = infoblox_db.get_tenant(self.ctx.session, 'tenant-id2')
        self.assertEqual('tenant-name2', tenant.tenant_name)