def test_delete_datasource(self):
     id_ = uuidutils.generate_uuid()
     datasources.add_datasource(
         id_=id_, name="hiya", driver="foo", config="{user: foo}", description="hello", enabled=True
     )
     self.assertTrue(datasources.delete_datasource(id_))
     self.assertIsNone(datasources.get_datasource(id_))
Beispiel #2
0
 def test_delete_datasource(self):
     id_ = uuidutils.generate_uuid()
     datasources.add_datasource(id_=id_,
                                name="hiya",
                                driver="foo",
                                config='{user: foo}',
                                description="hello",
                                enabled=True)
     self.assertTrue(datasources.delete_datasource(id_))
 def test_delete_datasource_with_data(self):
     id_ = uuidutils.generate_uuid()
     datasources.add_datasource(
         id_=id_, name="hiya", driver="foo", config="{user: foo}", description="hello", enabled=True
     )
     db_ds_table_data.store_ds_table_data(
         ds_id=id_, tablename="bar", tabledata=set([("a1", "b1"), ("a2", "b2"), ("a3", "a4")])
     )
     self.assertTrue(datasources.delete_datasource_with_data(id_))
     self.assertEqual(db_ds_table_data.get_ds_table_data(id_), [])
 def test_get_datasource(self):
     id_ = uuidutils.generate_uuid()
     datasources.add_datasource(
         id_=id_, name="hiya", driver="foo", config="{user: foo}", description="hello", enabled=True
     )
     sources = datasources.get_datasources()
     self.assertEqual(id_, sources[0].id)
     self.assertEqual("hiya", sources[0].name)
     self.assertEqual("foo", sources[0].driver)
     self.assertEqual("hello", sources[0].description)
     self.assertEqual('"{user: foo}"', sources[0].config)
     self.assertTrue(sources[0].enabled)
Beispiel #5
0
 def test_delete_datasource_with_data(self):
     id_ = uuidutils.generate_uuid()
     datasources.add_datasource(id_=id_,
                                name="hiya",
                                driver="foo",
                                config={'user': '******'},
                                description="hello",
                                enabled=True)
     db_ds_table_data.store_ds_table_data(ds_id=id_,
                                          tablename='bar',
                                          tabledata=set([('a1', 'b1'),
                                                         ('a2', 'b2'),
                                                         ('a3', 'a4')]))
     self.assertTrue(datasources.delete_datasource_with_data(id_))
     self.assertEqual(db_ds_table_data.get_ds_table_data(id_), [])
Beispiel #6
0
 def test_get_datasource(self):
     id_ = uuidutils.generate_uuid()
     datasources.add_datasource(id_=id_,
                                name="hiya",
                                driver="foo",
                                config='{user: foo}',
                                description="hello",
                                enabled=True)
     sources = datasources.get_datasources()
     self.assertEqual(id_, sources[0].id)
     self.assertEqual("hiya", sources[0].name)
     self.assertEqual("foo", sources[0].driver)
     self.assertEqual("hello", sources[0].description)
     self.assertEqual('"{user: foo}"', sources[0].config)
     self.assertEqual(True, sources[0].enabled)
Beispiel #7
0
 def test_get_datasource_by_id(self):
     id_ = uuidutils.generate_uuid()
     datasources.add_datasource(id_=id_,
                                name="hiya",
                                driver="foo",
                                config={'user': '******'},
                                description="hello",
                                enabled=True)
     source = datasources.get_datasource(id_)
     self.assertEqual(id_, source.id)
     self.assertEqual("hiya", source.name)
     self.assertEqual("foo", source.driver)
     self.assertEqual("hello", source.description)
     self.assertEqual({'user': '******'}, json.loads(source.config))
     self.assertTrue(source.enabled)
Beispiel #8
0
 def test_get_datasource_by_id(self):
     id_ = uuidutils.generate_uuid()
     datasources.add_datasource(
         id_=id_,
         name="hiya",
         driver="foo",
         config={'user': '******'},
         description="hello",
         enabled=True)
     source = datasources.get_datasource(id_)
     self.assertEqual(id_, source.id)
     self.assertEqual("hiya", source.name)
     self.assertEqual("foo", source.driver)
     self.assertEqual("hello", source.description)
     self.assertEqual({'user': '******'}, json.loads(source.config))
     self.assertTrue(source.enabled)
Beispiel #9
0
    def add_datasource(cls, item, deleted=False, update_db=True):
        req = cls.make_datasource_dict(item)

        # check the request has valid infomation
        cls.validate_create_datasource(req)
        # If update_db is True, new_id will get a new value from the db.
        new_id = req['id']
        LOG.debug("adding datasource %s", req['name'])
        if update_db:
            LOG.debug("updating db")
            try:
                datasource = datasources_db.add_datasource(
                    id_=req['id'],
                    name=req['name'],
                    driver=req['driver'],
                    config=req['config'],
                    description=req['description'],
                    enabled=req['enabled'])
                new_id = datasource['id']
                req['id'] = new_id
            except db_exc.DBDuplicateEntry:
                raise exception.DatasourceNameInUse(value=req['name'])

        try:
            cls._add_datasource_service(req, item)
        except exception.DatasourceNameInUse:
            LOG.info('the datasource service is created by synchronizer.')
        except Exception:
            if update_db:
                datasources_db.delete_datasource(new_id)
            raise

        new_item = dict(item)
        new_item['id'] = new_id
        return cls.make_datasource_dict(new_item)
Beispiel #10
0
    def add_datasource(cls, item, deleted=False):
        req = cls.make_datasource_dict(item)
        driver_info = cls.validate_create_datasource(req)
        session = db.get_session()
        try:
            with session.begin(subtransactions=True):
                datasource = datasources_db.add_datasource(
                    id_=req['id'],
                    name=req['name'],
                    driver=req['driver'],
                    config=req['config'],
                    description=req['description'],
                    enabled=req['enabled'],
                    session=session)
                datasource = cls.make_datasource_dict(datasource)
                cage = d6cage.d6Cage()
                engine = cage.service_object('engine')
                try:
                    engine.create_policy(datasource['name'])
                except KeyError:
                    # FIXME(arosen): we need a better exception then
                    # key error being raised here
                    raise DatasourceNameInUse(name=req['name'])
                cage.createservice(name=datasource['name'],
                                   moduleName=driver_info['module'],
                                   args=datasource['config'],
                                   module_driver=True)
                service = cage.service_object(req['name'])
                engine.set_schema(req['name'], service.get_schema())

        except db_exc.DBDuplicateEntry:
            raise DatasourceNameInUse(name=req['name'])
        return cls.make_datasource_dict(datasource)
Beispiel #11
0
 def test_get_datasource_with_encryption(self):
     id_ = uuidutils.generate_uuid()
     datasources.add_datasource(id_=id_,
                                name="hiya",
                                driver="foo",
                                config={'user': '******'},
                                description="hello",
                                enabled=True,
                                secret_config_fields=['user'])
     sources = datasources.get_datasources()
     self.assertEqual(id_, sources[0].id)
     self.assertEqual("hiya", sources[0].name)
     self.assertEqual("foo", sources[0].driver)
     self.assertEqual("hello", sources[0].description)
     self.assertEqual({'user': '******'}, json.loads(sources[0].config))
     self.assertTrue(sources[0].enabled)
Beispiel #12
0
 def test_delete_datasource_with_data(self):
     id_ = uuidutils.generate_uuid()
     datasources.add_datasource(
         id_=id_,
         name="hiya",
         driver="foo",
         config={'user': '******'},
         description="hello",
         enabled=True)
     db_ds_table_data.store_ds_table_data(
         ds_id=id_,
         tablename='bar',
         tabledata=set([('a1', 'b1'), ('a2', 'b2'), ('a3', 'a4')])
         )
     self.assertTrue(datasources.delete_datasource_with_data(id_))
     self.assertEqual(db_ds_table_data.get_ds_table_data(id_), [])
Beispiel #13
0
 def test_get_datasource_by_id(self):
     id_ = uuidutils.generate_uuid()
     datasources.add_datasource(
         id_=id_,
         name="hiya",
         driver="foo",
         config='{user: foo}',
         description="hello",
         enabled=True)
     source = datasources.get_datasource(id_)
     self.assertEqual(id_, source.id)
     self.assertEqual("hiya", source.name)
     self.assertEqual("foo", source.driver)
     self.assertEqual("hello", source.description)
     self.assertEqual('"{user: foo}"', source.config)
     self.assertEqual(True, source.enabled)
Beispiel #14
0
 def test_get_datasource_with_encryption(self):
     id_ = uuidutils.generate_uuid()
     datasources.add_datasource(
         id_=id_,
         name="hiya",
         driver="foo",
         config={'user': '******'},
         description="hello",
         enabled=True,
         secret_config_fields=['user'])
     sources = datasources.get_datasources()
     self.assertEqual(id_, sources[0].id)
     self.assertEqual("hiya", sources[0].name)
     self.assertEqual("foo", sources[0].driver)
     self.assertEqual("hello", sources[0].description)
     self.assertEqual({'user': '******'}, json.loads(sources[0].config))
     self.assertTrue(sources[0].enabled)
Beispiel #15
0
    def add_datasource(cls, item, deleted=False, update_db=True):
        req = cls.make_datasource_dict(item)
        # If update_db is True, new_id will get a new value from the db.
        new_id = req['id']
        driver_info = cls.get_driver_info(item['driver'])
        session = db.get_session()
        try:
            with session.begin(subtransactions=True):
                LOG.debug("adding datasource %s", req['name'])
                if update_db:
                    LOG.debug("updating db")
                    datasource = datasources_db.add_datasource(
                        id_=req['id'],
                        name=req['name'],
                        driver=req['driver'],
                        config=req['config'],
                        description=req['description'],
                        enabled=req['enabled'],
                        session=session)
                    new_id = datasource['id']

                cls.validate_create_datasource(req)
                cage = cls.dseNode or d6cage.d6Cage()
                engine = cage.service_object('engine')
                try:
                    LOG.debug("creating policy %s", req['name'])
                    engine.create_policy(req['name'],
                                         kind=base.DATASOURCE_POLICY_TYPE)
                except KeyError:
                    # FIXME(arosen): we need a better exception then
                    # key error being raised here
                    raise DatasourceNameInUse(value=req['name'])
                try:
                    if cls.dseNode:
                        cls.createservice(name=req['name'],
                                          moduleName=driver_info['module'],
                                          args=item['config'],
                                          module_driver=True,
                                          type_='datasource_driver',
                                          id_=new_id)
                    else:
                        cage.createservice(name=req['name'],
                                           moduleName=driver_info['module'],
                                           args=item['config'],
                                           module_driver=True,
                                           type_='datasource_driver',
                                           id_=new_id)
                    service = cage.service_object(req['name'])
                    engine.set_schema(req['name'], service.get_schema())
                except Exception:
                    engine.delete_policy(req['name'])
                    raise DatasourceCreationError(value=req['name'])

        except db_exc.DBDuplicateEntry:
            raise DatasourceNameInUse(value=req['name'])
        new_item = dict(item)
        new_item['id'] = new_id
        return cls.make_datasource_dict(new_item)
Beispiel #16
0
    def add_datasource(self, item, deleted=False, update_db=True):
        req = self.make_datasource_dict(item)

        # check the request has valid information
        self.node.validate_create_datasource(req)
        if (len(req['name']) == 0 or req['name'][0] == '_'):
            raise exception.InvalidDatasourceName(value=req['name'])

        new_id = req['id']
        LOG.debug("adding datasource %s", req['name'])
        if update_db:
            LOG.debug("updating db")
            try:
                driver_info = self.node.get_driver_info(req['driver'])
                # Note(thread-safety): blocking call
                datasource = datasources_db.add_datasource(
                    id_=req['id'],
                    name=req['name'],
                    driver=req['driver'],
                    config=req['config'],
                    description=req['description'],
                    enabled=req['enabled'],
                    secret_config_fields=driver_info.get('secret', []))
            except db_exc.DBDuplicateEntry:
                raise exception.DatasourceNameInUse(value=req['name'])
            except db_exc.DBError:
                LOG.exception('Creating a new datasource failed due to '
                              'database backend error.')
                raise exception.DatasourceCreationError(value=req['name'])

        new_id = datasource['id']
        try:
            self.synchronizer.sync_datasource(req['name'])
            # immediate synch policies on local PE if present
            # otherwise wait for regularly scheduled synch
            engine = self.node.service_object(api_base.ENGINE_SERVICE_ID)
            if engine is not None:
                engine.synchronizer.sync_one_policy(req['name'])
            # TODO(dse2): also broadcast to all PE nodes to synch
        except exception.DataServiceError:
            LOG.debug('the datasource service is already '
                      'created in the node')
        except Exception:
            LOG.exception(
                'Unexpected exception encountered while registering '
                'new datasource %s.', req['name'])
            if update_db:
                datasources_db.delete_datasource(req['id'])
            msg = ("Datasource service: %s creation fails." % req['name'])
            raise exception.DatasourceCreationError(message=msg)

        new_item = dict(item)
        new_item['id'] = new_id
        return self.node.make_datasource_dict(new_item)
Beispiel #17
0
    def add_datasource(cls, item, deleted=False, update_db=True):
        req = cls.make_datasource_dict(item)
        # If update_db is True, new_id will get a new value from the db.
        new_id = req['id']
        driver_info = cls.get_driver_info(item['driver'])
        session = db.get_session()
        try:
            with session.begin(subtransactions=True):
                LOG.debug("adding datasource %s", req['name'])
                if update_db:
                    LOG.debug("updating db")
                    datasource = datasources_db.add_datasource(
                        id_=req['id'],
                        name=req['name'],
                        driver=req['driver'],
                        config=req['config'],
                        description=req['description'],
                        enabled=req['enabled'],
                        session=session)
                    new_id = datasource['id']

                cage = d6cage.d6Cage()
                engine = cage.service_object('engine')
                try:
                    LOG.debug("creating policy %s", req['name'])
                    engine.create_policy(req['name'])
                except KeyError:
                    # FIXME(arosen): we need a better exception then
                    # key error being raised here
                    raise DatasourceNameInUse(name=req['name'])
                cage.createservice(name=req['name'],
                                   moduleName=driver_info['module'],
                                   args=item['config'],
                                   module_driver=True,
                                   type_='datasource_driver',
                                   id_=new_id)
                service = cage.service_object(req['name'])
                engine.set_schema(req['name'], service.get_schema())

        except db_exc.DBDuplicateEntry:
            raise DatasourceNameInUse(name=req['name'])
        new_item = dict(item)
        new_item['id'] = new_id
        return cls.make_datasource_dict(new_item)
Beispiel #18
0
    def add_datasource(self, item, deleted=False, update_db=True):
        req = self.make_datasource_dict(item)

        # check the request has valid information
        self.validate_create_datasource(req)
        if self.is_valid_service(req['name']):
            raise exception.DatasourceNameInUse(value=req['name'])

        new_id = req['id']
        driver_info = self.get_driver_info(item['driver'])
        LOG.debug("adding datasource %s", req['name'])
        if update_db:
            LOG.debug("updating db")
            try:
                datasource = datasources_db.add_datasource(
                    id_=req['id'],
                    name=req['name'],
                    driver=req['driver'],
                    config=req['config'],
                    description=req['description'],
                    enabled=req['enabled'])
            except db_exc.DBDuplicateEntry:
                raise exception.DatasourceNameInUse(value=req['name'])

        new_id = datasource['id']
        try:
            # TODO(dse2): Call synchronizer to create datasource service after
            # implementing synchronizer for dse2.
            # https://bugs.launchpad.net/congress/+bug/1588167
            service = self.create_service(
                class_path=driver_info['module'],
                kwargs={'name': req['name'], 'args': item['config']})
            self.register_service(service)
        except exception.DataServiceError:
            LOG.exception('the datasource service is already'
                          'created in the node')
        except Exception:
            if update_db:
                datasources_db.delete_datasource(new_id)
            raise exception.DatasourceCreationError(value=req['name'])

        new_item = dict(item)
        new_item['id'] = new_id
        return self.make_datasource_dict(new_item)
Beispiel #19
0
    def add_datasource(self, item, deleted=False, update_db=True):
        req = self.make_datasource_dict(item)
        # If update_db is True, new_id will get a new value from the db.
        new_id = req['id']
        driver_info = self.get_driver_info(item['driver'])
        session = db.get_session()
        try:
            with session.begin(subtransactions=True):
                LOG.debug("adding datasource %s", req['name'])
                if update_db:
                    LOG.debug("updating db")
                    datasource = datasources_db.add_datasource(
                        id_=req['id'],
                        name=req['name'],
                        driver=req['driver'],
                        config=req['config'],
                        description=req['description'],
                        enabled=req['enabled'],
                        session=session)
                    new_id = datasource['id']

                self.validate_create_datasource(req)
                if self.is_valid_service(req['name']):
                    raise DatasourceNameInUse(value=req['name'])
                try:
                    self.create_service(
                        class_path=driver_info['module'],
                        kwargs={'name': req['name'], 'args': item['config']})
                except Exception:
                    raise DatasourceCreationError(value=req['name'])

        except db_exc.DBDuplicateEntry:
            raise DatasourceNameInUse(value=req['name'])
        new_item = dict(item)
        new_item['id'] = new_id
        return self.make_datasource_dict(new_item)