def get_all(self, vault_id, file_id): vault = Vault.get(request.project_id, vault_id) assert vault is not None f = vault.get_file(file_id) if not f: abort(404) inmarker = int(request.params.get('marker', 0)) limit = int(request.params.get('limit', conf.api_configuration.max_returned_num)) # Get the block generator from the metadata driver. # Note: +1 on limit is to fetch one past the limt # for the purpose of determining if the # list was truncated retblks = deuce.metadata_driver.create_file_block_generator( request.project_id, vault_id, file_id, inmarker, limit + 1) resp = list(retblks) truncated = len(resp) > 0 and len(resp) == limit + 1 outmarker = resp.pop()[1] if truncated else None if outmarker: query_args = {'marker': outmarker} query_args['limit'] = limit returl = set_qs(request.url, query_args) response.headers["X-Next-Batch"] = returl return resp
def get_one(self, attr_def_name): handler = (attribute_definition_handler.AttributeDefinitionHandler( pecan.request.security_context)) raw_def = handler.get(attr_def_name) if not raw_def: core.abort(404, '%s is not a attribute_definition' % attr_def_name) return raw_def.fix_uris(pecan.request.host_url)
def get_graph(graph_type, depth, query, root, all_tenants): TopologyController._check_input_para(graph_type, depth, query, root, all_tenants) try: graph_data = pecan.request.client.call(pecan.request.context, 'get_topology', graph_type=graph_type, depth=depth, query=query, root=root, all_tenants=all_tenants) LOG.info(graph_data) graph = json.loads(graph_data) if graph_type == 'graph': return graph if graph_type == 'tree': node_id = OPENSTACK_CLUSTER if root: for node in graph['nodes']: if node[VProps.VITRAGE_ID] == root: node_id = node[VProps.ID] break return RootRestController.as_tree(graph, node_id) except Exception as e: LOG.exception('failed to get topology %s ', e) abort(404, str(e))
def get_graph(graph_type, depth, query, root): try: graph_data = pecan.request.client.call(pecan.request.context, 'get_topology', graph_type=graph_type, depth=depth, query=query, root=root) LOG.info(graph_data) graph = json.loads(graph_data) if graph_type == 'graph': return graph if graph_type == 'tree': node_id = OPENSTACK_CLUSTER if root: for node in graph['nodes']: if node[VProps.VITRAGE_ID] == root: node_id = node[VProps.ID] break return RootRestController.as_tree(graph, node_id) except Exception as e: LOG.exception('failed to get topology %s ', e) abort(404, str(e))
def post(self, vault_id, file_id=None): """Initializes a new file. The location of the new file is returned in the Location header """ vault = Vault.get(vault_id) # caller tried to post to a vault that # does not exist if not vault: logger.error('Vault [{0}] does not exist'.format(vault_id)) abort(400, headers={"Transaction-ID": deuce.context.transaction.request_id}) # overload to use the same end-point for creating a new file # and assigning blocks to a file that is in progress if file_id is not None: return self._assign(vault, vault_id, file_id) file = vault.create_file() response.headers["Location"] = "files/%s" % file.file_id response.status_code = 201 # Created logger.info('File [{0}] created'. format(response.headers["Location"]))
def get_graph(graph_type, depth, query, root, all_tenants): TopologyController._check_input_para(graph_type, depth, query, root, all_tenants) try: graph_data = pecan.request.client.call(pecan.request.context, 'get_topology', graph_type=graph_type, depth=depth, query=query, root=root, all_tenants=all_tenants) graph = decompress_obj(graph_data) if graph_type == 'graph': return graph if graph_type == 'tree': if nx.__version__ >= '2.0': node_id = '' for node in graph['nodes']: if (root and node[VProps.VITRAGE_ID] == root) or \ (not root and node[VProps.ID] == CLUSTER_ID): node_id = node[VProps.GRAPH_INDEX] break else: node_id = CLUSTER_ID if root: for node in graph['nodes']: if node[VProps.VITRAGE_ID] == root: node_id = node[VProps.ID] break return RootRestController.as_tree(graph, node_id) except Exception: LOG.exception('failed to get topology.') abort(404, 'Failed to get topology.')
def get_graph(graph_type, depth, query, root, all_tenants): TopologyController._check_input_para(graph_type, depth, query, root, all_tenants) try: graph_data = pecan.request.client.call(pecan.request.context, 'get_topology', graph_type=graph_type, depth=depth, query=query, root=root, all_tenants=all_tenants) LOG.debug(graph_data) graph = json.loads(graph_data) if graph_type == 'graph': return graph if graph_type == 'tree': node_id = CLUSTER_ID if root: for node in graph['nodes']: if node[VProps.VITRAGE_ID] == root: node_id = node[VProps.ID] break return RootRestController.as_tree(graph, node_id) except Exception: LOG.exception('failed to get topology.') abort(404, 'Failed to get topology.')
def get_all(self, vault_id): vault = Vault.get(request.project_id, vault_id) if not vault: abort(404) inmarker = request.params.get('marker') limit = int(request.params.get('limit', conf.api_configuration.max_returned_num)) # The +1 is to fetch one past the user's # requested limit so that we can determine # if the list was truncated or not files = vault.get_files(inmarker, limit + 1) resp = list(files) # Note: the list may not actually be truncated truncated = len(resp) == limit + 1 outmarker = resp.pop().file_id if truncated else None if outmarker: query_args = {'marker': outmarker} query_args['limit'] = limit returl = set_qs(request.url, query_args) response.headers["X-Next-Batch"] = returl return resp
def get_one(self, type_def_name): handler = (type_definition_handler.TypeDefinitionHandler( pecan.request.security_context)) raw_def = handler.get(type_def_name) if not raw_def: core.abort(404, '%s is not a type_definition' % type_def_name) return raw_def.fix_uris(pecan.request.host_url)
def put(self, **kwargs): templates = kwargs['templates'] LOG.info("add template: %s", templates) enforce("template add", pecan.request.headers, pecan.request.enforcer, {}) template_type = kwargs['template_type'] params = kwargs.get('params') overwrite = kwargs.get('overwrite') if overwrite: names = [ template[1]['metadata']['name'] for template in templates ] uuids = self._to_uuids(names) self._delete_templates_and_wait(uuids) try: return self._add(templates, template_type, params) except Exception: LOG.exception('Failed to add template.') abort(404, 'Failed to add template.')
def post(self, **kwargs): resource_type = kwargs.get('resource_type', None) all_tenants = kwargs.get('all_tenants', False) all_tenants = bool_from_string(all_tenants) query = kwargs.get('query') group_by = kwargs.get('group_by') if query: query = json.loads(query) if all_tenants: enforce("count resources:all_tenants", pecan.request.headers, pecan.request.enforcer, {}) else: enforce("count resources", pecan.request.headers, pecan.request.enforcer, {}) LOG.info('received get resource counts') try: resource_counts_json = pecan.request.client.call( pecan.request.context, 'count_resources', resource_type=resource_type, all_tenants=all_tenants, query=query, group_by=group_by) return json.loads(resource_counts_json) except Exception: LOG.exception('failed to get resource count.') abort(404, 'Failed to get resource count.')
def _delete_registration(id): resource = pecan.request.client.call(pecan.request.context, 'delete_webhook', id=id) if not resource: abort(404, "Failed to find resource with ID: %s" % id) LOG.info("Request returned with: %s" % resource) return resource
def from_json(cls, dct): ret_val = cls() for key, value in dct.items(): if hasattr(ret_val, key): setattr(ret_val, key, value) else: core.abort(500, 'internal metadata is incorrect') return ret_val
def post(self): try: data = request.json return {'id': self.model.create(data)} except exc.ValidationError: abort(400) except exc.NotFound: abort(404)
def put(self, id): try: data = request.json self.model.update(id, data) except exc.ValidationError: abort(400) except exc.NotFound: abort(404)
def fix_uris(self, host_url): handler = (attribute_definition_handler.AttributeDefinitionHandler( pecan.request.security_context)) raw_def = handler.get(self.href) if not raw_def: core.abort(404, 'no attribute definition_link for %s' % self.href) self.target_name = raw_def.name self.href = uris.ATTRIBUTE_DEF_URI_STR % (host_url, self.href)
def get(self): LOG.info('get template versions') try: return pecan.request.client.call(pecan.request.context, 'template_versions') except Exception: LOG.exception('Failed to get template versions') abort(404, 'Failed to get template versions')
def _show_template(uuid): try: templates = pecan.request.storage.templates.query(uuid=uuid) if not templates: raise VitrageError("Template %s not found", uuid) return templates[0].file_content except Exception: LOG.exception('Failed to show template with uuid: %s ', uuid) abort(404, 'Failed to show template.')
def get_one(self, format_name): """Return the appropriate format resource.""" handler = (format_handler.FormatHandler( pecan.request.security_context)) raw_format = handler.get(format_name) if not raw_format: core.abort(404, '%s is not a format resource' % format_name) host_url = pecan.request.application_url.rstrip('/') return raw_format.fix_uris(host_url)
def get(self): """Return the appropriate parameter_definitions resource.""" handler = (parameter_definitions_handler.ParameterDefinitionsHandler( pecan.request.security_context)) raw_defs = handler.get(self._id) if not raw_defs: core.abort( 404, '%s is not a parameter_definitions collection' % self._id) return raw_defs.fix_uris(pecan.request.host_url)
def get_one(self, type_def_name): handler = (type_definition_handler. TypeDefinitionHandler(pecan.request.security_context)) raw_def = handler.get(type_def_name) if not raw_def: core.abort(404, '%s is not a type_definition' % type_def_name) return raw_def.fix_uris(pecan.request.host_url)
def _get(id): webhook = \ pecan.request.client.call(pecan.request.context, 'get_webhook', id=id) LOG.info(webhook) if not webhook: abort(404, "Failed to find webhook with ID: %s" % id) return webhook
def get_one(self, attr_def_name): handler = (attribute_definition_handler. AttributeDefinitionHandler(pecan.request.security_context)) raw_def = handler.get(attr_def_name) if not raw_def: core.abort(404, '%s is not a attribute_definition' % attr_def_name) return raw_def.fix_uris(pecan.request.host_url)
def _delete(uuid): try: results = pecan.request.client.call(pecan.request.context, 'delete_template', uuids=uuid) return results except Exception: LOG.exception('Failed to delete template.') abort(404, 'Failed to delete template.')
def post(self, **kwargs): LOG.info("Add webhook with following props: %s" % str(kwargs)) enforce('webhook add', pecan.request.headers, pecan.request.enforcer, {}) try: return self._post(**kwargs) except Exception as e: LOG.exception('Failed to add webhooks %s', e) abort(400, str(e))
def _get_templates(cls): try: templates = pecan.request.storage.templates.query() templates = [t for t in templates if t.status != TStatus.DELETED] templates.sort(key=lambda template: template.created_at) return [cls._db_template_to_dict(t) for t in templates] except Exception: LOG.exception('Failed to get template list.') abort(404, 'Failed to get template list.')
def _validate(templates): result_json = pecan.request.client.call(pecan.request.context, 'validate_template', templates=templates) try: return json.loads(result_json) except Exception as e: LOG.exception('failed to open template file(s) %s ', e) abort(404, str(e))
def post_event(event_time, event_type, details): try: pecan.request.client.call(pecan.request.context, 'post', event_time=event_time, event_type=event_type, details=details) except Exception: LOG.exception('Failed to post an event') abort(404, 'Failed to post event')
def _add(cls, templates, template_type): try: results = pecan.request.client.call(pecan.request.context, 'add_template', templates=templates, template_type=template_type) return results except Exception: LOG.exception('Failed to add template file.') abort(404, 'Failed to add template file.')
def _show_template(_id): uuid = TemplateController._to_uuid(_id) try: templates = pecan.request.storage.templates.query(uuid=uuid) if not templates or templates[0].status == TStatus.DELETED: raise VitrageError("Template %s not found", uuid) return templates[0].file_content except Exception: LOG.exception('Failed to show template with uuid: %s ', uuid) abort(404, 'Failed to show template.')
def get(self): enforce('get resource', pecan.request.headers, pecan.request.enforcer, {}) LOG.info(_LI('received get resource with id %s') % self.id) try: return self.get_resource(self.id) except Exception as e: LOG.exception('failed to get resource %s', e) abort(404, str(e))
def _delete(uuid): try: results = pecan.request.client.call( pecan.request.context, 'delete_template', uuids=uuid) return results except Exception as e: LOG.exception('failed to delete template %s ', e) abort(404, str(e))
def on_route(self, state): # Enforce the existence of the x-project-id header and assign # the value to the request project id. try: hdr_name = 'x-project-id' state.request.project_id = state.request.headers[hdr_name] # TODO: validate the project_id except KeyError: abort(400) # Invalid request
def _show_template(uuid): try: templates = pecan.request.storage.templates.query(uuid=uuid) if not templates: raise VitrageError("Template %s not found", uuid) return templates[0].file_content except Exception as e: to_unicode = encodeutils.exception_to_unicode(e) LOG.exception('failed to show template with uuid: %s ', to_unicode) abort(404, to_unicode)
def fix_uris(self, host_url): handler = (attribute_definition_handler. AttributeDefinitionHandler(pecan.request.security_context)) raw_def = handler.get(self.href) if not raw_def: core.abort(404, 'no attribute definition_link for %s' % self.href) self.target_name = raw_def.name self.href = uris.ATTRIBUTE_DEF_URI_STR % (host_url, self.href)
def get(self, vitrage_id): enforce("get alarm", pecan.request.headers, pecan.request.enforcer, {}) LOG.info('returns show alarm with vitrage id %s', vitrage_id) try: return self._show_alarm(vitrage_id) except Exception: LOG.exception('Failed to load JSON.') abort(404, "Failed to show alarm.")
def get(self): """Return the appropriate parameter_definitions resource.""" handler = (parameter_definitions_handler. ParameterDefinitionsHandler(pecan.request.security_context)) raw_defs = handler.get(self._id) if not raw_defs: core.abort(404, '%s is not a parameter_definitions collection' % self._id) return raw_defs.fix_uris(pecan.request.host_url)
def _get_templates(cls): try: templates = pecan.request.storage.templates.query() templates = [t for t in templates if t.status != TStatus.DELETED] templates.sort(key=lambda template: template.created_at) return [cls._db_template_to_dict(t) for t in templates] except Exception as e: to_unicode = encodeutils.exception_to_unicode(e) LOG.exception('failed to get template list %s ', to_unicode) abort(404, to_unicode)
def get(self): enforce("get resource", pecan.request.headers, pecan.request.enforcer, {}) LOG.info(_LI('received get resource with id %s') % self.id) try: return self.get_resource(self.id) except Exception as e: LOG.exception("failed to get resource %s", e) abort(404, str(e))
def get_one(self, format_name): """Return the appropriate format resource.""" handler = (format_handler. FormatHandler(pecan.request.security_context)) raw_format = handler.get(format_name) if not raw_format: core.abort(404, '%s is not a format resource' % format_name) return raw_format.fix_uris(pecan.request.host_url)
def _get_templates(): templates_json = pecan.request.client.call(pecan.request.context, 'get_templates') LOG.info(templates_json) try: template_list = json.loads(templates_json)['templates_details'] return template_list except Exception as e: LOG.exception('failed to get template list %s ', e) abort(404, str(e))
def index(self, resource_type=None): enforce("list resources", pecan.request.headers, pecan.request.enforcer, {}) LOG.info(_LI('received list resources with filter %s') % resource_type) try: return self.get_resources(resource_type) except Exception as e: LOG.exception("failed to get resources %s", e) abort(404, str(e))
def delete(self, vault_id, file_id): vault = Vault.get(vault_id) if not vault: abort(404) f = vault.get_file(file_id) if not f: abort(404) vault.delete_file(file_id)
def get_one(self, param_defs_name): """Return the appropriate format resource.""" handler = (parameter_definitions_handler. ParameterDefinitionsHandler(pecan.request.security_context)) raw_defs = handler.get(param_defs_name) if not raw_defs: core.abort(404, '%s is not a parameter_definitions collection' % param_defs_name) host_url = pecan.request.application_url.rstrip('/') return raw_defs.fix_uris(host_url)
def delete(self, **kwargs): uuid = kwargs['uuid'] LOG.info("delete template. uuid: %s", str(uuid)) enforce("template delete", pecan.request.headers, pecan.request.enforcer, {}) try: return self._delete(uuid) except Exception: LOG.exception('Failed to delete template.') abort(404, 'Failed to delete template.')
def get_rca(alarm_id): try: graph_data = pecan.request.client.call(pecan.request.context, 'get_rca', root=alarm_id) LOG.info(graph_data) graph = json.loads(graph_data) return graph except Exception as e: LOG.exception('failed to get rca %s ', e) abort(404, str(e))
def _show_template(template_uuid): template_json = pecan.request.client.call(pecan.request.context, 'show_template', template_uuid=template_uuid) LOG.info(template_json) try: return json.loads(template_json) except Exception as e: LOG.exception('failed to show template with uuid: %s ', e) abort(404, str(e))
def _assign(self, vault, vault_id, file_id): f = vault.get_file(file_id) if not f: logger.error('File [{0}] does not exist'.format(file_id)) abort(404, headers={"Transaction-ID": deuce.context.transaction.request_id}) if not request.body: try: # Fileid with an empty body will finalize the file. filesize = request.headers['Filesize'] if 'Filesize' \ in request.headers.keys() else 0 res = deuce.metadata_driver.finalize_file(vault_id, file_id, filesize) return res except Exception as e: # There are gaps or overlaps in blocks of the file # The list of errors returns # NEED RETURN 413 details = str(e) response.status_code = 413 logger.error('File [{0}] finalization ' 'failed; [{1}]'.format(file_id, details)) return details if f.finalized: # A finalized file cannot be # modified # TODO: Determine a better, more precise # status code logger.error('Finalized file [{0}] ' 'cannot be modified'.format(file_id)) abort(400, headers={"Transaction-ID": deuce.context.transaction.request_id}) blocks = request.json_body['blocks'] missing_blocks = list() for mapping in blocks: block_id = mapping['id'] offset = int(mapping['offset']) if not deuce.metadata_driver.has_block(vault_id, block_id): missing_blocks.append(block_id) deuce.metadata_driver.assign_block(vault_id, file_id, mapping['id'], mapping['offset']) return missing_blocks
def get_all(self): LOG.info(_LI('returns template list')) enforce("template list", pecan.request.headers, pecan.request.enforcer, {}) try: return self._get_templates() except Exception as e: LOG.exception('failed to get template list %s', e) abort(404, str(e))
def get_alarms(vitrage_id=None): alarms_json = pecan.request.client.call(pecan.request.context, 'get_alarms', arg=vitrage_id) LOG.info(alarms_json) try: alarms_list = json.loads(alarms_json)['alarms'] return alarms_list except Exception as e: LOG.exception('failed to open file %s ', e) abort(404, str(e))
def get(self, template_uuid): LOG.info(_LI('get template content')) enforce("template show", pecan.request.headers, pecan.request.enforcer, {}) try: return self._show_template(template_uuid) except Exception as e: LOG.exception('failed to show template %s' % template_uuid, e) abort(404, str(e))
def _assign(self, vault, vault_id, file_id): f = vault.get_file(file_id) if not f: abort(404) # Fileid with an empty body will finalize the file. filesize = request.headers['Filesize'] if 'Filesize' \ in request.headers.keys() else 0 if not request.body: try: res = deuce.metadata_driver.finalize_file(request.project_id, vault_id, file_id, filesize) return res except Exception as e: # There are gaps or overlaps in blocks of the file # The list of errors returns # NEED RETURN 413 details = str(e) response.status_code = 413 return details if f.finalized: # A finalized file cannot be # modified # TODO: Determine a better, more precise # status code abort(400) blocks = request.json_body['blocks'] missing_blocks = list() for mapping in blocks: block_id = mapping['id'] offset = int(mapping['offset']) if not deuce.metadata_driver.has_block(request.project_id, vault_id, block_id): missing_blocks.append(block_id) deuce.metadata_driver.assign_block(request.project_id, vault_id, file_id, mapping['id'], mapping['offset']) return missing_blocks
def post(self, vitrage_id): enforce("list alarms", pecan.request.headers, pecan.request.enforcer, {}) LOG.info(_LI('received list alarms with vitrage id %s') % vitrage_id) try: if pecan.request.cfg.api.use_mock_file: return self.get_mock_data('alarms.sample.json') else: return self.get_alarms(vitrage_id) except Exception as e: LOG.exception('failed to get alarms %s', e) abort(404, str(e))
def post(self, **kwargs): LOG.info(_LI('validate template. args: %s') % kwargs) enforce("template validate", pecan.request.headers, pecan.request.enforcer, {}) templates = kwargs['templates'] try: return self._validate(templates) except Exception as e: LOG.exception('failed to validate template(s) %s', e) abort(404, str(e))
def get_graph(self, graph_type): graph_data = self.client.call(self.ctxt, 'get_topology', arg=None) LOG.info(graph_data) # graph_file = pecan.request.cfg.find_file('graph.sample.json') try: if graph_type == 'graph': return json.loads(graph_data) if graph_type == 'tree': return json_graph.tree_data( json_graph.node_link_graph(json.loads(graph_data)), root='RESOURCE:node') except Exception as e: LOG.exception("failed to open file %s ", e) abort(404, str(e))
def on_route(self, state): # Enforce the existence of the x-project-id header and assign # the value to the request project id. try: if hasattr(state.request, 'path') and \ (state.request.path == '/v1.0/health' or state.request.path == '/v1.0/ping'): return deuce.context.project_id = state.request.headers['x-project-id'] # TODO: validate the project_id except KeyError: # Invalid request abort(400, comment="Missing Header : X-Project-ID", headers={ 'Transaction-ID': deuce.context.transaction.request_id })
def post(self, vault_id, file_id=None): """Initializes a new file. The location of the new file is returned in the Location header """ vault = Vault.get(request.project_id, vault_id) # caller tried to post to a vault that # does not exist if not vault: abort(400) if file_id is not None: return self._assign(vault, vault_id, file_id) file = vault.create_file() response.headers["Location"] = "files/%s" % file.file_id response.status_code = 201 # Created
def from_json(cls, dct): ret_val = cls() for key, value in dct.items(): if key == 'inherits_from': inherit_links = [] for l_dict in value: link = common_types.Link(href=l_dict['href'], target_name=l_dict['target_name']) inherit_links.append(link) setattr(ret_val, 'inherits_from', inherit_links) elif key == 'attribute_definition_links': ad_links = [] for ad_dct in value: ad_link = AttributeLink.from_json(ad_dct) ad_links.append(ad_link) setattr(ret_val, 'attribute_definition_links', ad_links) elif hasattr(ret_val, key): setattr(ret_val, key, value) else: core.abort(500, 'internal metadata is incorrect') return ret_val