Exemple #1
0
def show(driver, name):
    """
Returns a json representation of the given object

Example:

.. code:: bash

    $ ${post} -d 'name=showpool' ${server_url}/entity/pool
    [
        "/pool/showpool"
    ]
    HTTP: 201
    Content-type: application/json

.. code:: bash

    $ ${get} ${server_url}/entity/pool/showpool
    {
        "attrs": [],
        "contents": [],
        "driver": "pool",
        "name": "showpool",
        "parents": [],
        "type": "pool"
    }
    HTTP: 200
    Content-type: application/json

Will return a JSON representation of the previously created ``showpool``.

.. code:: bash

    $ ${get} ${server_url}/entity/basicserver/showpool
    "The driver for object \"showpool\" is not \"basicserver\""
    HTTP: 409
    Content-type: application/json

Will yield a 409 (Conflict) because the object ``showpool`` is not a
``basicserver`` object.
"""

    obj, status, msg = util.get(name, driver)
    if not obj:
        return util.dumps(msg, status)

    return util.dumps(util.show(obj))
Exemple #2
0
def show(driver, name):
    """
Returns a json representation of the given object

Example:

.. code:: bash

    $ ${post} -d 'name=showpool' ${server_url}/entity/pool
    [
        "/pool/showpool"
    ]
    HTTP: 201
    Content-type: application/json

.. code:: bash

    $ ${get} ${server_url}/entity/pool/showpool
    {
        "attrs": [],
        "contents": [],
        "driver": "pool",
        "name": "showpool",
        "parents": [],
        "type": "pool"
    }
    HTTP: 200
    Content-type: application/json

Will return a JSON representation of the previously created ``showpool``.

.. code:: bash

    $ ${get} ${server_url}/entity/basicserver/showpool
    "The driver for object \"showpool\" is not \"basicserver\""
    HTTP: 409
    Content-type: application/json

Will yield a 409 (Conflict) because the object ``showpool`` is not a
``basicserver`` object.
"""

    obj, status, msg = util.get(name, driver)
    if not obj:
        return util.dumps(msg, status)

    return util.dumps(util.show(obj))
Exemple #3
0
def show(driver, manager):
    """
Shows the details of the given resource manager, if it is a resource manager

Examples:

.. code:: bash

    $ ${post} -d 'name=nameman2' ${server_url}/resourcemanager/simplenamemanager
    {
        "attrs": [
            ...
        ],
        "contents": [],
        "count": 0,
        "driver": "simplenamemanager",
        "name": "nameman2",
        "parents": [],
        "type": "resourcemanager"
    }
    HTTP: 201
    Content-type: application/json

    $ ${get} ${server_url}/resourcemanager/simplenamemanager/nameman1
    {
        "attrs": [
            ...
        ],
        "contents": [],
        "count": 0,
        "driver": "simplenamemanager",
        "name": "nameman1",
        "parents": [],
        "type": "resourcemanager"
    }
    HTTP: 200
    Content-type: application/json

Will create the ``nameman2`` resource manager, then show its details. In this
case both operations yield the same data.

.. code:: bash

    $ ${get} ${server_url}/resourcemanager/simpleentitynamemanager/nonames
    "Object \"nonames\" not found (nonames does not exist.)"
    HTTP: 404
    Content-type: application/json

Will return a ``404`` error since the resource manager wasn't found

.. code:: bash

    $ ${get} ${server_url}/resourcemanager/nomanager/testnames
    "The driver \"nomanager\" is not a valid driver"
    HTTP: 412
    Content-type: application/json

Will return a ``412`` because the driver ``nomanager`` doesn't exist

.. code:: bash

    $ ${get} ${server_url}/resourcemanager/basicserver/testserver1
    "The object \"testserver1\" is not a resource manager"
    HTTP: 409
    Content-type: application/json

Will return a ``412`` instead because even though the driver ``basicserver``
exists, it is not a resource manager driver
"""

    obj, status, msg = _get_resource_manager(manager, driver)
    if not obj:
        return util.dumps(msg, status)
    else:
        return util.dumps(util.show(obj))
Exemple #4
0
def create(driver):
    """
This differs from the standard way of creating entities is that resource
managers can have a number of extra parameters added to them that not
necessarily match any of the other entities. These parameters are defined
by each resource manager driver and are pretty much arbitrary. Seems like
a good idea to separate these crucial differences.

Examples:

.. code:: bash

    $ ${post} -d 'name=nameman1' ${server_url}/resourcemanager/simplenamemanager
    {
        "attrs": [
            ...
        ],
        "contents": [],
        "count": 0,
        "driver": "simplenamemanager",
        "name": "nameman1",
        "parents": [],
        "type": "resourcemanager"
    }
    HTTP: 201
    Content-type: application/json

Will create a ``SimpleNameManager`` resource manager named ``namemgr1`` with
all default values set.

.. code:: bash

    $ ${post} -d 'name=ipman1' -d 'gateway=192.168.1.1' -d 'netmask=255.255.255.0' -d 'baseip=192.168.1.10' ${server_url}/resourcemanager/ipmanager
    {
        "attrs": [
            {
                "datatype": "string",
                "key": "baseip",
                "number": null,
                "subkey": "property",
                "value": "192.168.1.10"
            },
            {
                "datatype": "string",
                "key": "gateway",
                "number": null,
                "subkey": "property",
                "value": "192.168.1.1"
            },
            {
                "datatype": "string",
                "key": "netmask",
                "number": null,
                "subkey": "property",
                "value": "255.255.255.0"
            }
        ],
        "contents": [],
        "count": 0,
        "driver": "ipmanager",
        "name": "ipman1",
        "parents": [],
        "type": "resourcemanager"
    }
    HTTP: 201
    Content-type: application/json

Will create a  ``IPManager`` resource manager named ``ipman1`` with some
additional arguments such as ``netmask``, ``gateway`` and ``baseip``

"""
    if driver not in clusto.driverlist:
        return util.dumps('Requested driver "%s" does not exist' % (driver,), 412)
    cls = clusto.driverlist[driver]
    name = request.params.get('name')
    request.params.pop('name')

#   Pass any additional parameters as is to the constructor
    kwargs = {}
    for param, value in request.params.items():
        kwargs[param] = value

    found = None
    try:
        found = util.unclusto(clusto.get_by_name(name))
    except LookupError:
        pass

    obj = clusto.get_or_create(name, cls, **kwargs)

    headers = {}
    if found:
        headers['Warnings'] = 'Resource manager "%s" already exists' % (found,)

    code = 201
    if found:
        code = 202
    return util.dumps(util.show(obj), code, headers=headers)
Exemple #5
0
def list(driver=None):
    """
Returns all entities, or (optionally) all entities of the given driver

Example:

.. code:: bash

    $ ${get} ${server_url}/entity/
    [
        ...
    ]
    HTTP: 200
    Content-type: application/json

Will list all entities

Example:

.. code:: bash

    $ ${get} ${server_url}/entity/clustometa
    [
        "/clustometa/clustometa"
    ]
    HTTP: 200
    Content-type: application/json

Will list all entities that match the driver ``clustometa``

The following example should fail because there is no driver ``nondriver``:

.. code:: bash

    $ ${get} ${server_url}/entity/nondriver
    "The requested driver \"nondriver\" does not exist"
    HTTP: 412
    Content-type: application/json

"""

    result = []
    kwargs = {}
    mode = bottle.request.headers.get('Clusto-Mode', default='compact')
    headers = {}
    try:
        # Assignments are moved into the try block because of the int casting.
        current = int(bottle.request.headers.get('Clusto-Page', default='0'))
        per = int(bottle.request.headers.get('Clusto-Per-Page', default='50'))
    except TypeError as ve:
        return util.dumps('%s' % (ve,), 400)

    for param in request.params.keys():
        kwargs[param] = request.params.getall(param)
    if driver:
        if driver in clusto.driverlist:
            kwargs['clusto_drivers'] = [clusto.driverlist[driver]]
        else:
            return util.dumps('The requested driver "%s" does not exist' % (driver,), 412)
    ents = clusto.get_entities(**kwargs)
    if current:
        ents, total = util.page(ents, current=current, per=per)
        headers['Clusto-Pages'] = total
        headers['Clusto-Per-Page'] = per
        headers['Clusto-Page'] = current

    for ent in ents:
        result.append(util.show(ent, mode))
    return util.dumps(result, headers=headers)
Exemple #6
0
def list(driver=None):
    """
Returns all entities, or (optionally) all entities of the given driver

Example:

.. code:: bash

    $ ${get} ${server_url}/entity/
    [
        ...
    ]
    HTTP: 200
    Content-type: application/json

Will list all entities

Example:

.. code:: bash

    $ ${get} ${server_url}/entity/clustometa
    [
        "/clustometa/clustometa"
    ]
    HTTP: 200
    Content-type: application/json

Will list all entities that match the driver ``clustometa``

The following example should fail because there is no driver ``nondriver``:

.. code:: bash

    $ ${get} ${server_url}/entity/nondriver
    "The requested driver \"nondriver\" does not exist"
    HTTP: 412
    Content-type: application/json

"""

    result = []
    kwargs = {}
    mode = bottle.request.headers.get('Clusto-Mode', default='compact')
    headers = {}
    try:
        # Assignments are moved into the try block because of the int casting.
        current = int(bottle.request.headers.get('Clusto-Page', default='0'))
        per = int(bottle.request.headers.get('Clusto-Per-Page', default='50'))
    except TypeError as ve:
        return util.dumps('%s' % (ve, ), 400)

    for param in request.params.keys():
        kwargs[param] = request.params.getall(param)
    if driver:
        if driver in clusto.driverlist:
            kwargs['clusto_drivers'] = [clusto.driverlist[driver]]
        else:
            return util.dumps(
                'The requested driver "%s" does not exist' % (driver, ), 412)
    ents = clusto.get_entities(**kwargs)
    if current:
        ents, total = util.page(ents, current=current, per=per)
        headers['Clusto-Pages'] = total
        headers['Clusto-Per-Page'] = per
        headers['Clusto-Page'] = current

    for ent in ents:
        result.append(util.show(ent, mode))
    return util.dumps(result, headers=headers)