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 delete(client, label): """Delete one or more labels. Deletes the LABELs with the given ids """ all_labels = Label.all(client) label = [Label.lookup(client, id, resources=all_labels) for id in label] for entry in label: entry.delete() click.echo("Deleted {} ".format(entry.id))
def list(client, label, **kwargs): """List labels. Lists information for a given LABEL or all labels in the organization. """ if label: labels = [Label.lookup(client, label, include=label_includes)] else: metadata = kwargs.get('metadata') or None labels = Label.where(client, include=label_includes, metadata=metadata) Label.display(client, labels, include=label_includes)
def list(client, label, **kwargs): """List labels. Lists information for a given LABEL or all labels in the organization. """ include = [Sensor] if label: labels = [Label.lookup(client, label, include=include)] else: labels = Label.all(client, include=include) Label.display(client, labels, include=include)
def test_display_map(client, capsys): labels = Label.all(client, include=[Sensor]) assert len(labels) > 0 display_map = Label.display_map(client) assert display_map is not None label = labels[0] values = [f(label) for f in display_map.values()] assert values is not None Label.display(client, [label]) out, err = capsys.readouterr() assert label.short_id in out
def update(client, label, name): """Update a label. Changes basic attributes on a label. To add or remove sensors or elements from a label see the `label element` and `label sensor` commands. """ label = Label.lookup(client, label) if name: label.update(attributes={'name': name}) label = Label.find(client, label.id, include=label_includes) Label.display(client, [label], include=label_includes)
def element(client, label, mac, **kwargs): """List elements for a label. List elements 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, Element, mac=mac, **kwargs) if actions.add is not None: label.add_elements(actions.add) if actions.remove is not None: label.remove_elements(actions.remove) if actions.replace is not None: label.update_elements(actions.replace) elements = label.elements() Element.display(client, elements, **kwargs)
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 create(ctx, name, **kwargs): """Create a label. Creates a label with a given NAME and an (optional) list of sensors associated with that label. """ client = ctx.find_object(Client) label = Label.create(client, name=name) ctx.invoke(update, client, label=label.id, **kwargs)
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)