Exemplo n.º 1
0
def create(client, name):
    """Create a virtual sensor.

    Create a virtual sensor with a name.
    """
    sensor = Sensor.create(client, name=name)
    Sensor.display(client, [sensor])
Exemplo n.º 2
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)
Exemplo n.º 3
0
def create(client, name):
    """Create a virtual sensor.

    Create a virtual sensor with a name.
    """
    sensor = Sensor.create(client, attributes={'name': name})
    Sensor.display(client, [sensor])
Exemplo n.º 4
0
def sensor(client, label, mac, **kwargs):
    """List sensors for a label.

    List sensors for a given LABEL.

    Add, remove or replace sensors from the LABEL by using the --add,
    --remove or --replace arguments respectively. Note that you can
    specify "none" with these to indicate an empty list.

    """
    label = Label.lookup(client, label)

    actions = lookup_label_action_resources(client, Sensor, mac=mac, **kwargs)

    if actions.add is not None:
        label.add_sensors(actions.add)

    if actions.remove is not None:
        label.remove_sensors(actions.remove)

    if actions.replace is not None:
        label.update_sensors(actions.replace)

    sensors = label.sensors()
    Sensor.display(client, sensors, **kwargs)
Exemplo n.º 5
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())
Exemplo n.º 6
0
def sensor(client, label, **kwargs):
    """Lists sensors for a label.

    Lists sensors for a given LABEL.
    """
    label = Label.lookup(client, label, include=[Sensor])
    sensors = label.sensors(use_included=True)
    Sensor.display(client, sensors, **kwargs)
Exemplo 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])
Exemplo n.º 8
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])
Exemplo n.º 9
0
def sensor(client, element, mac, **kwargs):
    """Get the sensors for an element.

    Gets the sensors last known to be connected to a given ELEMENT.

    """
    element = Element.lookup(client, element, mac=mac, include=[Sensor])
    sensors = element.sensors(use_included=True)
    Sensor.display(client, sensors, **kwargs)
Exemplo n.º 10
0
def sensor(client, element, mac, **kwargs):
    """Get the sensors for an element.

    Gets the sensors last known to be connected to a given ELEMENT.

    """
    element = Element.lookup(client, element, mac=mac, include=[Sensor])
    sensors = element.sensors(use_included=True)
    Sensor.display(client, sensors, **kwargs)
Exemplo n.º 11
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)
Exemplo n.º 12
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)
Exemplo 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:
        sensors = Sensor.all(client)
    Sensor.display(client, sensors, **kwargs)
Exemplo n.º 14
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)
Exemplo n.º 15
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 [])
Exemplo n.º 16
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))
Exemplo n.º 17
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))
Exemplo n.º 18
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 [])
Exemplo n.º 19
0
def test_display_map(client, first_sensor, sensors):
    assert first_sensor is not None

    display_map = Sensor.display_map(client)
    assert display_map is not None

    for sensor in sensors:
        values = [f(sensor) for f in display_map.values()]
        assert values is not None
Exemplo n.º 20
0
def test_display_map(client, first_sensor, sensors):
    assert first_sensor is not None

    display_map = Sensor.display_map(client)
    assert display_map is not None

    for sensor in sensors:
        values = [f(sensor) for f in display_map.values()]
        assert values is not None
Exemplo n.º 21
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)
Exemplo n.º 22
0
def validate_format(output, client, sensors, capsys):
    first_sensor = sensors[0]

    # With sort
    Sensor.display(client, sensors, format=output, sort='name')
    out, err = capsys.readouterr()
    assert first_sensor.short_id in out

    Sensor.display(client, sensors, format=output, sort='name', reverse=True)
    reversed, err = capsys.readouterr()
    assert reversed != out

    # Without sort
    Sensor.display(client, sensors, format=output)
    out, err = capsys.readouterr()
    assert first_sensor.short_id in out

    Sensor.display(client, sensors, format=output, reverse=True)
    reversed, err = capsys.readouterr()
    assert reversed != out
Exemplo n.º 23
0
def test_formats(client, sensors, capsys):
    for output in ['csv', 'tabular', 'json']:
        validate_format(output, client, sensors, capsys)

    with pytest.raises(AttributeError):
        Sensor.display(client, sensors, format='xxx')