Exemple #1
0
    def get_bulk_action_list(cls, query, fields=None, show_related=True):
        """
        Return a list of serialized system objects and their related objects to
        be used in the bulk_action api.

        This function will serialize and export StaticReg objects and their
        accompanying HWAdapter objects
        """
        if not fields:
            fields = cls.get_api_fields() + ['pk']

        # Pull in all system blobs and tally which pks we've seen. In one swoop
        # pull in all staticreg blobs and put them with their systems.
        sys_t_bundles = cls.objects.filter(query).values_list(*fields)

        sys_d_bundles = {}
        sys_pks = []
        for t_bundle in sys_t_bundles:
            d_bundle = dict(zip(fields, t_bundle))
            system_hostname = d_bundle['hostname']
            sys_d_bundles[system_hostname] = d_bundle
            sys_d_bundles[system_hostname]['keyvalue_set'] = create_key_index(
                cls.keyvalue_set.related.model.objects.filter(
                    obj=d_bundle['pk']
                ).values('key', 'value', 'pk')
            )
            if show_related:
                sys_pks.append(d_bundle['pk'])

        sys_q = Q(system__in=sys_pks)

        # Note that CNAMEs are pulled in during this call
        sreg_bundles = cls.staticreg_set.related.model.get_bulk_action_list(
            sys_q
        )

        hw_q = Q(sreg__system__in=sys_pks)
        hw_bundles = (
            cls.staticreg_set.related.model.
            hwadapter_set.related.model.get_bulk_action_list(hw_q)
        )

        # JOIN staticreg, hw_adapter ON sreg_pk
        for sreg_pk, hw_bundle in hw_bundles.iteritems():
            sreg_bundles[sreg_pk]['hwadapter_set'] = hw_bundle

        for sreg_pk, sreg_bundle in sreg_bundles.iteritems():
            system = sreg_bundle.pop('system__hostname')
            sys_d_bundles[system].setdefault(
                'staticreg_set', {}
            )[sreg_bundle['name']] = sreg_bundle

        return sys_d_bundles
    def get_bulk_action_list(cls, query, fields=None):
        if not fields:
            fields = cls.get_api_fields() + ['pk', 'sreg']

        hw_t_bundles = cls.objects.filter(query).values_list(*fields)

        d_bundles = {}
        for t_bundle in hw_t_bundles:
            d_bundle = dict(zip(fields, t_bundle))
            d_bundle['keyvalue_set'] = create_key_index(
                cls.keyvalue_set.related.model.objects.filter(
                    obj=d_bundle['pk']).values('key', 'value', 'pk'))
            sreg = d_bundle.pop('sreg')
            d_bundles.setdefault(sreg, {})[d_bundle['name']] = d_bundle

        return d_bundles
Exemple #3
0
    def get_bulk_action_list(cls, query, fields=None, show_related=True):
        """
        Return a list of serialized system objects and their related objects to
        be used in the bulk_action api.

        This function will serialize and export StaticReg objects and their
        accompanying HWAdapter objects
        """
        if not fields:
            fields = cls.get_api_fields() + ['pk']

        # Pull in all system blobs and tally which pks we've seen. In one swoop
        # pull in all staticreg blobs and put them with their systems.
        sys_t_bundles = cls.objects.filter(query).values_list(*fields)

        sys_d_bundles = {}
        sys_pks = []
        for t_bundle in sys_t_bundles:
            d_bundle = dict(zip(fields, t_bundle))
            system_hostname = d_bundle['hostname']
            sys_d_bundles[system_hostname] = d_bundle
            sys_d_bundles[system_hostname]['keyvalue_set'] = create_key_index(
                cls.keyvalue_set.related.model.objects.filter(
                    obj=d_bundle['pk']).values('key', 'value', 'pk'))
            if show_related:
                sys_pks.append(d_bundle['pk'])

        sys_q = Q(system__in=sys_pks)

        # Note that CNAMEs are pulled in during this call
        sreg_bundles = cls.staticreg_set.related.model.get_bulk_action_list(
            sys_q)

        hw_q = Q(sreg__system__in=sys_pks)
        hw_bundles = (cls.staticreg_set.related.model.hwadapter_set.related.
                      model.get_bulk_action_list(hw_q))

        # JOIN staticreg, hw_adapter ON sreg_pk
        for sreg_pk, hw_bundle in hw_bundles.iteritems():
            sreg_bundles[sreg_pk]['hwadapter_set'] = hw_bundle

        for sreg_pk, sreg_bundle in sreg_bundles.iteritems():
            system = sreg_bundle.pop('system__hostname')
            sys_d_bundles[system].setdefault(
                'staticreg_set', {})[sreg_bundle['name']] = sreg_bundle

        return sys_d_bundles
Exemple #4
0
    def get_bulk_action_list(cls, query, fields=None):
        if not fields:
            fields = cls.get_api_fields() + ['pk', 'sreg']

        hw_t_bundles = cls.objects.filter(query).values_list(*fields)

        d_bundles = {}
        for t_bundle in hw_t_bundles:
            d_bundle = dict(zip(fields, t_bundle))
            d_bundle['keyvalue_set'] = create_key_index(
                cls.keyvalue_set.related.model.objects.filter(
                    obj=d_bundle['pk']
                ).values('key', 'value', 'pk')
            )
            sreg = d_bundle.pop('sreg')
            d_bundles.setdefault(sreg, {})[d_bundle['name']] = d_bundle

        return d_bundles
Exemple #5
0
    def get_bulk_action_list(cls, query, fields=None, show_related=True):
        if not fields:
            fields = cls.get_api_fields() + ['pk', 'name']
            # views is a M2M relationship and won't show up correctley in
            # values_list
            fields.remove('views')

        if show_related:
            # StaticReg objects are serialized. All other fields
            # are not serialized into JSON
            fields += ['system__hostname']

        # Pull in all system blobs and tally which pks we've seen. In one swoop
        # pull in all staticreg blobs and put them with their systems.
        sreg_q = cls.objects.filter(query)
        sreg_t_bundles = sreg_q.values_list(*fields)
        sreg_fqdns = sreg_q.values_list('fqdn', flat=True)
        cname_q = Q(target__in=sreg_fqdns)
        cname_bundles = CNAME.get_bulk_action_list(cname_q)

        d_bundles = {}
        for t_bundle in sreg_t_bundles:
            d_bundle = dict(zip(fields, t_bundle))
            d_bundle['keyvalue_set'] = create_key_index(
                cls.keyvalue_set.related.model.objects.filter(
                    obj=d_bundle['pk']
                ).values('key', 'value', 'pk')
            )
            d_bundle['views'] = list(
                cls.objects.get(pk=d_bundle['pk'])
                .views.values_list('pk', flat=True)
            )

            d_bundles[d_bundle['pk']] = d_bundle

            # Look up a cname and set it as the only value of 'cname' if *one*
            # cname exists. If there are more than one set the cname as a list.

            # cname_bundles is a inverted index (dict) on 'target'
            if d_bundle['fqdn'] in cname_bundles:
                d_bundle['cname'] = cname_bundles[d_bundle['fqdn']]

        return d_bundles
Exemple #6
0
    def get_bulk_action_list(cls, query, fields=None, show_related=True):
        if not fields:
            fields = cls.get_api_fields() + ['pk', 'name']
            # views is a M2M relationship and won't show up correctley in
            # values_list
            fields.remove('views')

        if show_related:
            # StaticReg objects are serialized. All other fields
            # are not serialized into JSON
            fields += ['system__hostname']

        # Pull in all system blobs and tally which pks we've seen. In one swoop
        # pull in all staticreg blobs and put them with their systems.
        sreg_q = cls.objects.filter(query)
        sreg_t_bundles = sreg_q.values_list(*fields)
        sreg_fqdns = sreg_q.values_list('fqdn', flat=True)
        cname_q = Q(target__in=sreg_fqdns)
        cname_bundles = CNAME.get_bulk_action_list(cname_q)

        d_bundles = {}
        for t_bundle in sreg_t_bundles:
            d_bundle = dict(zip(fields, t_bundle))
            d_bundle['keyvalue_set'] = create_key_index(
                cls.keyvalue_set.related.model.objects.filter(
                    obj=d_bundle['pk']).values('key', 'value', 'pk'))
            d_bundle['views'] = list(
                cls.objects.get(pk=d_bundle['pk']).views.values_list(
                    'pk', flat=True))

            d_bundles[d_bundle['pk']] = d_bundle

            # Look up a cname and set it as the only value of 'cname' if *one*
            # cname exists. If there are more than one set the cname as a list.

            # cname_bundles is a inverted index (dict) on 'target'
            if d_bundle['fqdn'] in cname_bundles:
                d_bundle['cname'] = cname_bundles[d_bundle['fqdn']]

        return d_bundles