Esempio n. 1
0
def delete(ctx, names):
    """Delete an area.

    NAMES - one or more area names or id to delete
    """
    ctx.auto_output("data")
    excode = 0

    for name in names:
        area = api.find_area(ctx, name)
        if not area:
            _LOGGING.error("Could not find area with id or name: %s", name)
            excode = 1
        else:
            result = api.delete_area(ctx, area['area_id'])

            ctx.echo(
                helper.format_output(
                    ctx,
                    [result],
                    columns=ctx.columns
                    if ctx.columns else const.COLUMNS_DEFAULT,
                ))

    if excode != 0:
        sys.exit(excode)
Esempio n. 2
0
def delete(ctx, names):
    """Delete an area.

    NAMES - one or more area names or id to delete
    """
    ctx.auto_output("data")
    excode = 0

    for name in names:
        area = api.find_area(ctx, name)
        if not area:
            _LOGGING.error("Could not find area with id or name: %s", name)
            excode = 1
        else:
            result = api.delete_area(ctx, area['area_id'])

            ctx.echo(
                helper.format_output(
                    ctx,
                    [result],
                    columns=ctx.columns
                    if ctx.columns
                    else const.COLUMNS_DEFAULT,
                )
            )

    if excode != 0:
        sys.exit(excode)
Esempio n. 3
0
def assign(
    ctx: Configuration,
    area_id_or_name,
    names: List[str],
    match: Optional[str] = None,
):
    """Update area on one or more devices.

    NAMES - one or more name or id (Optional)
    """
    ctx.auto_output("data")

    devices = api.get_devices(ctx)

    result = []  # type: List[Dict]

    area = api.find_area(ctx, area_id_or_name)
    if not area:
        _LOGGING.error("Could not find area with id or name: %s",
                       area_id_or_name)
        sys.exit(1)

    if match:
        if match == ".*":
            result = devices
        else:
            devicefilterre = re.compile(match)  # type: Pattern

            for device in devices:
                if devicefilterre.search(device['name']):
                    result.append(device)

    for id_or_name in names:
        device = next(
            (x for x in devices if x['id'] == id_or_name),
            None  # type: ignore
        )
        if not device:
            device = next(
                (x for x in devices if x['name'] == id_or_name),
                None,  # type: ignore
            )
        if not device:
            _LOGGING.error("Could not find device with id or name: %s",
                           id_or_name)
            sys.exit(1)
        result.append(device)

    for device in result:
        output = api.assign_area(ctx, device['id'], area['area_id'])
        if output['success']:
            ctx.echo("Successfully assigned '{}' to '{}'".format(
                area['name'], device['name']))
        else:
            _LOGGING.error("Failed to assign '%s' to '%s'", area['name'],
                           device['name'])

            ctx.echo(str(output))
Esempio n. 4
0
def rename(ctx, oldname, newname):
    """Rename an area."""
    ctx.auto_output("data")

    area = api.find_area(ctx, oldname)
    if not area:
        _LOGGING.error("Could not find area with id or name: %s", oldname)
        sys.exit(1)

    result = api.rename_area(ctx, area['area_id'], newname)

    ctx.echo(
        helper.format_output(
            ctx,
            [result],
            columns=ctx.columns if ctx.columns else const.COLUMNS_DEFAULT,
        ))
Esempio n. 5
0
def rename(ctx, oldname, newname):
    """Rename an area."""
    ctx.auto_output("data")

    area = api.find_area(ctx, oldname)
    if not area:
        _LOGGING.error("Could not find area with id or name: %s", oldname)
        sys.exit(1)

    result = api.rename_area(ctx, area['area_id'], newname)

    ctx.echo(
        helper.format_output(
            ctx,
            [result],
            columns=ctx.columns if ctx.columns else const.COLUMNS_DEFAULT,
        )
    )
def assign(
    ctx: Configuration,
    area_id_or_name,
    names: List[str],
    match: Optional[str] = None,
):
    """Update area on one or more devices.

    NAMES - one or more name or id (Optional)
    """
    ctx.auto_output("data")

    devices = api.get_devices(ctx)

    result = []  # type: List[Dict]

    area = api.find_area(ctx, area_id_or_name)
    if not area:
        _LOGGING.error(
            "Could not find area with id or name: %s", area_id_or_name
        )
        sys.exit(1)

    if match:
        if match == ".*":
            result = devices
        else:
            devicefilterre = re.compile(match)  # type: Pattern

            for device in devices:
                if devicefilterre.search(device['name']):
                    result.append(device)

    for id_or_name in names:
        device = next(  # type: ignore
            (x for x in devices if x['id'] == id_or_name), None
        )
        if not device:
            device = next(  # type: ignore
                (x for x in devices if x['name'] == id_or_name), None
            )
        if not device:
            _LOGGING.error(
                "Could not find device with id or name: %s", id_or_name
            )
            sys.exit(1)
        result.append(device)

    for device in result:
        output = api.assign_area(ctx, device['id'], area['area_id'])
        if output['success']:
            ctx.echo(
                "Successfully assigned '{}' to '{}'".format(
                    area['name'], device['name']
                )
            )
        else:
            _LOGGING.error(
                "Failed to assign '%s' to '%s'", area['name'], device['name']
            )

            ctx.echo(str(output))