Ejemplo n.º 1
0
def test_lookup(client, sensors, first_sensor):
    assert len(sensors) > 0
    # Look up by id
    lookup_sensor = Sensor.lookup(client, first_sensor.id, resources=sensors)
    assert first_sensor == lookup_sensor
    # Look up by short_id
    lookup_sensor = Sensor.lookup(client,
                                  first_sensor.short_id,
                                  resources=sensors)
    assert first_sensor == lookup_sensor
    # Look up by mac
    assert first_sensor.meta.last_seen is not None
    assert first_sensor.meta.mac is not None
    lookup_sensor = Sensor.lookup(client,
                                  first_sensor.meta.mac,
                                  mac=True,
                                  resources=sensors)
    assert first_sensor == lookup_sensor
    # Lookup by name
    lookup_sensor = Sensor.lookup(client, first_sensor.name, resources=sensors)
    assert first_sensor == lookup_sensor
    lookup_sensor = Sensor.filter(client,
                                  None,
                                  resource.filter_string_attribute('name'),
                                  resources=sensors)
    assert len(lookup_sensor) > 0

    # Test some lookup failures
    with pytest.raises(KeyError):
        Sensor.lookup(client, '8', mac=True)
    with pytest.raises(KeyError):
        Sensor.lookup(client, 'zzfs', mac=True)
Ejemplo n.º 2
0
def test_lookup(client, sensors, first_sensor):
    assert len(sensors) > 0
    # Look up by id
    lookup_sensor = Sensor.lookup(client, first_sensor.id,
                                  resources=sensors)
    assert first_sensor == lookup_sensor
    # Look up by short_id
    lookup_sensor = Sensor.lookup(client, first_sensor.short_id,
                                  resources=sensors)
    assert first_sensor == lookup_sensor
    # Look up by mac
    assert first_sensor.meta.last_seen is not None
    assert first_sensor.meta.mac is not None
    lookup_sensor = Sensor.lookup(client, first_sensor.meta.mac, mac=True,
                                  resources=sensors)
    assert first_sensor == lookup_sensor
    # Lookup by name
    lookup_sensor = Sensor.lookup(client, first_sensor.name,
                                  resources=sensors)
    assert first_sensor == lookup_sensor
    lookup_sensor = Sensor.filter(client, None,
                                  resource.filter_string_attribute('name'),
                                  resources=sensors)
    assert len(lookup_sensor) > 0

    # Test some lookup failures
    with pytest.raises(KeyError):
        Sensor.lookup(client, '8', mac=True)
    with pytest.raises(KeyError):
        Sensor.lookup(client, 'zzfs', mac=True)
Ejemplo n.º 3
0
def label(client, sensor, mac):
    """Get the labels for a sensor.

    Gets all the labels that a given SENSOR is part of.
    """
    sensor = Sensor.lookup(client, sensor, mac=mac)
    Sensor.display(client, sensor.labels())
Ejemplo n.º 4
0
def create(ctx, name, sensors, elements):
    """Create a label.

    Creates a label with a given NAME and an (optional) list of
    sensors and elements associated with that label.

    """
    client = ctx.find_object(Client)

    sensors = sensors or []
    if sensors:
        all_sensors = Sensor.all(client)
        sensors = [
            Sensor.lookup(client, id, resources=all_sensors) for id in sensors
        ]

    elements = elements or []
    if elements:
        all_elements = Element.all(client)
        elements = [
            Element.lookup(client, id, resources=all_elements)
            for id in elements
        ]

    label = Label.create(client, attributes={'name': name})

    if sensors:
        label.update_sensors(sensors)

    if elements:
        label.update_elements(elements)

    label = Label.find(client, label.id, include=label_includes)
    Label.display(client, [label], include=label_includes)
Ejemplo n.º 5
0
def element(client, sensor, mac):
    """Get the element for a sensor.

    Gets the element a given SENSOR was last seen connected to.
    """
    sensor = Sensor.lookup(client, sensor, mac=mac, include=[Element])
    element = sensor.element(use_included=True)
    Element.display(client, [element] if element else [])
Ejemplo n.º 6
0
def delete(client, sensor, mac, **kwargs):
    """Delete a sensor.

    Deletes the SENSOR with the given id.
    """
    sensor = Sensor.lookup(client, sensor, mac=mac)
    sensor.delete()
    click.echo("Deleted {}".format(sensor.id))
Ejemplo n.º 7
0
def update(client, sensor, name, mac, **kwargs):
    """Updates the attributes of a sensor.

    Updates the attributes of a given SENSOR.
    """
    sensor = Sensor.lookup(client, sensor, mac=mac)
    sensor = sensor.update(name=name)
    Sensor.display(client, [sensor])
Ejemplo n.º 8
0
def element(client, sensor, mac):
    """Get the element for a sensor.

    Gets the element a given SENSOR was last seen connected to.
    """
    sensor = Sensor.lookup(client, sensor, mac=mac, include=[Element])
    element = sensor.element(use_included=True)
    Element.display(client, [element] if element else [])
Ejemplo n.º 9
0
def delete(client, sensor, mac, **kwargs):
    """Delete a sensor.

    Deletes the SENSOR with the given id.
    """
    sensor = Sensor.lookup(client, sensor, mac=mac)
    sensor.delete()
    click.echo("Deleted {}".format(sensor.id))
Ejemplo n.º 10
0
def update(client, sensor, name, mac, **kwargs):
    """Updates the attributes of a sensor.

    Updates the attributes of a given SENSOR.
    """
    sensor = Sensor.lookup(client, sensor, mac=mac)
    sensor = sensor.update(attributes={'name': name})
    Sensor.display(client, [sensor])
Ejemplo n.º 11
0
def list(client, sensor, mac, **kwargs):
    """List sensors.

    Lists information for a given SENSOR or all sensors in the
    organization.

    """
    if sensor:
        sensors = [Sensor.lookup(client, sensor, mac=mac)]
    else:
        sensors = Sensor.all(client)
    Sensor.display(client, sensors, **kwargs)
Ejemplo n.º 12
0
def update(client, label, name, **kwargs):
    label = Label.lookup(client, label)
    if name:
        label.update(name=name)

    all_sensors = Sensor.all(client)
    add_sensors = kwargs.pop('add', None) or []
    remove_sensors = kwargs.pop('remove', None) or []
    sensors = [Sensor.lookup(client, s, resources=all_sensors)
               for s in add_sensors]
    if sensors:
        label.add_sensors(sensors)

    sensors = [Sensor.lookup(client, s, resources=all_sensors)
               for s in remove_sensors]
    if sensors:
        label.remove_sensors(sensors)

    include = [Sensor]
    label = Label.find(client, label.id, include=include)
    Label.display(client, [label], include=include)
Ejemplo n.º 13
0
def list(client, sensor, mac, **kwargs):
    """List sensors.

    Lists information for a given SENSOR or all sensors in the
    organization.

    """
    if sensor:
        sensors = [Sensor.lookup(client, sensor, mac=mac)]
    else:
        metadata = kwargs.get('metadata') or None
        sensors = Sensor.where(client, metadata=metadata)
    Sensor.display(client, sensors, **kwargs)