Esempio n. 1
0
    def _compose(  # pylint: disable=arguments-differ
            self, all_violations, total_resources):
        """Compose the scan summary.

        Build a summary of the violations and counts for the email.
        resource summary:
            {
                RESOURCE_TYPE: {
                    'pluralized_resource_type': '{RESOURCE_TYPE}s'
                    'total': TOTAL,
                    'violations': {
                        RESOURCE_ID: NUM_VIOLATIONS,
                        RESOURCE_ID: NUM_VIOLATIONS,
                        ...
                    }
                },
                ...
            }
        Args:
            all_violations (list): List of violations.
            total_resources (dict): A dict of the resources and their count.

        Returns:
            int: total_violations, an integer of the total violations.
            dict: resource_summaries, a dict of resource to violations.
                {'organization':
                    {'pluralized_resource_type': 'Organizations',
                     'total': 1,
                     'violations': OrderedDict([('660570133860', 67)])},
                 'project':
                    {'pluralized_resource_type': 'Projects',
                     'total': 41,
                     'violations': OrderedDict([('foo1_project', 111),
                                                ('foo2_project', 222),
                                                ('foo3_project', 333)])}}
        """
        resource_summaries = {}
        total_violations = 0

        for violation in sorted(all_violations,
                                key=lambda v: v.get('resource_id')):
            resource_id = violation.get('resource_id')
            resource_type = violation.get('resource_type')
            if resource_type not in resource_summaries:
                resource_summaries[resource_type] = {
                    'pluralized_resource_type': resource_util.pluralize(
                        resource_type),
                    'total': total_resources[resource_type],
                    'violations': collections.OrderedDict()
                }

            # Keep track of # of violations per resource id.
            if (resource_id not in
                    resource_summaries[resource_type]['violations']):
                resource_summaries[resource_type]['violations'][resource_id] = 0

            resource_summaries[resource_type]['violations'][resource_id] += 1
            total_violations += 1

        return total_violations, resource_summaries
Esempio n. 2
0
def _build_scan_summary(all_violations, total_resources):
    """Build the scan summary.

    Build a summary of the violations and counts for the email.

    resource summary:
        {
            RESOURCE_TYPE: {
                'pluralized_resource_type': '{RESOURCE_TYPE}s'
                'total': TOTAL,
                'violations': {
                    RESOURCE_ID: NUM_VIOLATIONS,
                    RESOURCE_ID: NUM_VIOLATIONS,
                    ...
                }
            },
            ...
        }

    Args:
        all_violations: List of violations.
        total_resources: A dict of the resources and their count.

    Returns:
        Total counts and summaries.
    """

    resource_summaries = {}
    total_violations = 0

    for violation in sorted(all_violations, key=lambda v: v.resource_id):
        resource_type = violation.resource_type
        if resource_type not in resource_summaries:
            resource_summaries[resource_type] = {
                'pluralized_resource_type':
                resource_util.pluralize(resource_type),
                'total': total_resources[resource_type],
                'violations': collections.OrderedDict()
            }

        # Keep track of # of violations per resource id.
        if (violation.resource_id
                not in resource_summaries[resource_type]['violations']):
            resource_summaries[resource_type]['violations'][
                violation.resource_id] = 0

        resource_summaries[resource_type]['violations'][
            violation.resource_id] += len(violation.members)
        total_violations += len(violation.members)

    return total_violations, resource_summaries
Esempio n. 3
0
 def test_plural_nonexist_resource_returns_none(self):
     """Test that trying to get plural nonexistent resource returns None."""
     self.assertIsNone(resource_util.pluralize('nonexistent'))
Esempio n. 4
0
 def test_plural_is_correct(self):
     """Test that the resource type is pluralized correctly."""
     self.assertEqual('Organizations',
         resource_util.pluralize(ResourceType.ORGANIZATION))
     self.assertEqual('Projects',
         resource_util.pluralize(ResourceType.PROJECT))