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])
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)
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])
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)
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())
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)
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])
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])
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)
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)
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)
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)
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 [])
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))
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
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)
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
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')