Exemplo n.º 1
0
def find_tenant(ds_handler, tenant_id, create_on_missing=False):
    """
    Retrieves a dictionary describing a tenant object and its Hosts, Profiles,
    and eventProducers and maps them to a tenant object
    """
    # get the tenant dictionary form the data source
    tenant_dict = ds_handler.find_one('tenant', {'tenant_id': tenant_id})

    if tenant_dict:
        tenant = load_tenant_from_dict(tenant_dict)
        return tenant

    if create_on_missing:
        #create new token for the tenant
        new_token = Token()
        new_tenant = Tenant(tenant_id, new_token)

        ds_handler.put('tenant', new_tenant.format())
        ds_handler.create_sequence(new_tenant.tenant_id)

        tenant_dict = ds_handler.find_one('tenant', {'tenant_id': tenant_id})

        tenant = load_tenant_from_dict(tenant_dict)
        return tenant

    return None
Exemplo n.º 2
0
class WhenTestingTenantObject(unittest.TestCase):
    def setUp(self):
        self.test_token = Token('89c38542-0c78-41f1-bcd2-5226189ccab9',
                                '89c38542-0c78-41f1-bcd2-5226189ddab1',
                                '2013-04-01T21:58:16.995031Z')
        self.test_tenant_bare = Tenant('1022', self.test_token, [], [], [])
        self.test_tenant = Tenant('1022', self.test_token, [], [], [], 'MDBid')

    def test_tenant_get_id(self):
        self.assertEqual(self.test_tenant.get_id(), 'MDBid')

    def test_tenant_format(self):
        tenant_dict = self.test_tenant_bare.format()
        self.assertEqual(tenant_dict['tenant_id'], '1022')
        self.assertEqual(tenant_dict['hosts'], [])
        self.assertEqual(tenant_dict['profiles'], [])
        self.assertEqual(tenant_dict['event_producers'], [])
        self.assertTrue('token' in tenant_dict)

    def test_tenant_format_for_save(self):
        tenant_dict = self.test_tenant.format_for_save()
        self.assertEqual(tenant_dict['tenant_id'], '1022')
        self.assertEqual(tenant_dict['hosts'], [])
        self.assertEqual(tenant_dict['profiles'], [])
        self.assertEqual(tenant_dict['event_producers'], [])
        self.assertEqual(tenant_dict['_id'], 'MDBid')
Exemplo n.º 3
0
def find_tenant(ds_handler, tenant_id, create_on_missing=False):
    """
    Retrieves a dictionary describing a tenant object and its Hosts, Profiles,
    and eventProducers and maps them to a tenant object
    """
    # get the tenant dictionary form the data source
    tenant_dict = ds_handler.find_one('tenant', {'tenant_id': tenant_id})

    if tenant_dict:
        tenant = load_tenant_from_dict(tenant_dict)
        return tenant

    if create_on_missing:
        #create new token for the tenant
        new_token = Token()
        new_tenant = Tenant(tenant_id, new_token)

        ds_handler.put('tenant', new_tenant.format())
        ds_handler.create_sequence(new_tenant.tenant_id)

        tenant_dict = ds_handler.find_one('tenant', {'tenant_id': tenant_id})

        tenant = load_tenant_from_dict(tenant_dict)
        return tenant

    return None
Exemplo n.º 4
0
def create_tenant(tenant_id, tenant_name=None):
    """
    Creates a new tenant and and persists to the datastore
    """
    #create new token for the tenant
    new_token = Token()
    new_tenant = Tenant(tenant_id, new_token, tenant_name=tenant_name)

    #save the new tenant to the datastore
    _db_handler.put('tenant', new_tenant.format())
    #create a new sequence for the tenant for creation of IDs on child objects
    _db_handler.create_sequence(new_tenant.tenant_id)

    #create an index for the tenant in the default sink
    # and enables time to live for the default doc_type
    ttl_tasks.create_index.delay(tenant_id)
Exemplo n.º 5
0
def create_tenant(tenant_id, tenant_name=None):
    """
    Creates a new tenant and and persists to the datastore
    """
    #create new token for the tenant
    new_token = Token()
    new_tenant = Tenant(tenant_id, new_token, tenant_name=tenant_name)

    #save the new tenant to the datastore
    _db_handler.put('tenant', new_tenant.format())
    #create a new sequence for the tenant for creation of IDs on child objects
    _db_handler.create_sequence(new_tenant.tenant_id)

    #create an index for the tenant in the default sink
    # and enables time to live for the default doc_type
    mapping_tasks.create_index.delay(tenant_id)
Exemplo n.º 6
0
    def on_post(self, req, resp, validated_body):

        body = validated_body['tenant']
        tenant_id = str(body['tenant_id'])

        tenant_name = body.get('tenant_name', tenant_id)

        #validate that tenant does not already exists
        tenant = find_tenant(self.db, tenant_id=tenant_id)
        if tenant:
            abort(falcon.HTTP_400, 'Tenant with tenant_id {0} '
                  'already exists'.format(tenant_id))

        #create new token for the tenant
        new_token = Token()
        new_tenant = Tenant(tenant_id, new_token, tenant_name=tenant_name)

        self.db.put('tenant', new_tenant.format())
        self.db.create_sequence(new_tenant.tenant_id)
        resp.status = falcon.HTTP_201
        resp.set_header('Location', '/v1/{0}'.format(tenant_id))
Exemplo n.º 7
0
    def on_post(self, req, resp, validated_body):

        body = validated_body['tenant']
        tenant_id = str(body['tenant_id'])

        tenant_name = body.get('tenant_name', tenant_id)

        #validate that tenant does not already exists
        tenant = find_tenant(self.db, tenant_id=tenant_id)
        if tenant:
            abort(
                falcon.HTTP_400, 'Tenant with tenant_id {0} '
                'already exists'.format(tenant_id))

        #create new token for the tenant
        new_token = Token()
        new_tenant = Tenant(tenant_id, new_token, tenant_name=tenant_name)

        self.db.put('tenant', new_tenant.format())
        self.db.create_sequence(new_tenant.tenant_id)
        resp.status = falcon.HTTP_201
        resp.set_header('Location', '/v1/{0}'.format(tenant_id))
Exemplo n.º 8
0
class WhenTestingTenantObject(unittest.TestCase):
    def setUp(self):
        self.test_token = Token('89c38542-0c78-41f1-bcd2-5226189ccab9',
                                '89c38542-0c78-41f1-bcd2-5226189ddab1',
                                '2013-04-01T21:58:16.995031Z')
        self.test_tenant_bare = Tenant('1022', self.test_token)
        self.test_tenant = Tenant('1022', self.test_token, [], 'MDBid',
                                  'TenantName')

    def test_tenant_get_id(self):
        self.assertEqual(self.test_tenant.get_id(), 'MDBid')

    def test_tenant_format(self):
        tenant_dict = self.test_tenant_bare.format()
        self.assertEqual(tenant_dict['tenant_id'], '1022')
        self.assertEqual(tenant_dict['event_producers'], [])
        self.assertTrue('token' in tenant_dict)

    def test_tenant_format_for_save(self):
        tenant_dict = self.test_tenant.format_for_save()
        self.assertEqual(tenant_dict['tenant_id'], '1022')
        self.assertEqual(tenant_dict['tenant_name'], 'TenantName')
        self.assertEqual(tenant_dict['event_producers'], [])
        self.assertEqual(tenant_dict['_id'], 'MDBid')
class WhenTestingTenantCache(unittest.TestCase):
    def setUp(self):
        self.cache_clear = MagicMock()
        self.cache_true = MagicMock(return_value=True)
        self.cache_false = MagicMock(return_value=False)
        self.cache_update = MagicMock()
        self.cache_set = MagicMock()
        self.cache_del = MagicMock()
        self.tenant_id = '101'
        self.tenant = Tenant(
            tenant_id=self.tenant_id,
            token=Token()
        )
        self.tenant_json = jsonutils.dumps(self.tenant.format())
        self.cache_get_tenant = MagicMock(return_value=self.tenant_json)

    def test_clear_calls_cache_clear(self):
        with patch.object(NativeProxy, 'cache_clear', self.cache_clear):
            tenant_cache = TenantCache()
            tenant_cache.clear()
        self.cache_clear.assert_called_once_with(CACHE_TENANT)

    def test_set_tenant_calls_cache_update(self):
        with patch.object(
                NativeProxy, 'cache_exists', self.cache_true
        ), patch.object(NativeProxy, 'cache_update', self.cache_update):
            tenant_cache = TenantCache()
            tenant_cache.set_tenant(self.tenant)

        self.cache_update.assert_called_once_with(
            self.tenant_id, jsonutils.dumps(self.tenant.format()),
            DEFAULT_EXPIRES, CACHE_TENANT)

    def test_set_tenant_calls_cache_set(self):
        with patch.object(
                NativeProxy, 'cache_exists', self.cache_false
        ), patch.object(NativeProxy, 'cache_set', self.cache_set):
            tenant_cache = TenantCache()
            tenant_cache.set_tenant(self.tenant)

        self.cache_set.assert_called_once_with(
            self.tenant_id, jsonutils.dumps(self.tenant.format()),
            DEFAULT_EXPIRES, CACHE_TENANT)

    def test_get_tenant_calls_returns_tenant(self):
        with patch.object(
                NativeProxy, 'cache_exists', self.cache_true
        ), patch.object(NativeProxy, 'cache_get',  self.cache_get_tenant):
            tenant_cache = TenantCache()
            tenant = tenant_cache.get_tenant(self.tenant_id)

        self.cache_get_tenant.assert_called_once_with(
            self.tenant_id, CACHE_TENANT)
        self.assertIsInstance(tenant, Tenant)

    def test_get_tenant_calls_returns_none(self):
        with patch.object(
                NativeProxy, 'cache_exists', self.cache_false):
            tenant_cache = TenantCache()
            tenant = tenant_cache.get_tenant(self.tenant_id)

        self.assertIs(tenant, None)

    def test_delete_tenant_calls_cache_del(self):
        with patch.object(
                NativeProxy, 'cache_exists', self.cache_true
        ), patch.object(NativeProxy, 'cache_del', self.cache_del):
            tenant_cache = TenantCache()
            tenant_cache.delete_tenant(self.tenant_id)

        self.cache_del.assert_called_once_with(
            self.tenant_id, CACHE_TENANT)

    def test_delete_tenant_does_not_call_cache_del(self):
        with patch.object(
                NativeProxy, 'cache_exists', self.cache_false
        ), patch.object(NativeProxy, 'cache_del', self.cache_del):
            tenant_cache = TenantCache()
            tenant_cache.delete_tenant(self.tenant_id)

        with self.assertRaises(AssertionError):
            self.cache_del.assert_called_once_with(
                self.tenant_id, CACHE_TENANT)
Exemplo n.º 10
0
class WhenTestingTenantCache(unittest.TestCase):
    def setUp(self):
        self.cache_clear = MagicMock()
        self.cache_true = MagicMock(return_value=True)
        self.cache_false = MagicMock(return_value=False)
        self.cache_update = MagicMock()
        self.cache_set = MagicMock()
        self.cache_del = MagicMock()
        self.tenant_id = '101'
        self.tenant = Tenant(tenant_id=self.tenant_id, token=Token())
        self.tenant_json = jsonutils.dumps(self.tenant.format())
        self.cache_get_tenant = MagicMock(return_value=self.tenant_json)

    def test_clear_calls_cache_clear(self):
        with patch.object(NativeProxy, 'cache_clear', self.cache_clear):
            tenant_cache = TenantCache()
            tenant_cache.clear()
        self.cache_clear.assert_called_once_with(CACHE_TENANT)

    def test_set_tenant_calls_cache_update(self):
        with patch.object(NativeProxy, 'cache_exists',
                          self.cache_true), patch.object(
                              NativeProxy, 'cache_update', self.cache_update):
            tenant_cache = TenantCache()
            tenant_cache.set_tenant(self.tenant)

        self.cache_update.assert_called_once_with(
            self.tenant_id, jsonutils.dumps(self.tenant.format()),
            DEFAULT_EXPIRES, CACHE_TENANT)

    def test_set_tenant_calls_cache_set(self):
        with patch.object(NativeProxy, 'cache_exists',
                          self.cache_false), patch.object(
                              NativeProxy, 'cache_set', self.cache_set):
            tenant_cache = TenantCache()
            tenant_cache.set_tenant(self.tenant)

        self.cache_set.assert_called_once_with(
            self.tenant_id, jsonutils.dumps(self.tenant.format()),
            DEFAULT_EXPIRES, CACHE_TENANT)

    def test_get_tenant_calls_returns_tenant(self):
        with patch.object(NativeProxy, 'cache_exists',
                          self.cache_true), patch.object(
                              NativeProxy, 'cache_get', self.cache_get_tenant):
            tenant_cache = TenantCache()
            tenant = tenant_cache.get_tenant(self.tenant_id)

        self.cache_get_tenant.assert_called_once_with(self.tenant_id,
                                                      CACHE_TENANT)
        self.assertIsInstance(tenant, Tenant)

    def test_get_tenant_calls_returns_none(self):
        with patch.object(NativeProxy, 'cache_exists', self.cache_false):
            tenant_cache = TenantCache()
            tenant = tenant_cache.get_tenant(self.tenant_id)

        self.assertIs(tenant, None)

    def test_delete_tenant_calls_cache_del(self):
        with patch.object(NativeProxy, 'cache_exists',
                          self.cache_true), patch.object(
                              NativeProxy, 'cache_del', self.cache_del):
            tenant_cache = TenantCache()
            tenant_cache.delete_tenant(self.tenant_id)

        self.cache_del.assert_called_once_with(self.tenant_id, CACHE_TENANT)

    def test_delete_tenant_does_not_call_cache_del(self):
        with patch.object(NativeProxy, 'cache_exists',
                          self.cache_false), patch.object(
                              NativeProxy, 'cache_del', self.cache_del):
            tenant_cache = TenantCache()
            tenant_cache.delete_tenant(self.tenant_id)

        with self.assertRaises(AssertionError):
            self.cache_del.assert_called_once_with(self.tenant_id,
                                                   CACHE_TENANT)