def cross_link_regions(self, components):
    region_and_vpc_tuples = [(r, c.vpc) for (r, c) in components.iteritems()]
    host_vpc = None
    if (self.host_vpc_id and self.host_vpc_region):
        host_vpc = get_client(self.host_vpc_region).Vpc(self.host_vpc_id)
        region_and_vpc_tuples.append((self.host_vpc_region, host_vpc))
    for i in xrange((len(region_and_vpc_tuples) - 1)):
        (i_region, i_vpc) = region_and_vpc_tuples[i]
        for j in xrange((i + 1), len(region_and_vpc_tuples)):
            (j_region, j_vpc) = region_and_vpc_tuples[j]
            peering = create_vpc_peering(client=get_client(i_region), vpc=j_vpc, host_vpc=i_vpc, target_region=j_region)
            if (len(peering) != 1):
                raise YBOpsRuntimeError('Expecting one peering connection, got {}'.format(peer_conn))
            peering = peering[0]
            add_route_to_rt(components[i_region].route_table, j_vpc.cidr_block, 'VpcPeeringConnectionId', peering.id)
            if ((host_vpc is None) or (j != (len(region_and_vpc_tuples) - 1))):
                add_route_to_rt(components[j_region].route_table, i_vpc.cidr_block, 'VpcPeeringConnectionId', peering.id)
            else:
                for rt in list(host_vpc.route_tables.all()):
                    add_route_to_rt(rt, i_vpc.cidr_block, 'VpcPeeringConnectionId', peering.id)
    all_cidrs = [vpc.cidr_block for (r, vpc) in region_and_vpc_tuples]
    rules = []
    for cidr in all_cidrs:
        add_cidr_to_rules(rules, cidr)
    for cidr in self.metadata.get('custom_network_whitelisted_ip_cidrs', []):
        add_cidr_to_rules(rules, cidr)
    for (region, component) in components.iteritems():
        sg = component.sg_yugabyte
        ip_perms = sg.ip_permissions
        for rule in rules:
            found = False
            for perm in ip_perms:
                if ((perm.get('FromPort') == rule['from_port']) and (perm.get('ToPort') == rule['to_port']) and (perm.get('IpProtocol') == rule['ip_protocol']) and (len([True for r in perm.get('IpRanges', []) if (r.get('CidrIp') == rule['cidr_ip'])]) > 0)):
                    found = True
                    break
            if (not found):
                try:
                    sg.authorize_ingress(IpProtocol=rule['ip_protocol'], CidrIp=rule['cidr_ip'], FromPort=rule['from_port'], ToPort=rule['to_port'])
                except Exception as e:
                    if ('InvalidPermission.Duplicate' not in str(e)):
                        raise YBOpsRuntimeError('Authorize Security Group Ingress failed: {}'.format(repr(e)))
def cross_link_regions(self, components):
    region_and_vpc_tuples = [(r, c.vpc) for (r, c) in components.iteritems()]
    host_vpc = None
    if (self.host_vpc_id and self.host_vpc_region):
        host_vpc = get_client(self.host_vpc_region).Vpc(self.host_vpc_id)
        region_and_vpc_tuples.append((self.host_vpc_region, host_vpc))
    for i in xrange((len(region_and_vpc_tuples) - 1)):
        (i_region, i_vpc) = region_and_vpc_tuples[i]
        for j in xrange((i + 1), len(region_and_vpc_tuples)):
            (j_region, j_vpc) = region_and_vpc_tuples[j]
            peering = create_vpc_peering(client=get_client(i_region), vpc=j_vpc, host_vpc=i_vpc, target_region=j_region)
            if (len(peering) != 1):
                raise YBOpsRuntimeError('Expecting one peering connection, got {}'.format(peer_conn))
            peering = peering[0]
            add_route_to_rt(components[i_region].route_table, j_vpc.cidr_block, 'VpcPeeringConnectionId', peering.id)
            if ((host_vpc is None) or (j != (len(region_and_vpc_tuples) - 1))):
                add_route_to_rt(components[j_region].route_table, i_vpc.cidr_block, 'VpcPeeringConnectionId', peering.id)
            else:
                for rt in list(host_vpc.route_tables.all()):
                    add_route_to_rt(rt, i_vpc.cidr_block, 'VpcPeeringConnectionId', peering.id)
    all_cidrs = [vpc.cidr_block for (r, vpc) in region_and_vpc_tuples]
    rules = []
    for cidr in all_cidrs:
        add_cidr_to_rules(rules, cidr)
    for cidr in self.metadata.get('custom_network_whitelisted_ip_cidrs', []):
        add_cidr_to_rules(rules, cidr)
    for (region, component) in components.iteritems():
        sg = component.sg_yugabyte
        ip_perms = sg.ip_permissions
        for rule in rules:
            found = False
            for perm in ip_perms:
                if ((perm.get('FromPort') == rule['from_port']) and (perm.get('ToPort') == rule['to_port']) and (perm.get('IpProtocol') == rule['ip_protocol']) and (len([True for r in perm.get('IpRanges', []) if (r.get('CidrIp') == rule['cidr_ip'])]) > 0)):
                    found = True
                    break
            if (not found):
                try:
                    sg.authorize_ingress(IpProtocol=rule['ip_protocol'], CidrIp=rule['cidr_ip'], FromPort=rule['from_port'], ToPort=rule['to_port'])
                except Exception as e:
                    if ('InvalidPermission.Duplicate' not in str(e)):
                        raise YBOpsRuntimeError('Authorize Security Group Ingress failed: {}'.format(repr(e)))