コード例 #1
0
ファイル: workflows.py プロジェクト: ramineni/my_congress
 def _get_schema_columns(self, request, table):
     table_parts = table.split(congress.TABLE_SEPARATOR)
     datasource = table_parts[0]
     table_name = table_parts[1]
     try:
         schema = congress.datasource_table_schema_get_by_name(
             request, datasource, table_name)
     except Exception:
         # Maybe it's a policy table, not a service.
         try:
             schema = congress.policy_table_schema_get(
                 request, datasource, table_name)
         except Exception as e:
             # Nope.
             LOG.error('Unable to get schema for table "%s", '
                       'datasource "%s": %s',
                       table_name, datasource, str(e))
             return str(e)
     return schema['columns']
コード例 #2
0
 def _get_schema_columns(self, request, table):
     table_parts = table.split(congress.TABLE_SEPARATOR)
     datasource = table_parts[0]
     table_name = table_parts[1]
     try:
         schema = congress.datasource_table_schema_get_by_name(
             request, datasource, table_name)
     except Exception:
         # Maybe it's a policy table, not a service.
         try:
             schema = congress.policy_table_schema_get(
                 request, datasource, table_name)
         except Exception as e:
             # Nope.
             LOG.error(
                 'Unable to get schema for table "%s", '
                 'datasource "%s": %s', table_name, datasource, str(e))
             return str(e)
     return schema['columns']
コード例 #3
0
ファイル: views.py プロジェクト: ramineni/my_congress
    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.TABLE_SEPARATOR in table_name:
                    table_name_parts = table_name.split(
                        congress.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': str(e)
            }
            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': str(e)
            }
            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': str(e)
                }
                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
コード例 #4
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.TABLE_SEPARATOR in table_name:
                    table_name_parts = table_name.split(
                        congress.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': str(e)
            }
            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': str(e)
            }
            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': str(e)
                }
                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
コード例 #5
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, str(e))

    try:
        # Get all the services.
        services = congress.datasources_list(request)
    except Exception as e:
        LOG.error('Unable to get list of data sources: %s', str(e))
    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, str(e))

    return all_columns
コード例 #6
0
ファイル: utils.py プロジェクト: ramineni/my_congress
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, str(e))

    try:
        # Get all the services.
        services = congress.datasources_list(request)
    except Exception as e:
        LOG.error('Unable to get list of data sources: %s', str(e))
    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, str(e))

    return all_columns