Ejemplo n.º 1
0
    def sync_grid(self):
        """Synchronize an active grid.

        Only one active grid should be kept where grid_status is set to 'ON'.
        """
        session = self._context.session
        grid_connection = self._grid_config.get_grid_connection()
        grid_connection_json = jsonutils.dumps(grid_connection)

        db_grids = dbi.get_grids(session)
        db_grid_ids = utils.get_values_from_records('grid_id', db_grids)

        # update the existing grid or add new grid
        if self._grid_config.grid_id in db_grid_ids:
            dbi.update_grid(session, self._grid_config.grid_id,
                            self._grid_config.grid_name, grid_connection_json,
                            const.GRID_STATUS_ON)
        else:
            dbi.add_grid(session, self._grid_config.grid_id,
                         self._grid_config.grid_name, grid_connection_json,
                         const.GRID_STATUS_ON, utils.get_hash())

        # deleting grids are delicate operation so we won't allow it
        # but we will set grid status to OFF to unused grids.
        persisted_set = set(db_grid_ids)
        disable_set = persisted_set.difference([self._grid_config.grid_id])
        disabling_grid_ids = list(disable_set)
        for grid_id in disabling_grid_ids:
            dbi.update_grid(session,
                            grid_id,
                            grid_status=const.GRID_STATUS_OFF)
        session.flush()
Ejemplo n.º 2
0
    def test_db_records_to_json(self):
        grid_1_id = 100
        grid_2_id = 200
        dbi.add_grid(self.ctx.session, grid_1_id, 'test grid 1',
                     '{"wapi_version": "2.0",'
                     '"wapi_admin_user": '******'{ "name": "admin", "password": "******" }}',
                     'ON',
                     'gm-id-1')
        dbi.add_grid(self.ctx.session, grid_2_id, 'test grid 2', '{}', 'OFF',
                     'gm-id-2')

        grids = dbi.get_grids(self.ctx.session)

        json = utils.db_records_to_json(grids)

        self.assertEqual('test grid 1', json[0]["grid_name"])
        self.assertEqual('test grid 2', json[1]["grid_name"])

        json_string = json[0]["grid_connection"]
        grid_connection_json = jsonutils.loads(json_string)

        self.assertIsInstance(json_string, six.string_types)
        self.assertIsInstance(grid_connection_json, dict)
        self.assertEqual('2.0', grid_connection_json['wapi_version'])
        self.assertEqual('admin',
                         grid_connection_json['wapi_admin_user']['name'])

        grid_connection = utils.json_to_obj('grid_connection',
                                            grid_connection_json)
        self.assertEqual('2.0', grid_connection.wapi_version)
        self.assertEqual('admin', grid_connection.wapi_admin_user.name)

        self.assertEqual('{}', json[1]["grid_connection"])
        self.assertEqual({}, jsonutils.loads(json[1]["grid_connection"]))
Ejemplo n.º 3
0
    def test_get_values_from_records(self):
        grid_1_id = 100
        grid_2_id = 200
        dbi.add_grid(self.ctx.session, grid_1_id, 'test grid 1', '{}', 'ON',
                     'gm-id-1')
        dbi.add_grid(self.ctx.session, grid_2_id, 'test grid 2', '{}', 'OFF',
                     'gm-id-2')

        grids = dbi.get_grids(self.ctx.session)
        grid_ids = utils.get_values_from_records('grid_id', grids)

        self.assertEqual(grid_1_id, grid_ids[0])
        self.assertEqual(grid_2_id, grid_ids[1])

        grid_names = utils.get_values_from_records('grid_name', grids)
        self.assertEqual('test grid 1', grid_names[0])
        self.assertEqual('test grid 2', grid_names[1])
Ejemplo n.º 4
0
    def test_get_composite_values_from_records(self):
        grid_1_id = 100
        grid_1_name = 'test grid 1'
        grid_2_id = 200
        grid_2_name = 'test grid 2'
        dbi.remove_grids(self.ctx.session, [grid_1_id, grid_2_id])
        dbi.add_grid(self.ctx.session, grid_1_id, grid_1_name, '{}', 'ON',
                     'gm-id-1')
        dbi.add_grid(self.ctx.session, grid_2_id, grid_2_name, '{}', 'OFF',
                     'gm-id-2')

        grids = dbi.get_grids(self.ctx.session)
        composite_keys = ['grid_id', 'grid_name']
        delimiter = '-'
        composite_values = utils.get_composite_values_from_records(
            composite_keys, grids, delimiter)
        expected_value = str(grid_1_id) + delimiter + grid_1_name
        self.assertEqual(expected_value, composite_values[0])
        expected_value = str(grid_2_id) + delimiter + grid_2_name
        self.assertEqual(expected_value, composite_values[1])
Ejemplo n.º 5
0
    def test_db_records_to_obj(self):
        grid_1_id = 100
        grid_2_id = 200
        dbi.add_grid(
            self.ctx.session, grid_1_id, 'test grid 1',
            '{"wapi_version": "2.0",'
            '"wapi_admin_user": '******'{ "name": "admin", "password": "******" }}', 'ON', 'gm-id-1')
        dbi.add_grid(self.ctx.session, grid_2_id, 'test grid 2', '{}', 'ON',
                     'gm-id-2')

        grids = dbi.get_grids(self.ctx.session)
        grid_obj = utils.db_records_to_obj('Grid', grids)

        self.assertEqual('test grid 1', grid_obj[0].grid_name)
        self.assertEqual('test grid 1', grid_obj[0].get('grid_name'))
        self.assertEqual('test grid 1', grid_obj[0]['grid_name'])
        self.assertEqual('test grid 2', grid_obj[1].grid_name)
        self.assertEqual('test grid 2', grid_obj[1].get('grid_name'))
        self.assertEqual('test grid 2', grid_obj[1]['grid_name'])

        grid_connection = jsonutils.loads(grid_obj[0].grid_connection)
        self.assertEqual('admin', grid_connection["wapi_admin_user"]["name"])
Ejemplo n.º 6
0
    def sync_grid(self):
        """Synchronize an active grid.

        Only one active grid should be kept where grid_status is set to 'ON'.
        """
        session = self._context.session
        grid_connection = self._grid_config.get_grid_connection()
        grid_connection_json = jsonutils.dumps(grid_connection)

        db_grids = dbi.get_grids(session)
        db_grid_ids = utils.get_values_from_records('grid_id', db_grids)

        # update the existing grid or add new grid
        if self._grid_config.grid_id in db_grid_ids:
            dbi.update_grid(session,
                            self._grid_config.grid_id,
                            self._grid_config.grid_name,
                            grid_connection_json,
                            const.GRID_STATUS_ON)
        else:
            dbi.add_grid(session,
                         self._grid_config.grid_id,
                         self._grid_config.grid_name,
                         grid_connection_json,
                         const.GRID_STATUS_ON,
                         utils.get_hash())

        # deleting grids are delicate operation so we won't allow it
        # but we will set grid status to OFF to unused grids.
        persisted_set = set(db_grid_ids)
        disable_set = persisted_set.difference([self._grid_config.grid_id])
        disabling_grid_ids = list(disable_set)
        for grid_id in disabling_grid_ids:
            dbi.update_grid(session,
                            grid_id,
                            grid_status=const.GRID_STATUS_OFF)
        session.flush()
Ejemplo n.º 7
0
    def test_db_records_to_obj(self):
        grid_1_id = 100
        grid_2_id = 200
        dbi.add_grid(self.ctx.session, grid_1_id, 'test grid 1',
                     '{"wapi_version": "2.0",'
                     '"wapi_admin_user": '******'{ "name": "admin", "password": "******" }}',
                     'ON',
                     'gm-id-1')
        dbi.add_grid(self.ctx.session, grid_2_id, 'test grid 2', '{}', 'ON',
                     'gm-id-2')

        grids = dbi.get_grids(self.ctx.session)
        grid_obj = utils.db_records_to_obj('Grid', grids)

        self.assertEqual('test grid 1', grid_obj[0].grid_name)
        self.assertEqual('test grid 1', grid_obj[0].get('grid_name'))
        self.assertEqual('test grid 1', grid_obj[0]['grid_name'])
        self.assertEqual('test grid 2', grid_obj[1].grid_name)
        self.assertEqual('test grid 2', grid_obj[1].get('grid_name'))
        self.assertEqual('test grid 2', grid_obj[1]['grid_name'])

        grid_connection = jsonutils.loads(grid_obj[0].grid_connection)
        self.assertEqual('admin', grid_connection["wapi_admin_user"]["name"])
 def _create_grids(self, grid_list):
     for grid in grid_list:
         infoblox_db.add_grid(self.ctx.session, grid['grid_id'],
                              grid['grid_name'], grid['grid_connection'],
                              grid['grid_status'],
                              'gm-id-' + str(grid['grid_id']))
 def _create_default_grid(self):
     infoblox_db.add_grid(self.ctx.session, self.grid_id, self.grid_name,
                          self.grid_connection, self.grid_status,
                          'gm-id-' + str(self.grid_id))
     self.ctx.session.flush()
Ejemplo n.º 10
0
 def _create_grids(self, grid_list):
     for grid in grid_list:
         infoblox_db.add_grid(self.ctx.session, grid['grid_id'],
                              grid['grid_name'], grid['grid_connection'],
                              grid['grid_status'],
                              'gm-id-' + str(grid['grid_id']))
Ejemplo n.º 11
0
 def _create_default_grid(self):
     infoblox_db.add_grid(self.ctx.session, self.grid_id, self.grid_name,
                          self.grid_connection, self.grid_status,
                          'gm-id-' + str(self.grid_id))
     self.ctx.session.flush()