def get_endpoint(service_catalog, service_type='image', endpoint_region=None, endpoint_type='publicURL'): """ Select an endpoint from the service catalog We search the full service catalog for services matching both type and region. If the client supplied no region then any 'image' endpoint is considered a match. There must be one -- and only one -- successful match in the catalog, otherwise we will raise an exception. """ endpoint = None for service in service_catalog: s_type = None try: s_type = service['type'] except KeyError: msg = _('Encountered service with no "type": %s') % s_type LOG.warn(msg) continue if s_type == service_type: for ep in service['endpoints']: if endpoint_region is None or endpoint_region == ep['region']: if endpoint is not None: # This is a second match, abort raise exception.RegionAmbiguity(region=endpoint_region) endpoint = ep if endpoint and endpoint.get(endpoint_type): return endpoint[endpoint_type] else: raise exception.NoServiceEndpoint()
def get_endpoint(service_catalog, service_type='image', endpoint_region=None, endpoint_type='publicURL'): """ Select an endpoint from the service catalog We search the full service catalog for services matching both type and region. If the client supplied no region then any 'image' endpoint is considered a match. There must be one -- and only one -- successful match in the catalog, otherwise we will raise an exception. """ endpoints = ks_service_catalog.ServiceCatalogV2({ 'serviceCatalog': service_catalog }).get_urls(service_type=service_type, region_name=endpoint_region, endpoint_type=endpoint_type) if endpoints is None: raise exception.NoServiceEndpoint() elif len(endpoints) == 1: return endpoints[0] else: raise exception.RegionAmbiguity(region=endpoint_region)
def get_endpoint(service_catalog): """ Select an endpoint from the service catalog We search the full service catalog for services matching both type and region. If the client supplied no region then any 'image' endpoint is considered a match. There must be one -- and only one -- successful match in the catalog, otherwise we will raise an exception. """ # FIXME(sirp): for now just use the public url. endpoint = None region = self.creds.get('region') for service in service_catalog: try: service_type = service['type'] except KeyError: msg = _('Encountered service with no "type": %s' % service) logger.warn(msg) continue if service_type == 'image': for ep in service['endpoints']: if region is None or region == ep['region']: if endpoint is not None: # This is a second match, abort raise exception.RegionAmbiguity(region=region) endpoint = ep if endpoint is None: raise exception.NoServiceEndpoint() return endpoint['publicURL']
def _v2_auth(self, token_url): creds = self.creds creds = { "auth": { "tenantName": creds['tenant'], "passwordCredentials": { "username": creds['username'], "password": creds['password'] } } } headers = {} headers['Content-Type'] = 'application/json' req_body = json.dumps(creds) resp, resp_body = self._do_request(token_url, 'POST', headers=headers, body=req_body) if resp.status == 200: resp_auth = json.loads(resp_body)['access'] # FIXME(sirp): for now just using the first endpoint we get back # from the service catalog for glance, and using the public url. for service in resp_auth['serviceCatalog']: if service['type'] == 'image': glance_endpoint = service['endpoints'][0]['publicURL'] break else: raise exception.NoServiceEndpoint() self.management_url = glance_endpoint self.auth_token = resp_auth['token']['id'] elif resp.status == 305: raise exception.RedirectException(resp['location']) elif resp.status == 400: raise exception.AuthBadRequest(url=token_url) elif resp.status == 401: raise exception.NotAuthorized() elif resp.status == 404: raise exception.AuthUrlNotFound(url=token_url) else: raise Exception(_('Unexpected response: %s') % resp.status)