Beispiel #1
0
    def get_datasources_tables_data(self):
        try:
            datasources = congress.datasources_list(self.request)
        except Exception as e:
            msg = _('Unable to get services list: %s') % e.message
            messages.error(self.request, msg)
            return []

        ds_temp = []
        for ds in datasources:
            ds_id = ds['id']
            try:
                ds_tables = congress.datasource_tables_list(self.request,
                                                            ds_id)
            except Exception as e:
                msg_args = {'ds_id': ds_id, 'error': e.message}
                msg = _('Unable to get tables list for service "%(ds_id)s": '
                        '%(error)s') % msg_args
                messages.error(self.request, msg)
                return []

            for table in ds_tables:
                table.set_value('datasource_id', ds_id)
                table.set_value('datasource_name', ds['name'])
                table.set_value('datasource_driver', ds['driver'])
                table.set_id_as_name_if_empty()
                ds_temp.append(table)

        logger.info("ds_temp %s" % ds_temp)
        return ds_temp
Beispiel #2
0
def _get_service_tables(request):
    # Return all service tables.
    all_tables = []
    try:
        # Get all the services.
        services = congress.datasources_list(request)
    except Exception as e:
        LOG.error('Unable to get list of data sources: %s' % e.message)
    else:
        try:
            for service in services:
                # Get all the tables in this service.
                service_id = service['id']
                service_tables = congress.datasource_tables_list(request,
                                                                 service_id)
                # Get the names of the tables.
                datasource_tables = []
                for table in service_tables:
                    table.set_id_as_name_if_empty()
                    datasource_tables.append(table['name'])

                all_tables.append({'datasource': service['name'],
                                   'tables': datasource_tables})
        except Exception as e:
            LOG.error('Unable to get tables for data source "%s": %s' %
                      (service_id, e.message))
    return all_tables
Beispiel #3
0
    def get_datasources_tables_data(self):
        try:
            datasources = congress.datasources_list(self.request)
        except Exception as e:
            msg = _('Unable to get services list: %s') % e.message
            messages.error(self.request, msg)
            return []

        ds_temp = []
        for ds in datasources:
            ds_id = ds['id']
            try:
                ds_tables = congress.datasource_tables_list(self.request,
                                                            ds_id)
            except Exception as e:
                msg_args = {'ds_id': ds_id, 'error': e.message}
                msg = _('Unable to get tables list for service "%(ds_id)s": '
                        '%(error)s') % msg_args
                messages.error(self.request, msg)
                return []

            for table in ds_tables:
                table.set_value('datasource_id', ds_id)
                table.set_value('datasource_name', ds['name'])
                table.set_value('datasource_driver', ds['driver'])
                table.set_id_as_name_if_empty()
                # Object ids within a Horizon table must be unique. Otherwise,
                # Horizon will cache the column values for the object by id and
                # use the same column values for all rows with the same id.
                table.set_value('table_id', table['id'])
                table.set_value('id', '%s-%s' % (ds_id, table['table_id']))
                ds_temp.append(table)

        logger.debug("ds_temp %s" % ds_temp)
        return ds_temp
Beispiel #4
0
    def get_datasources_tables_data(self):
        try:
            datasources = congress.datasources_list(self.request)
        except Exception as e:
            msg = _('Unable to get services list: %s') % e.message
            messages.error(self.request, msg)
            return []

        ds_temp = []
        for ds in datasources:
            ds_id = ds['id']
            try:
                ds_tables = congress.datasource_tables_list(
                    self.request, ds_id)
            except Exception as e:
                msg_args = {'ds_id': ds_id, 'error': e.message}
                msg = _('Unable to get tables list for service "%(ds_id)s": '
                        '%(error)s') % msg_args
                messages.error(self.request, msg)
                return []

            for table in ds_tables:
                table.set_value('datasource_id', ds_id)
                table.set_value('datasource_name', ds['name'])
                table.set_value('datasource_driver', ds['driver'])
                table.set_id_as_name_if_empty()
                ds_temp.append(table)

        logger.info("ds_temp %s" % ds_temp)
        return ds_temp
Beispiel #5
0
    def get_datasources_tables_data(self):
        try:
            ds1 = congress.datasources_list(self.request)
        except Exception as e:
            msg = _('Unable to get plugins list: %s') % e.message
            messages.error(self.request, msg)
            return []

        ds_temp = []
        for d1 in ds1:
            s = d1['id']
            owner_id = d1['owner_id']
            try:
                ds = congress.datasource_tables_list(self.request, s)
            except Exception as e:
                msg_args = {'ds_name': s, 'error': e.message}
                msg = _('Unable to get tables list for plugin "%(ds_name)s": '
                        '%(error)s') % msg_args
                messages.error(self.request, msg)
                return []

            for d in ds:
                d.set_value('datasource', s)
                d.set_value('owner_id', owner_id)
                d.set_id_as_name_if_empty()
                ds_temp.append(d)

        logger.info("ds_temp %s" % ds_temp)
        return ds_temp
Beispiel #6
0
    def get_data(self):
        datasource_id = self.kwargs['datasource_id']
        table_name = self.kwargs.get('policy_table_name')
        is_service = False

        try:
            if table_name:
                # Policy data table.
                rows = congress.policy_rows_list(self.request, datasource_id,
                                                 table_name)
                if congress.SERVICE_TABLE_SEPARATOR in table_name:
                    table_name_parts = table_name.split(
                        congress.SERVICE_TABLE_SEPARATOR)
                    maybe_datasource_name = table_name_parts[0]
                    datasources = congress.datasources_list(self.request)
                    for datasource in datasources:
                        if datasource['name'] == maybe_datasource_name:
                            # Serivce-derived policy data table.
                            is_service = True
                            datasource_id = datasource['id']
                            table_name = table_name_parts[1]
                            break
            else:
                # Service data table.
                is_service = True
                datasource = congress.datasource_get_by_name(
                    self.request, datasource_id)
                table_name = self.kwargs['service_table_name']
                rows = congress.datasource_rows_list(
                    self.request, datasource_id, table_name)
        except Exception as e:
            msg_args = {
                'table_name': table_name,
                'ds_id': datasource_id,
                'error': e.message
            }
            msg = _('Unable to get rows in table "%(table_name)s", data '
                    'source "%(ds_id)s": %(error)s') % msg_args
            messages.error(self.request, msg)
            redirect = reverse('horizon:admin:datasources:index')
            raise exceptions.Http302(redirect)

        # Normally, in Horizon, the columns for a table are defined as
        # attributes of the Table class. When the class is instantiated,
        # the columns are processed during the metaclass initialization. To
        # add columns dynamically, re-create the class from the metaclass
        # with the added columns, re-create the Table from the new class,
        # then reassign the Table stored in this View.
        column_names = []
        table_class_attrs = copy.deepcopy(dict(self.table_class.__dict__))
        # Get schema from the server.
        try:
            if is_service:
                schema = congress.datasource_table_schema_get(
                    self.request, datasource_id, table_name)
            else:
                schema = congress.policy_table_schema_get(
                    self.request, datasource_id, table_name)
        except Exception as e:
            msg_args = {
                'table_name': table_name,
                'ds_id': datasource_id,
                'error': e.message
            }
            msg = _('Unable to get schema for table "%(table_name)s", '
                    'data source "%(ds_id)s": %(error)s') % msg_args
            messages.error(self.request, msg)
            redirect = reverse('horizon:admin:datasources:index')
            raise exceptions.Http302(redirect)

        columns = schema['columns']
        row_len = 0
        if len(rows):
            row_len = len(rows[0].get('data', []))

        if not row_len or row_len == len(columns):
            for col in columns:
                col_name = col['name']
                # Attribute name for column in the class must be a valid
                # identifier. Slugify it.
                col_slug = slugify(col_name)
                column_names.append(col_slug)
                table_class_attrs[col_slug] = tables.Column(
                    col_slug, verbose_name=col_name)
        else:
            # There could be another table with the same name and different
            # arity. Divide the rows into unnamed columns. Number them for
            # internal reference.
            for i in xrange(0, row_len):
                col_name = str(i)
                column_names.append(col_name)
                table_class_attrs[col_name] = tables.Column(
                    col_name, verbose_name='')

        # Class and object re-creation, using a new class name, the same base
        # classes, and the new class attributes, which now includes columns.
        columnized_table_class_name = '%s%sRows' % (
            slugify(datasource_id).title(), slugify(table_name).title())
        columnized_table_class = tables.base.DataTableMetaclass(
            str(columnized_table_class_name), self.table_class.__bases__,
            table_class_attrs)

        self.table_class = columnized_table_class
        columnized_table = columnized_table_class(self.request, **self.kwargs)
        self._tables[columnized_table_class._meta.name] = columnized_table

        # Map columns names to row values.
        num_cols = len(column_names)
        for row in rows:
            try:
                row_data = row['data']
                row.delete_by_key('data')
                for i in xrange(0, num_cols):
                    row.set_value(column_names[i], row_data[i])
            except Exception as e:
                msg_args = {
                    'table_name': table_name,
                    'ds_id': datasource_id,
                    'error': e.message
                }
                msg = _('Unable to get data for table "%(table_name)s", data '
                        'source "%(ds_id)s": %(error)s') % msg_args
                messages.error(self.request, msg)
                redirect = reverse('horizon:admin:datasources:index')
                raise exceptions.Http302(redirect)

        return rows
Beispiel #7
0
def get_datasource_columns(request):
    """Get of names of columns from all data sources.

    Example:
    [
        {
            'datasource': 'classification',
            'tables': [
                {
                    'table': 'error',
                    'columns': ['name']
                }
            ]
        },
        {
            'datasource': 'neutronv2',
            'tables': [
                {
                    'table': 'networks',
                    'columns':  ['id', 'tenant_id', ...],
                },
                ...
            ],
            ...
        },
        ...
    ]
    """
    all_columns = []

    # Get all the policy tables.
    policy_tables = _get_policy_tables(request)
    try:
        for policy in policy_tables:
            # Get all the columns in this policy. Unlike for the services,
            # there's currently no congress client API to get the schema for
            # all tables in a policy in a single call.
            policy_name = policy['datasource']
            tables = policy['tables']

            datasource_tables = []
            for table_name in tables:
                # Get all the columns in this policy table.
                schema = congress.policy_table_schema_get(request, policy_name,
                                                          table_name)
                columns = [c['name'] for c in schema['columns']]
                datasource_tables.append({'table': table_name,
                                          'columns': columns})

            all_columns.append({'datasource': policy_name,
                                'tables': datasource_tables})
    except Exception as e:
        LOG.error('Unable to get schema for policy "%s" table "%s": %s' %
                  (policy_name, table_name, e.message))

    try:
        # Get all the services.
        services = congress.datasources_list(request)
    except Exception as e:
        LOG.error('Unable to get list of data sources: %s' % e.message)
    else:
        try:
            for service in services:
                # Get the schema for this service.
                service_id = service['id']
                service_name = service['name']
                schema = congress.datasource_schema_get(request, service_id)

                datasource_tables = []
                for table in schema['tables']:
                    # Get the columns for this table.
                    columns = [c['name'] for c in table['columns']]
                    datasource_table = {'table': table['table_id'],
                                        'columns': columns}
                    datasource_tables.append(datasource_table)

                all_columns.append({'datasource': service_name,
                                    'tables': datasource_tables})
        except Exception as e:
            LOG.error('Unable to get schema for data source "%s": %s' %
                      (service_id, e.message))

    return all_columns
Beispiel #8
0
    def get_data(self):
        datasource_id = self.kwargs['datasource_id']
        table_name = self.kwargs.get('policy_table_name')
        is_service = False

        try:
            if table_name:
                # Policy data table.
                rows = congress.policy_rows_list(self.request, datasource_id,
                                                 table_name)
                if congress.SERVICE_TABLE_SEPARATOR in table_name:
                    table_name_parts = table_name.split(
                        congress.SERVICE_TABLE_SEPARATOR)
                    maybe_datasource_name = table_name_parts[0]
                    datasources = congress.datasources_list(self.request)
                    for datasource in datasources:
                        if datasource['name'] == maybe_datasource_name:
                            # Serivce-derived policy data table.
                            is_service = True
                            datasource_id = datasource['id']
                            table_name = table_name_parts[1]
                            break
            else:
                # Service data table.
                is_service = True
                datasource = congress.datasource_get_by_name(
                    self.request, datasource_id)
                table_name = self.kwargs['service_table_name']
                rows = congress.datasource_rows_list(self.request,
                                                     datasource_id, table_name)
        except Exception as e:
            msg_args = {
                'table_name': table_name,
                'ds_id': datasource_id,
                'error': e.message
            }
            msg = _('Unable to get rows in table "%(table_name)s", data '
                    'source "%(ds_id)s": %(error)s') % msg_args
            messages.error(self.request, msg)
            redirect = reverse('horizon:admin:datasources:index')
            raise exceptions.Http302(redirect)

        # Normally, in Horizon, the columns for a table are defined as
        # attributes of the Table class. When the class is instantiated,
        # the columns are processed during the metaclass initialization. To
        # add columns dynamically, re-create the class from the metaclass
        # with the added columns, re-create the Table from the new class,
        # then reassign the Table stored in this View.
        column_names = []
        table_class_attrs = copy.deepcopy(dict(self.table_class.__dict__))
        # Get schema from the server.
        try:
            if is_service:
                schema = congress.datasource_table_schema_get(
                    self.request, datasource_id, table_name)
            else:
                schema = congress.policy_table_schema_get(
                    self.request, datasource_id, table_name)
        except Exception as e:
            msg_args = {
                'table_name': table_name,
                'ds_id': datasource_id,
                'error': e.message
            }
            msg = _('Unable to get schema for table "%(table_name)s", '
                    'data source "%(ds_id)s": %(error)s') % msg_args
            messages.error(self.request, msg)
            redirect = reverse('horizon:admin:datasources:index')
            raise exceptions.Http302(redirect)

        columns = schema['columns']
        row_len = 0
        if len(rows):
            row_len = len(rows[0].get('data', []))

        if not row_len or row_len == len(columns):
            for col in columns:
                col_name = col['name']
                # Attribute name for column in the class must be a valid
                # identifier. Slugify it.
                col_slug = slugify(col_name)
                column_names.append(col_slug)
                table_class_attrs[col_slug] = tables.Column(
                    col_slug, verbose_name=col_name)
        else:
            # There could be another table with the same name and different
            # arity. Divide the rows into unnamed columns. Number them for
            # internal reference.
            for i in xrange(0, row_len):
                col_name = str(i)
                column_names.append(col_name)
                table_class_attrs[col_name] = tables.Column(col_name,
                                                            verbose_name='')

        # Class and object re-creation, using a new class name, the same base
        # classes, and the new class attributes, which now includes columns.
        columnized_table_class_name = '%s%sRows' % (
            slugify(datasource_id).title(), slugify(table_name).title())
        columnized_table_class = tables.base.DataTableMetaclass(
            str(columnized_table_class_name), self.table_class.__bases__,
            table_class_attrs)

        self.table_class = columnized_table_class
        columnized_table = columnized_table_class(self.request, **self.kwargs)
        self._tables[columnized_table_class._meta.name] = columnized_table

        # Map columns names to row values.
        num_cols = len(column_names)
        for row in rows:
            try:
                row_data = row['data']
                row.delete_by_key('data')
                for i in xrange(0, num_cols):
                    row.set_value(column_names[i], row_data[i])
            except Exception as e:
                msg_args = {
                    'table_name': table_name,
                    'ds_id': datasource_id,
                    'error': e.message
                }
                msg = _('Unable to get data for table "%(table_name)s", data '
                        'source "%(ds_id)s": %(error)s') % msg_args
                messages.error(self.request, msg)
                redirect = reverse('horizon:admin:datasources:index')
                raise exceptions.Http302(redirect)

        return rows