def rest_api_request(token, method, api_cmd, api_cmd_headers=None, api_cmd_payload=None): """ Make a rest-api request """ try: request_info = urlrequest.Request(api_cmd) request_info.get_method = lambda: method request_info.add_header("X-Auth-Token", token.get_id()) request_info.add_header("Accept", "application/json") if api_cmd_headers is not None: for header_type, header_value in api_cmd_headers.items(): request_info.add_header(header_type, header_value) if api_cmd_payload is not None: request_info.add_header("Content-type", "application/json") request_info.add_data(api_cmd_payload) request = urlrequest.urlopen(request_info) response = request.read() if response == "": response = json.loads("{}") else: response = json.loads(response) request.close() return response except HTTPError as e: if httplib.UNAUTHORIZED == e.code: token.set_expired() LOG.exception(e) raise KeystoneFail("REST API HTTP Error for url: %s. Error: %s" % (api_cmd, e)) except (URLError, httplib.BadStatusLine) as e: LOG.exception(e) raise KeystoneFail("REST API URL Error for url: %s. Error: %s" % (api_cmd, e))
def get_service_id(self, name, type): """ Search the services for the id """ for service in self._data['services']: if service['name'] == name: if service['type'] == type: return service['id'] raise KeystoneFail( ("Keystone service type %s, name %s not available" % (type, name)))
def get_service_url(self, region_name, service_id, endpoint_type): """ Search the endpoints for the url """ for endpoint in self._data['endpoints']: if endpoint['service_id'] == service_id: if (endpoint['region'] == region_name and endpoint['interface'] == endpoint_type): return endpoint['url'] raise KeystoneFail(( "Keystone service id %s, region %s, endpoint type %s not " "available" % (service_id, region_name, endpoint_type)))
def get_service_url(self, region_name, service_name, service_type, endpoint_type): """ Search the catalog of a service in a region for the url """ for catalog in self._data['token']['catalog']: if catalog['type'] == service_type: if catalog['name'] == service_name: if 0 != len(catalog['endpoints']): for endpoint in catalog['endpoints']: if (endpoint['region'] == region_name and endpoint['interface'] == endpoint_type): return endpoint['url'] raise KeystoneFail(( "Keystone service type %s, name %s, region %s, endpoint type %s " "not available" % (service_type, service_name, region_name, endpoint_type)))