예제 #1
0
def create_inventory(req):
    """POST to create one inventory.

    On success return a 201 response, a location header pointing
    to the newly created inventory and an application/json representation
    of the inventory.
    """
    context = req.environ['placement.context']
    uuid = util.wsgi_path_item(req.environ, 'uuid')
    resource_provider = rp_obj.ResourceProvider.get_by_uuid(context, uuid)
    data = _extract_inventory(req.body, schema.POST_INVENTORY_SCHEMA)
    resource_class = data.pop('resource_class')

    inventory = _make_inventory_object(resource_provider, resource_class,
                                       **data)

    try:
        resource_provider.add_inventory(inventory)
    except (exception.ConcurrentUpdateDetected,
            db_exc.DBDuplicateEntry) as exc:
        raise webob.exc.HTTPConflict(
            _('Update conflict: %(error)s') % {'error': exc})
    except (exception.InvalidInventoryCapacity, exception.NotFound) as exc:
        raise webob.exc.HTTPBadRequest(
            _('Unable to create inventory for resource provider '
              '%(rp_uuid)s: %(error)s') % {
                  'rp_uuid': resource_provider.uuid,
                  'error': exc
              })

    response = req.response
    response.location = util.inventory_url(req.environ, resource_provider,
                                           resource_class)
    return _send_inventory(req, resource_provider, inventory, status=201)
예제 #2
0
 def test_inventory_url(self):
     resource_class = 'DISK_GB'
     environ = {}
     expected_url = ('/resource_providers/%s/inventories/%s'
                     % (uuidsentinel.rp_uuid, resource_class))
     self.assertEqual(expected_url, util.inventory_url(
         environ, self.resource_provider, resource_class))
예제 #3
0
 def test_inventories_url(self):
     environ = {}
     expected_url = ('/resource_providers/%s/inventories' %
                     uuidsentinel.rp_uuid)
     self.assertEqual(expected_url,
                      util.inventory_url(environ, self.resource_provider))