'Merge matching v3 endpoint refs into legacy refs.'

@controller.v2_deprecated
def get_endpoints(self, request):

    self.assert_admin(request)
    legacy_endpoints = {}
    v3_endpoints = {}
    for endpoint in self.catalog_api.list_endpoints():
        if (not endpoint.get('legacy_endpoint_id')):
            key = (endpoint['service_id'], endpoint['region_id'])
            v3_endpoints.setdefault(key, []).append(endpoint)
        else:
            if (endpoint['legacy_endpoint_id'] not in legacy_endpoints):
                legacy_ep = endpoint.copy()
                legacy_ep['id'] = legacy_ep.pop('legacy_endpoint_id')
                legacy_ep.pop('interface')
                legacy_ep.pop('url')
                legacy_ep['region'] = legacy_ep.pop('region_id')
                legacy_endpoints[endpoint['legacy_endpoint_id']] = legacy_ep
            else:
                legacy_ep = legacy_endpoints[endpoint['legacy_endpoint_id']]
            legacy_ep[('%surl' % endpoint['interface'])] = endpoint['url']
    for endpoints in v3_endpoints.values():
        legacy_ep = {}
        for key in ('service_id', 'enabled'):
            legacy_ep[key] = endpoints[0][key]
        legacy_ep['region'] = endpoints[0]['region_id']
        for endpoint in endpoints:
            if (endpoint['interface'] == 'public'):
                legacy_ep['id'] = endpoint['id']
            legacy_ep[('%surl' % endpoint['interface'])] = endpoint['url']
        if ('id' not in legacy_ep):
            continue
        legacy_endpoints[legacy_ep['id']] = legacy_ep
    return {'endpoints': list(legacy_endpoints.values())}
