import click


def validate_relay(context, param, value):
    """
    Validates presence of relay by name.
    """
    relays = context.obj.api.relays()
    relay = next((r for r in relays
                  if r["name"] == value), None)
    if relay is None:
        error = "Relay \"%s\" does not exist" % value
        raise click.BadParameter(error)

    return relay