def delete(self): if not api_utils.allow_allocations(): raise exception.NotFound() context = pecan.request.context cdict = context.to_policy_values() policy.authorize('baremetal:allocation:delete', cdict, cdict) rpc_node = api_utils.get_rpc_node_with_suffix(self.parent_node_ident) allocations = objects.Allocation.list( pecan.request.context, filters={'node_uuid': rpc_node.uuid}) try: rpc_allocation = allocations[0] except IndexError: raise exception.AllocationNotFound( _("Allocation for node %s was not found") % self.parent_node_ident) notify.emit_start_notification(context, rpc_allocation, 'delete', node_uuid=rpc_node.uuid) with notify.handle_error_notification(context, rpc_allocation, 'delete', node_uuid=rpc_node.uuid): topic = pecan.request.rpcapi.get_random_topic() pecan.request.rpcapi.destroy_allocation(context, rpc_allocation, topic) notify.emit_end_notification(context, rpc_allocation, 'delete', node_uuid=rpc_node.uuid)
def delete(self, allocation_ident): """Delete an allocation. :param allocation_ident: UUID or logical name of an allocation. """ if not api_utils.allow_allocations(): raise exception.NotFound() context = pecan.request.context cdict = context.to_policy_values() policy.authorize('baremetal:allocation:delete', cdict, cdict) rpc_allocation = api_utils.get_rpc_allocation_with_suffix( allocation_ident) if rpc_allocation.node_id: node_uuid = objects.Node.get_by_id(pecan.request.context, rpc_allocation.node_id).uuid else: node_uuid = None notify.emit_start_notification(context, rpc_allocation, 'delete', node_uuid=node_uuid) with notify.handle_error_notification(context, rpc_allocation, 'delete', node_uuid=node_uuid): topic = pecan.request.rpcapi.get_random_topic() pecan.request.rpcapi.destroy_allocation(context, rpc_allocation, topic) notify.emit_end_notification(context, rpc_allocation, 'delete', node_uuid=node_uuid)
def get_all(self, node=None, resource_class=None, state=None, marker=None, limit=None, sort_key='id', sort_dir='asc', fields=None): """Retrieve a list of allocations. :param node: UUID or name of a node, to get only allocations for that node. :param resource_class: Filter by requested resource class. :param state: Filter by allocation state. :param marker: pagination marker for large data sets. :param limit: maximum number of resources to return in a single result. This value cannot be larger than the value of max_limit in the [api] section of the ironic configuration, or only max_limit resources will be returned. :param sort_key: column to sort results by. Default: id. :param sort_dir: direction to sort. "asc" or "desc". Default: asc. :param fields: Optional, a list with a specified set of fields of the resource to be returned. """ if not api_utils.allow_allocations(): raise exception.NotFound() cdict = pecan.request.context.to_policy_values() policy.authorize('baremetal:allocation:get', cdict, cdict) return self._get_allocations_collection(node, resource_class, state, marker, limit, sort_key, sort_dir, fields=fields)
def post(self, allocation): """Create a new allocation. :param allocation: an allocation within the request body. """ if not api_utils.allow_allocations(): raise exception.NotFound() context = pecan.request.context cdict = context.to_policy_values() policy.authorize('baremetal:allocation:create', cdict, cdict) if allocation.node_uuid is not wtypes.Unset: msg = _("Cannot set node_uuid when creating an allocation") raise exception.Invalid(msg) if (allocation.name and not api_utils.is_valid_logical_name(allocation.name)): msg = _("Cannot create allocation with invalid name " "'%(name)s'") % {'name': allocation.name} raise exception.Invalid(msg) if allocation.traits: for trait in allocation.traits: api_utils.validate_trait(trait) if allocation.candidate_nodes: # Convert nodes from names to UUIDs and check their validity converted = [] for node in allocation.candidate_nodes: try: node = api_utils.get_rpc_node(node) except exception.NodeNotFound as exc: exc.code = http_client.BAD_REQUEST raise else: converted.append(node.uuid) allocation.candidate_nodes = converted all_dict = allocation.as_dict() # NOTE(yuriyz): UUID is mandatory for notifications payload if not all_dict.get('uuid'): all_dict['uuid'] = uuidutils.generate_uuid() new_allocation = objects.Allocation(context, **all_dict) topic = pecan.request.rpcapi.get_random_topic() notify.emit_start_notification(context, new_allocation, 'create') with notify.handle_error_notification(context, new_allocation, 'create'): new_allocation = pecan.request.rpcapi.create_allocation( context, new_allocation, topic) notify.emit_end_notification(context, new_allocation, 'create') # Set the HTTP Location Header pecan.response.location = link.build_url('allocations', new_allocation.uuid) return Allocation.convert_with_links(new_allocation)
def _route(self, args, request=None): if not api_utils.allow_allocations(): msg = _("The API version does not allow allocations") if api.request.method == "GET": raise webob_exc.HTTPNotFound(msg) else: raise webob_exc.HTTPMethodNotAllowed(msg) return super(AllocationsController, self)._route(args, request)
def _route(self, args, request=None): if not api_utils.allow_allocations(): msg = _("The API version does not allow allocations") if pecan.request.method == "GET": raise webob_exc.HTTPNotFound(msg) else: raise webob_exc.HTTPMethodNotAllowed(msg) return super(AllocationsController, self)._route(args, request)
def get_all(self, fields=None): if not api_utils.allow_allocations(): raise exception.NotFound() cdict = pecan.request.context.to_policy_values() policy.authorize('baremetal:allocation:get', cdict, cdict) result = self.inner._get_allocations_collection(self.parent_node_ident, fields=fields) try: return result.allocations[0] except IndexError: raise exception.AllocationNotFound( _("Allocation for node %s was not found") % self.parent_node_ident)
def get_one(self, allocation_ident, fields=None): """Retrieve information about the given allocation. :param allocation_ident: UUID or logical name of an allocation. :param fields: Optional, a list with a specified set of fields of the resource to be returned. """ if not api_utils.allow_allocations(): raise exception.NotFound() cdict = pecan.request.context.to_policy_values() policy.authorize('baremetal:allocation:get', cdict, cdict) rpc_allocation = api_utils.get_rpc_allocation_with_suffix( allocation_ident) return Allocation.convert_with_links(rpc_allocation, fields=fields)
def convert(): v1 = V1() v1.id = "v1" v1.links = [link.Link.make_link('self', pecan.request.public_url, 'v1', '', bookmark=True), link.Link.make_link('describedby', 'https://docs.openstack.org', '/ironic/latest/contributor/', 'webapi.html', bookmark=True, type='text/html') ] v1.media_types = [MediaType('application/json', 'application/vnd.openstack.ironic.v1+json')] v1.chassis = [link.Link.make_link('self', pecan.request.public_url, 'chassis', ''), link.Link.make_link('bookmark', pecan.request.public_url, 'chassis', '', bookmark=True) ] v1.nodes = [link.Link.make_link('self', pecan.request.public_url, 'nodes', ''), link.Link.make_link('bookmark', pecan.request.public_url, 'nodes', '', bookmark=True) ] v1.ports = [link.Link.make_link('self', pecan.request.public_url, 'ports', ''), link.Link.make_link('bookmark', pecan.request.public_url, 'ports', '', bookmark=True) ] if utils.allow_portgroups(): v1.portgroups = [ link.Link.make_link('self', pecan.request.public_url, 'portgroups', ''), link.Link.make_link('bookmark', pecan.request.public_url, 'portgroups', '', bookmark=True) ] v1.drivers = [link.Link.make_link('self', pecan.request.public_url, 'drivers', ''), link.Link.make_link('bookmark', pecan.request.public_url, 'drivers', '', bookmark=True) ] if utils.allow_volume(): v1.volume = [ link.Link.make_link('self', pecan.request.public_url, 'volume', ''), link.Link.make_link('bookmark', pecan.request.public_url, 'volume', '', bookmark=True) ] if utils.allow_ramdisk_endpoints(): v1.lookup = [link.Link.make_link('self', pecan.request.public_url, 'lookup', ''), link.Link.make_link('bookmark', pecan.request.public_url, 'lookup', '', bookmark=True) ] v1.heartbeat = [link.Link.make_link('self', pecan.request.public_url, 'heartbeat', ''), link.Link.make_link('bookmark', pecan.request.public_url, 'heartbeat', '', bookmark=True) ] if utils.allow_expose_conductors(): v1.conductors = [link.Link.make_link('self', pecan.request.public_url, 'conductors', ''), link.Link.make_link('bookmark', pecan.request.public_url, 'conductors', '', bookmark=True) ] if utils.allow_allocations(): v1.allocations = [link.Link.make_link('self', pecan.request.public_url, 'allocations', ''), link.Link.make_link('bookmark', pecan.request.public_url, 'allocations', '', bookmark=True) ] if utils.allow_expose_events(): v1.events = [link.Link.make_link('self', pecan.request.public_url, 'events', ''), link.Link.make_link('bookmark', pecan.request.public_url, 'events', '', bookmark=True) ] if utils.allow_deploy_templates(): v1.deploy_templates = [ link.Link.make_link('self', pecan.request.public_url, 'deploy_templates', ''), link.Link.make_link('bookmark', pecan.request.public_url, 'deploy_templates', '', bookmark=True) ] v1.version = version.default_version() return v1
def _route(self, args, request=None): if not api_utils.allow_allocations(): raise webob_exc.HTTPNotFound( _("The API version does not allow allocations")) return super(NodeAllocationController, self)._route(args, request)
def _route(self, args, request=None): if not api_utils.allow_allocations(): raise webob_exc.HTTPNotFound(_( "The API version does not allow allocations")) return super(NodeAllocationController, self)._route(args, request)
def v1(): v1 = { 'id': "v1", 'links': [ link.make_link('self', api.request.public_url, 'v1', '', bookmark=True), link.make_link('describedby', 'https://docs.openstack.org', '/ironic/latest/contributor/', 'webapi.html', bookmark=True, type='text/html') ], 'media_types': { 'base': 'application/json', 'type': 'application/vnd.openstack.ironic.v1+json' }, 'chassis': [ link.make_link('self', api.request.public_url, 'chassis', ''), link.make_link('bookmark', api.request.public_url, 'chassis', '', bookmark=True) ], 'nodes': [ link.make_link('self', api.request.public_url, 'nodes', ''), link.make_link('bookmark', api.request.public_url, 'nodes', '', bookmark=True) ], 'ports': [ link.make_link('self', api.request.public_url, 'ports', ''), link.make_link('bookmark', api.request.public_url, 'ports', '', bookmark=True) ], 'drivers': [ link.make_link('self', api.request.public_url, 'drivers', ''), link.make_link('bookmark', api.request.public_url, 'drivers', '', bookmark=True) ], 'version': version.default_version() } if utils.allow_portgroups(): v1['portgroups'] = [ link.make_link('self', api.request.public_url, 'portgroups', ''), link.make_link('bookmark', api.request.public_url, 'portgroups', '', bookmark=True) ] if utils.allow_volume(): v1['volume'] = [ link.make_link('self', api.request.public_url, 'volume', ''), link.make_link('bookmark', api.request.public_url, 'volume', '', bookmark=True) ] if utils.allow_ramdisk_endpoints(): v1['lookup'] = [ link.make_link('self', api.request.public_url, 'lookup', ''), link.make_link('bookmark', api.request.public_url, 'lookup', '', bookmark=True) ] v1['heartbeat'] = [ link.make_link('self', api.request.public_url, 'heartbeat', ''), link.make_link('bookmark', api.request.public_url, 'heartbeat', '', bookmark=True) ] if utils.allow_expose_conductors(): v1['conductors'] = [ link.make_link('self', api.request.public_url, 'conductors', ''), link.make_link('bookmark', api.request.public_url, 'conductors', '', bookmark=True) ] if utils.allow_allocations(): v1['allocations'] = [ link.make_link('self', api.request.public_url, 'allocations', ''), link.make_link('bookmark', api.request.public_url, 'allocations', '', bookmark=True) ] if utils.allow_expose_events(): v1['events'] = [ link.make_link('self', api.request.public_url, 'events', ''), link.make_link('bookmark', api.request.public_url, 'events', '', bookmark=True) ] if utils.allow_deploy_templates(): v1['deploy_templates'] = [ link.make_link('self', api.request.public_url, 'deploy_templates', ''), link.make_link('bookmark', api.request.public_url, 'deploy_templates', '', bookmark=True) ] return v1