示例#1
0
    def render_groups(self):
        groups = []

        rules = sorted(list(self))

        # assumes sorting by these keys:
        # - GroupId
        # - (IpProtocol, FromPort, ToPort)
        # - (Direction, OtherGroupId)
        # - (Direction, OtherCidrIp)
        current_group = None
        current_perm = None
        for rule in rules:
            new_group = rule["GroupId"]
            if new_group != current_group:
                group = {}
                utils.copy_fields(rule, group, self.grp_flds)
                group['IpPermissions'] = []
                group['IpPermissionsEgress'] = []
                groups.append(group)
                current_group = new_group

            new_perm = (rule["IpProtocol"], rule["FromPort"], rule["ToPort"])
            if new_perm != current_perm:
                perm = dict(zip(self.perm_flds, new_perm))
                perm['IpRanges'] = []
                perm['UserIdGroupPairs'] = []
                if rule["Direction"] == "in":
                    group['IpPermissions'].append(perm)
                else:
                    group['IpPermissionsEgress'].append(perm)
                current_perm = new_perm

            if "OtherCidrIp" in rule:
                perm['IpRanges'].append({"CidrIp": rule['OtherCidrIp']})
            else:
                group = {}
                utils.copy_fields(rule, group, self.flat_othergrp_flds, self.othergrp_flds)
                perm['UserIdGroupPairs'].append(group)

        return groups
示例#2
0
    def flatten_groups(self, groups):
        directional_lists = (("IpPermissions", "in"), ("IpPermissionsEgress", "out"))

        for group in groups:
            for perm_list, direction in directional_lists:
                for perm in group[perm_list]:
                    for other in perm['UserIdGroupPairs']:
                        rule = Rule(Direction=direction)
                        utils.copy_fields(group, rule, self.grp_flds)
                        utils.copy_fields(perm, rule, self.perm_flds, defaults=self.perm_dft)
                        utils.copy_fields(other, rule, self.othergrp_flds, self.flat_othergrp_flds)
                        self.add(rule)
                    for other in perm['IpRanges']:
                        rule = Rule(Direction=direction)
                        utils.copy_fields(group, rule, self.grp_flds)
                        utils.copy_fields(perm, rule, self.perm_flds, defaults=self.perm_dft)
                        utils.copy_fields(other, rule, self.othercidr_flds, self.flat_othercidr_flds)
                        self.add(rule)
        return self